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

Variables and Operators-1

Uploaded by

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

Variables and Operators-1

Uploaded by

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

C++ Variables

• Variables are containers for storing data


– Named Storage
– Values can be changed
– Unique names
– Type of variable followed by variable name
– E.g int students=80; int student; student=80;
– Case sensitive student and Student
Variables Examples in real life…
Rules for naming Variables
1. Should not begin with a number etc int 7students;
2. White spaces are not allowed within the variable name
3. A C++ keyword can’t be used as a variable name
4. It’s preferable to give meaningful variable names
5. One word should include all lowercase letters
1. students
2. students_Marks
C++ identifiers
1. All idintifiers’ names should begin with a letter (A to Z or a to z),
$ and _ must be unique
2. After the first letter/character identifiers can have any
combination of character
3. A C++ reserved keyword can’t be used
4. White spaces are not permitted
5. I dentifiers are case sensetaive
1. Studetns and students will be treated two separarte variables
Memory Units
int students = 80; What happens in memory?
• How much memory is consumed to store integer 80
• The most basic unit of data that a computer can store and
process is called Bit
• Every thing is converted to 0s and 1s by the computer
• E.g Welcome to GIKI (101010101100)
• 1s = Current passing or active state
• 0s= Current not passing or passive state
Data Types in C++
• When declaring a vriable
– Defining data type of variable is mandatory
– Tells about the data type the variable is storing
– Determines how much memory will be consumed
– Range of values in variable
1. Primary/ Primitive/ built in data types :
– Integer, character, boolean, floating point, double floating point
2. Derived: Derived from primary data types
– Array, function, pointer, refernce
3. User Defined: Defined by user
– Class, Structure, union
Data Types in C++
Integer Variable:
Int students=15;
4 bytes of memory
Range of unsigned integers = (0---232-1)
Range of signed integers =(-231-----231-1)
Character Variable:
char name=‘A’;
1 bytes of memory
Data Types in C++
Boolean:
bool is_ranning=true;
1 bytes of memory
Values : True or False
Floating Point:
float pi=3.14;
4 bytes of memory
Precision of 7 decimal points
For more precision you need Double
8 bytes of memory
Precision of 15 decimal points
9

Simple Program:
Printing a Line of Text
Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.
Operators:
are fundamental in computer programming and mathematics.
allow you to perform various mathematical operations on numerical values.
E.g +, -, /,%

Arithmetic Operators
C ++ operation Arithmetic Algebraic C ++ expression
operator expression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y

Modulus % r mod s r % s
11
Increment Decrement Operators
Increment Operators:

1. Post-increment (x++): Increments x by 1 after evaluating the expression.

Example: int x = 5; cout << x++ << endl; // outputs 5, x becomes 6

1. Pre-increment (++x): Increments x by 1 before evaluating the expression.

Example: int x = 5; cout << ++x << endl; // outputs 6, x becomes 6

Decrement Operators:

1. Post-decrement (x--): Decrements x by 1 after evaluating the expression.

Example: int x = 5; cout << x-- << endl; // outputs 5, x becomes 4

1. Pre-decrement (--x): Decrements x by 1 before evaluating the expression.

Example: int x = 5; cout << --x << endl; // outputs 4, x becomes 4


12
Decision Making: Equality and Relational
Operators
Standard algebraic C ++ equality Example Meaning of
equality operator or or relational of C ++ C ++ condition
relational operator operator condition

Relational operators
> > x > y x is greater than y
< < x < y x is less than y
 >= x >= y x is greater than or equal to y
 <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y
 != x != y x is not equal to y
13

Logical Operators:

• 1. Logical AND (&&): Evaluates to true if both operands are true.

• 1. Logical OR (||): Evaluates to true if either operand is true.

• 1. Logical NOT (!): Negates the operand.


14
Assignment Operators
1. Simple Assignment (=): Assigns the value of the right operand to the left operand.

Example: x = 5

1. Addition Assignment (+=): Adds the right operand to the left operand.

Example: x += 5 (equivalent to x = x + 5)

1. Subtraction Assignment (-=): Subtracts the right operand from the left operand.

Example: x -= 5 (equivalent to x = x - 5)

1. Multiplication Assignment (*=): Multiplies the left operand by the right operand.

Example: x *= 5 (equivalent to x = x * 5)

1. Division Assignment (/=): Divides the left operand by the right operand.

Example: x /= 5 (equivalent to x = x / 5)

1. Modulus Assignment (%=): Computes the remainder of the left operand divided by the right
operand.
15
Operator Precedence Rules:
1. Parentheses () have highest precedence.
2. Prefix operators (++, --, !, ~, etc.) are evaluated next.
3. Multiplicative operators (*, /, %) are evaluated after prefix operators.
4. Additive operators (+, -) follow multiplicative operators.
5. Comparison operators (==, !=, <, >, etc.) come next.
6. Logical operators (&&, ||) are evaluated after comparison operators.
7. Assignment operators (=, +=, -=, etc.) have lowest precedence.
16
Example 1:
#include <iostream>

int main() {

int y = 10 / 2 + 3 > 5 && 2 + 2 == 4;

std::cout << "Value of y: " << y << std::endl;

return 0;
}

#include <iostream>

int main() {

int y = 10 / 2 + 3 > 5 && 2 + 2 == 4;

std::cout << "Value of y: " << y << std::endl;

return 0;
}
17
Example 2:
#include <iostream>

int main() {

int result = (2 + 3) * 2 > 10 && (7 - 2) * 2 == 10;

std::cout << "Result of expression: " << result << std::endl;

return 0;
}
18
Example 3:

#include <iostream>

int main() {

int result = 16 / 2 + 3 * 2 - 5 % 2 + 11 / 3;

std::cout << "Result of expression: " << result << std::endl;

return 0;

}
The End

You might also like