Variables and Assignments

COS1511 - Introduction To Programming I · C++ Basics

Variables and Assignments

In C++, a variable is a named storage location in memory that can hold a value. You can think of a variable as a container that stores data you can use in your program. Each variable has a type, which determines what kind of data it can hold. This topic will focus on how to declare variables and assign values to them.

Declaring Variables

To use a variable in C++, you must first declare it. A declaration tells the compiler the name of the variable and its type. The syntax for declaring a variable is:

type variableName;

For example, to declare an integer variable named age, you would write:

int age;

In this example, int is the type, indicating that age will hold integer values.

Remember: Always specify the type of the variable when declaring it.

Assigning Values to Variables

After declaring a variable, you can assign a value to it using the assignment operator =. The syntax for assigning a value is:

variableName = value;

For instance, to assign the value 25 to the variable age, you would write:

age = 25;

You can also declare and assign a variable in one line:

int age = 25;

Multiple Variable Declarations

You can declare multiple variables of the same type in one line by separating them with commas. For example:

int a = 5, b = 10, c = 15;

In this line, three integer variables a, b, and c are declared and assigned values at the same time.

Tip: Declaring multiple variables can make your code cleaner and easier to read.

Variable Naming Rules

When naming variables, follow these rules:

  • Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
  • They can contain letters, digits (0-9), and underscores.
  • Variable names cannot contain spaces or special characters (e.g., @, #, $, %).
  • Variable names are case-sensitive, meaning age and Age are different variables.

Watch out: Avoid using reserved keywords (such as int, return, or if) as variable names.

Examples of Variable Declarations and Assignments

Here are some examples of declaring and assigning variables:

  1. Float Variable: To declare a float variable named price and assign it a value of 99.99:
float price = 99.99;
  1. Character Variable: To declare a character variable named grade and assign it the value 'A':
char grade = 'A';
  1. Boolean Variable: To declare a boolean variable named isPassed and assign it the value true:
bool isPassed = true;

Changing Variable Values

Once a variable has been assigned a value, you can change its value later in the program. For example:

age = 30;

This line changes the value of age from 25 to 30.

Tip: Ensure that the new value is of the same type as the variable. For example, you cannot assign a string value to an integer variable.

Scope of Variables

The scope of a variable refers to the part of the program where the variable is accessible. Variables declared inside a function are local to that function and cannot be accessed outside it. For example:

void myFunction() {
int localVar = 10;
}
// localVar is not accessible here

In this example, localVar is only accessible within myFunction.

Watch out: If you try to access a local variable outside its function, you will get an error.

Summary

  • A variable is a named storage location in memory.
  • Declare a variable using the syntax type variableName;
  • Assign a value to a variable using the assignment operator =.
  • Follow naming rules for variables.
  • Variables can be changed after their initial assignment.
  • The scope of a variable determines where it can be accessed.

Check your understanding

  1. What is the purpose of a variable in C++?
  2. How do you declare a variable of type double?
  3. What happens if you try to use a local variable outside its function?
  4. Can you assign a string value to an integer variable? Why or why not?