0% found this document useful (0 votes)
3 views

Lecture4

Uploaded by

elitelife029
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture4

Uploaded by

elitelife029
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming Fundamentals

By: Engr. Raheel Zaman


Variables in C++
In C++, a variable is like a container or a box
used to store data or information. The value
stored in a variable can be changed during the
program.
Imagine you want to store your age, a price, or
a letter. Instead of writing these values
everywhere, you can store them in variables.
Variables have names and types that tell the
computer what kind of data they hold.
Variables in C++

For example:
int age = 25;

int tells the computer that the variable will store


an integer (whole number).
age is the name of the variable.
25 is the value stored in the variable.
Rules for Naming Variables

The variable name should only contain letters,


numbers, and underscores (_).
It cannot start with a number.
You cannot use spaces in variable names.
Variable names are case-sensitive: Age and
age are different.
You cannot use reserved keywords like int, float,
for, etc., as variable names.
✅ Valid: myAge, price1, name_of_book
❌ Invalid: 1age, my age, float
Types of Variables
C++ supports different types of variables
depending on the kind of data they store.
Data Type Description Example
Whole numbers
int int x = 10;
(positive/negative)
float Decimal numbers float price = 5.5;

double Larger decimal numbers double pi = 3.14159;


Single characters
char char grade = 'A';
(letters/symbols)
bool True or false values bool isHappy = true;
Sequence of characters
string string name = "John";
(words)
Declaring and Initializing Variables
You can declare a variable (define its type and
name) and assign a value to it:
Separate Declaration and Initialization:
For Example:
int age; // Declare the variable
age = 25; // Assign value
Declaring and Initializing Variables

Combined Declaration and Initialization

int age = 25; // Declare and initialize in one step


Changing the Value of Variables

You can change the value of a variable later in


the program:
For Example:
int age = 25;
age = 30; // Now the value of 'age' is updated
to 30
Constants
If you don’t want the value of a variable to
change, use the const keyword. This creates a
constant variable:
For Example:
const float pi = 3.14;
pi = 3.14159; // ❌ Error! You cannot change a
constant variable.
Input and Output with Variables
You can take input from the user and display
output using cin and cout.

Example:
#include <iostream>
using namespace std;

int main() {
 int age;
 cout << "Enter your age: ";
 cin >> age; // User inputs a value
 cout << "You are " << age << " years old." <<
endl;
 return 0;
}
Output Example:
Enter your age: 25
You are 25 years old.

You might also like