Week 9 - Logical and Relational Operators
Week 9 - Logical and Relational Operators
STATEMENTS IN
C++ PROGRAMMING
BOOLEAN EXPRESSION
• Boolean Algebra
• Developed by George Boole
• IF – ELSE Statement
• NESTED IF Statement
• SWITCH Statement
SIMPLE IF STATEMENT
if(condition)
{ statement; }
EXAMPLE OF SIMPLE IF
STATEMENT
Sample Code Sample Flowchart
EXAMPLE OF SIMPLE IF
STATEMENT
IF - ELSE STATEMENT
if(condition)
{
statement 1;
}
else
{
statement 2;
}
EXAMPLE OF IF-ELSE
STATEMENT
Sample Code Sample Flowchart
if(Grade>=75)
{
cout<<“ Congratulations, You Passed!”;
}
else
{
cout<<“ You failed, better luck next
time!”;
}
EXAMPLE OF IF - ELSE
STATEMENT
NESTED IF - ELSE STATEMENT
{
if(condition 1)
{ statement 1; }
else
{ statement 2; }
}
else
{ statement 3; }
EXAMPLE OF NESTED IF-ELSE
STATEMENT
Sample Code Sample Flowchart
int ctr = 0;
int Password;
if(Password==“JRUat100”)
{
cout<<“ Login Successfully!”;
}
else
{
cout<<“Login Failed!”;
ctr++;
if (ctr==3){
cout<<“Please Try again later!”;
}
}
SWITCH STATEMENT
case n:
//execute your code
break;
default:
//execute your code
}
EXAMPLE OF SWITCH-CASE
STATEMENT
Sample Code Sample Flowchart
int num = 2;
cout<<"Pls enter number";
cin>>num;
switch(num) {
case 1:
cout<<“You selected 1.”;
break;
case 2:
cout<<“You selected 2”;
break;
default:
cout<<“Pls select a number from 1-2”;
}
THE FOLLOWING RULES APPLY
TO A SWITCH STATEMENT
1. The expression used in a switch statement must
have an integral or enumerated type or a class type in
which the class has a single conversion function to an
integral or enumerated type.
THE FOLLOWING RULES APPLY
TO A SWITCH STATEMENT
2. You can have any number of case statements within a
switch. Each case is followed by the value to be
compared to and a colon.