Variable v A named memory location v Memory

Variable: v A named memory location v. Memory location whose content may change during execution

Declaring and initializing the variable: • Syntax Data type var_name; • Example: int x; • Initializing int x=1; • cin is used with >> to gather input cin >> variable;

Operators: • Arithmetic operators • Relational operators • Logical operators

Arithmetic operators: Operators Meaning + Addition - Subtraction * Multiplication / Division % Mod operator

Relational Operators: v. Allow comparisons v. Return 1 if expression is true, 0 otherwise Operator Example Description == x == y x is equal to y > x>y X is greater than y < x<y x is less than y >= x >= y x is greater than equal to y <= x <= y x is less than equal to y != x != y x is not equal to y

Logical Operators: • Logical operators enable you to combine logical expressions Operators Examples && (x<y) && (y<z) || (x<y) || (y<=z) ! ! (y>=z)

Control structures: • Control structures are used to control the flow of execution in a program. • There are : a) Sequence b) Selection c) Repetition

Simple If statement • The syntax of selection is: if (expression) statement • Statement is executed if the value of the expression is true • Example: If student’s grade is greater than or equal to 60 Print “Passed” if( grade>=60) cout<<“passed”;

If else selection • If else selection takes the form: if (expression) statement 1 else statement 2 • If expression is true, statement 1 is executed otherwise statement 2 is executed

Example: • If student’s grade is greater than or equal to 60 print “Passed” Else print “Failed” • C++ code • if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

If else if statement: • If else if statement is used to choose one block of statement from many blocks of statements.

• Syntax If(condition) { block 1; } Else if(condition 2) { block 2; } Else if(condition 3) { block 3; }. . Else { block N; }

Example: If(n>0) cout<<“positive num”; Else if(n<0) cout<<“negative num”; Else cout<<“num is zero”;

Task Write a program that takes marks as input and calculate the grade according to the following criteria. Marks Grade 80 -100 A 60 -79 B 40 -59 C 0 -39 F

Task • Write a program that takes a character as input and display it’s ascii code if character is between ‘A’ to ‘Z’ or ‘a’ to ‘z’ otherwise display to put the right character.
- Slides: 15