Lecture#07 (1)
Lecture#07 (1)
Cs-111
(4 CrH = 3 Theory 1 Lab)
Lecture No. 07
if (expression-1)
statement1;
else
if (expression-2)
statement2;
else
statement3;
Chain if Statement (The if-else Nested within the else Part)
if (expression-1)
statement1;
else if (expression-2)
statement2;
else
statement3;
Selection Statements
Consider the following statements:
• if today is Thursday I have test today
• If you will run fast, u will catch the bus
• if x is dividable by 2 then x is even number
if Statement
(Single Selection)
• If statement is used in c++ for selection purposes
• Structure of if statement is:
True(1) OR False(0)
if(condition)
statement;
Example
• int main(){
• int num1,num2;
• cout<<"Enter two integers-->:";
• cin>>num1>>num2;
• if(num1==num2)
• cout<<num1<<" is equal to "<<num2<<endl;
• if(num1!=num2)
• cout<<num1<<" is not equal to "<<num2 <<endl;
• if(num1<num2)
• cout<<num1<<" is less than "<<num2 <<endl;
• if(num1>num2)
• cout<<num1<<" is greater "<<num2 <<endl;
• return 0;
• }
int main(){
int num1,num2;
cout<<"Enter two integers-->:";
cin>>num1>>num2;
if(num1==num2)
cout<<num1<<" is equal to
"<<num2<<endl;
if(num1!=num2)
cout<<num1<<" is not equal to "<<num2
<<endl;
if(num1<num2)
cout<<num1<<" is less than "<<num2
<<endl;
if(num1>num2)
cout<<num1<<" is greater "<<num2
<<endl;
return 0;
int main(){
int num1,num2;
cout<<"Enter two integers-->:";
cin>>num1>>num2;
if(num1==num2)
cout<<num1<<" is equal to
"<<num2<<endl;
if(num1!=num2)
cout<<num1<<" is not equal to "<<num2
<<endl;
if(num1<num2)
cout<<num1<<" is less than "<<num2
<<endl;
if(num1>num2)
cout<<num1<<" is greater "<<num2
<<endl;
return 0;
Problem