If Else
If Else
Structure
Relational Operators
• The relational operators are used to specify condition in
Program.
• A relational computer compares two values.
• It produces result as true of false.
• The relational operator are sometimes called the conditional
operator or comparison operator.
• == equal to operator
• != not equal to operator
• < less than operator
• > Greater than operator
• >= Greater than or equal to
• <= Less than or equal to
Relational Operators
• Examples
• 100>50
• 45<90
• 31<=32
• 44>=44
• 1!=5
• 6==6
• X==X
• Y!=Y
Flowchart Symbols
CIS162AD
Input or Output
Begin or End
Processing Decision
4
Branch or Direction of Flow
If Statement
• Syntax
if (Expression)
(statements)
Expression
• If the Expression is true then
• execute statements
if(condition)
{
statement 1;
statement 2;
statement N;
}
• Syntax
• if (condition)
• { statement; }
• if (condition)
• {
Statement1;
Statement2;
StatementN;
• }
if Selection Statement
• Selection statements
• Pseudocode example
• If student’s grade is greater than or equal to 60
Print “Passed”
• If the condition is true
• The print statement executes then the program continues to next statement
• If the condition is false
• The print statement ignored then the program continues
if Selection Statement
• Translation into C++
if ( grade >= 60 )
{ cout << "Passed";}
if (m < n)
++m;
++n;
cout << " m = " << m << " n = " n << endl;
if Selection Statement
• Inputs two number and find if both are equal
• main()
• {
• int a,b;
cin>>a;
cin>>b;
if(a==b)
{
cout<<“both are equal”;
}
• System(“pause”);
• }
The If-Else Statement
• Syntax
if (Expression)
Action1
else
Expression
Action2
• If Expression is true then execute false
true
Action1 otherwise execute Action2
if (v == 0) {
Action1 Action2
cout << "v is 0";
}
else {
cout << "v is not 0";
}
The If-Else Statement
{
cout<<“both are not equal ”;
}
• system(“pause”);
}
if / else if / else Selection
Structures
• The actual syntax for the multiple if / else if / else selection
structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ;
Simple Program Using if /
else if / else
#include <stdio.h>
int main ( )
{
int a , b ;
cout<<"Enter values for a " ;
cin>> a;
cout<<“enter value for b”;
cin>>b ;
Winter Quarter
if ( a < b )
{ cout<<"a is less than\n" ;
}
else if ( a == b )
{ cout<< " a is equal to b\n" ;
}
else Lect
{ cout<<"a is larger than b\n" ; 8
}
} P. 19
Logical Operators
case valuen:
statementn;
break;
default:
statement;
}
The switch Statement
cin>>grade;
//H
switch (grade)
{
case ‘A’:
cout << “Grade is between 90 & 100”;
break;
case ‘B’:
cout << “Grade is between 80 & 89”;
break;
case ‘C’:
cout << “Grade is between 70 & 79”;
break;
case ‘D’:
cout << “Grade is between 60 & 69”;
break;
case ‘E’:
cout << “Grade is between 0 & 59”;
break;
default:
cout << “You entered an invalid grade.”;
}
The switch Statement
* * * * Menu * * * *
1. Nablus
2. Rammallah
3. Tolkarm
4. Jenien
Choose either 1, 2, 3 or 4:
The switch Statement
switch (choice) switch (choice)
{ {
case 1: case 1:
cout << “Nablus”; cout << “Nablus”;
case 2: break;
cout << “Rammallah”; case 2:
case 3: cout <“Rammallah”;
cout << “Tolkarm”; break;
case 4: case 3:
cout << “Jenien”; cout << “Tolkarm”;
default: break;
cout<<“ invalid choice”; case 4:
} cout << “Jenien”;
break;
default:
cout<<“ invalid choice”;
}
The switch Statement
#include<iostream>
Void main ( )
{
int value
cout << “Enter 1- Palestine 2- Egypt 3- USA”;
cin >> value;
switch (value)
{
case 1: cout << “No of population is 5 million”; break;
case 2: cout << “No. of population is 70 million”; break;
case 3: cout << “No. of population is 180 million”; break;
default: cout<<“invalid choice”;
}
}
switch Multiple-Selection
Statement
• switch statement
• Used for multiple selections