Conditions
Conditions
Course
for
Beginners
Conditions
By eng.Manar ELsheref
We will cover these skills
Types of conditions
Outlines
PAGE 2
Decision Making
Students
Teachers
PAGE 3
What is Decision Making?
PAGE 4
if Statement Syntax
if(condition) {
/* Statements to execute if
if Statement
condition is true */
}
PAGE 5
C++
Relational and
Logical Operators
PAGE 6
C++ program to check if an integer variable's value is greater than 10.
int a=25;
if(a>10) //try < and <= and == and!= Example 1
{
Condition
using
cout<<"Yes "<<a<<" is greater than 10"; Relational operator
}
if(a==10) //try < and <= and!=
{
cout<<"Yes "<<a<<" is greater than
10";
} PAGE 7
int NumOne = 20;
int NumTwo = 18;
if (NumOne > NumTwo)
{ example
cout<<"NumOne is greater than NumTwo ";
}
PAGE 8
check if the sum of 2 integer variable's value is greater than
10.
PAGE 11
In C++ program the if statement alone tells us
that if a condition is true then it will execute a
block of statements and if the condition is false
it won’t. What is
But what-if we want to do something else if the
condition is false.
if...else
Here comes the else statement. Statement
We can use the else statement
with if statement to execute a block of code
when the condition is false.
PAGE 12
PAGE 13
if(condition)
{
/* Executes this block if condition is
true */
} else if else
{
/* Executes this block if condition is Statement
false */
}
In the above syntax inside the brackets ( ) of if statement we will
Syntax
write our condition. If the condition is true then the statements
written within the curly braces { } of if statement will execute
otherwise the statements written within the curly braces {
} of else statement will execute.
PAGE 14
check if an integer variable's value is even or odd number.
#include <iostream>
using namespace std;
int main()
{
int a=15;
if(a%2==0) example
{ cout<<a<<" is an even number";
}
else
{
cout<<a<<" is an odd number";
}
return 0;
}
PAGE 15
check if an integer variable's value is even and is also greater than 20 using nested if statement .
#include <iostream>
using namespace std;
int main()
{
int a=24;
if(a%2==0)
{
if(a>20)
{
cout<<a<<"is an even number and is also greater than 20"; Example
}
else
{
cout<<a<<" is an even number but not greater than 20";
}
} else
{
cout<<a<<" is an odd number";
}
return 0;
} PAGE 16
Here, a user can decide among multiple options.
The if statements in C++ program are executed from the top
down.
As soon as one of the conditions controlling the if or else if is
true, the statement associated with them is executed, and the What is
rest of the else if is bypassed.
If none of the conditions is true, then the final else statement will
if...else if
be executed. Statement
PAGE 17
if(condition)
{
statement;
}
else if(condition)
{ statement;
} else if(condition)
{ statement; } ...
if else if
else { statement; } Statement
Syntax
In the above syntax, you can see that we have started
with if statement
and then we are using else if statement.
The ... means that we can write more else
if statements if requires to check multiple options.
If none of the conditions written inside if statements are true,
then the final else statement will be executed.
Here else statement is optional.
PAGE 18
Example
C++ program to check if an integer variable's value is equal
to 10 or 15 or 20 using else if statement.
#include <iostream>
using namespace std;
int main()
{
int a=20;
if(a==10)
{
cout<<"value of a is 10";
}
else if(a==15)
{ cout<<"value of a is 15";
}
else if(a==20) {
cout<<"value of a is 20";}
}
else
{ cout<<"value of a is different"; }
return 0;
} PAGE 19
In C++ program a switch statement is used to compare
an expression’s output value from a list of values, where
each value is a case.
When the expression’s output value is equal to one of
the case’s value, then the statements following
What is
that case are executed. switch...case
A break statement ends the switch case.
The optional default case is used when the value the
Statement
test expression does not match with any of the case’s
values.
PAGE 20
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
C++ Switch
default:
// code block
}
This is how it works:
•The switch expression is evaluated once
•The value of the expression is compared with the values of each case
•If there is a match, the associated block of code is executed
PAGE 21
•The expression's output value statement must be primitive data types.
•You can have any number of case statements within a switch. Each case is followed by a
value and a colon :
•The value of a case must be a constant value and it must be of the same data type as
the output value of the test expression in the switch statement.
•When the value being switched on is equal to a case's value, then the statements
following that case will execute until a break statement is reached.
The rules
•Duplicate case values are not allowed. apply
•When a break statement is reached, the switch terminates, and the flow of control jumps
to the next line following the switch statement. to switch
•Every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
case statements:
•A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases is
true. No break is needed in the default case.
•Nesting of switch statements are allowed, which means you can have switch statements
inside another switch.
•However nested switch statements should be avoided as it makes the program more
complex and less readable.
PAGE 22
int d;
cout<<"Enter day number: ";
cin>>d;
switch(d)
{ case 1:
cout<<"Sunday";
break;
case 2:
cout<<"Monday";
break;
case 3:
cout<<"Tuesday";
break;
case 4:
cout<<"Wednesday";
break;
Example
case 5:
cout<<"Thursday";
break;
case 6:
cout<<"Friday";
break;
case 7:
cout<<"Saturday";
break;
default :
cout<<"Invalid Number"; PAGE 23
}
C++ program to print remark according to the grade obtained.
char grade='B';
cout<<“Enter your Grade";
Cin>>grade;
switch(g)
{
case 'A':
cout<<"Remark:\tExcellent!";
break;
Example
case 'B':
case 'C':
cout<<"Remark:\tWell Done";
break;
case 'D':
cout<<"Remark:\tFail"; break;
default :
cout<<"Invalid Grade";
}
PAGE 24
Char Sign;
Cout<<“enter first num”<<endl;
Cin>>NumOne;
Cout<<“enter the sign + - * /”<<endl;
Cin>>sign;
Cout<<“enter second num”<<endl;
Cin>>NumTwo;
switch(sign)
{
case ‘+’:
cout<<"Addition="<<NumOne+NumTwo;
break;
case ‘-’:
cout<<"Subtraction="<<NumOne-NumTwo;
calculator
break;
case ‘*’:
cout<<"Multiplication="<< NumOne*NumTwo;
break;
case ‘/’:
cout<<"Division="<<NumOne/NumTwo;
break;
default :
cout<<"Invalid Choice";
} PAGE 25
End of Session
Meet you
in
the Next Session
PAGE 26