Conditions 1
Conditions 1
Course
for
Beginners
Conditions
By eng.Manar ELsheref
We will cover these skills
PAGE 2
Decision Making
Students
Teachers
PAGE 3
What is Decision Making?
PAGE 4
if Statement Syntax
if(condition) {
if
Statement
/* Statements to execute if
condition is true */
}
PAGE 5
C++
Relational and
Logical Operators
•Greater than: >
•Less than: < .
•Equal to: ==.
•Not Equal: !=.
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 10
if(condition1)
{
// Executes when condition1 is true
if(condition2)
{ Nested if
// Executes when condition2 is true
}
Statement
} Syntax
In the above syntax, you can see that
we have written second if statement inside
the curly braces { } of first if statement.
If condition1 is true then condition2 will be checked,
if condition2 is also true then the statement inside
the curly braces { } of second if statement will execute.
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.
But what-if we want to do something else if the What is
condition is false. if...else
Here comes the else statement.
We can use the else statement with if statement to
Statement
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 rest of the else if is What is
if...else if
bypassed.
If none of the conditions is true, then the final else statement will be
executed.
Statement
PAGE 17
if(condition)
{
statement;
}
else if(condition)
{ statement; if else if
Statement
} else if(condition)
{ statement; } ...
else { 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 What is
case’s value, then the statements following that case are
executed. switch...case
A break statement ends the switch case.
The optional default case is used when the value the test
Statement
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 C++ Switch
break;
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
The rules
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.
apply
to switch
•Duplicate case values are not allowed.
•When a break statement is reached, the switch terminates, and the flow of control jumps to the
case statements:
next line following the switch statement.
•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.
•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";
Example
break;
case 4:
cout<<"Wednesday";
break;
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':
Example
cout<<"Remark:\tExcellent!";
break;
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