You are on page 1of 31

Decision

Decision Statements

 If statement
 If else statement
 Nested If statement
 Nested If-else statement
If statement

 If statement is a conditional branching


statement used to execute a set of code
when the condition is true. This belongs to
control structures.

 Syntax:
if(expression)
{
Statements
}
If Statement
If condition is true
statements

If Ali’s height is greater then 6 feet


Then
Ali can become a member of the
Basket Ball team
If Statement
If ( condition )
{
statement1 ;
statement2 ;
:
}
If statement
if (age1 > age2)
cout<<“Student 1 is older
than student 2” ;
Relational Operators
< less than
<= less than or equal to
== equal to
>= greater than or equal to
> greater than
!= not equal to
Example

if ((interMarks > 45) && (testMarks >= passMarks))


{
cout << “ Welcome to Virtual University”;
}
Example 1
 #include <iostream.h>
using namespace std;
void main()
{
int a=100;
int b=200;
if (b>a)
{
cout << "B is greater than A";
}
}
Example 2
#include <iostream.h>
main ( )
{
int AmirAge, AmaraAge;
AmirAge = 0;
AmaraAge = 0;

cout<<“Please enter Amir’s age”;


cin >> AmirAge;
cout<<“Please enter Amara’s age”;
cin >> AmaraAge;

if AmirAge > AmaraAge)


{
cout << “\n”<< “Amir’s age is greater then Amara’s age” ;
}
}
Flow Chart Symbols
Process

Flow line

Continuation mark

Decision
Flow Chart for if statement
Entry point for IF block

IF

Condition

Then

Process/
set of statement

Exit point for IF block

Note indentation from left to right


Logical Operators

AND &&
OR ||
Logical Operators
If a is greater than b
AND c is greater than d

In C
if(a > b && c> d)
if(age > 18 || height > 5)
If else statement

 If Else statement is a conditional


branching statement used to execute
a set of code when the condition is
true, otherwise executes the code in
the "else" part. This belongs to Control
Structures.
if-else
if (condition)
{
statement ;
-
-
}
else
{
statement ;
-
-
}
Example
If the student age is greater than
18 or his height is greater than
five feet then put him on the foot
balll team
Else
Put him on the chess team
if-else Entry point for IF-Else block

IF

Condition

Then

Process 1/
Set of statement

Else

Process 2
Set of statement

Exit point for IF block

Note indentation from left to right


Example 1
Code

if (AmirAge > AmaraAge)


{
cout<< “Amir is older than Amara” ;
}
else
{
cout<<“Amir is younger than Amara” ;
}
Example 2
 #include <iostream.h>
using namespace std;
void main()
{
int a=10;
int b=20;
if (a>b)
{
cout << "A is greater than B";
}
else
{
cout << "B is greater than A";
}
Unary Not operator !

 !true = false
 !false = true
Example
If (AmirAge != AmaraAge)
cout << “Amir and Amara’s Ages
are not equal”;
If (!(AmirAge > AmaraAge))

?
Nested If statement

 When an “if statement” is used


within another “if statement” ,it
is called the “nested if
statement”. It is used for multi-
dimensional decision making.
Nested If statement

 Syntax:
if (condition-1)
{
if (condition-2) {
Statements-2 }
Statement-3;
}
Nested If statement
Condition-1 is tested, if
true then control goes to
next “if statement” and
condition-2 is tested.

If condition-2 is true then


statement -2 is tested.
After that control goes to
statement-3 and this
statement is executed

If Condition-2 is false then


statement-2 is skipped and
control goes to statement-3
and will be executed.
Nested if
If (age > 18)
{
If(height > 5)
{
:
}
}
Example
/* Write a program to input three integers. cin>>b;
Compare the three integers to find out if cout<<"Enter third integer:";
they are equal. Nested if statement and
print the message "all values are equal" if cin>>c;
they are equal. Otherwise print the if (a==b)
message "these values are differnt“ */ {
if(a==c)
#include<iostream> cout<<"All values are equal";
using namespace std; //else
main() //cout<<"first condtion is true only";
{ int a,b,c;
}
cout<<"Enter first integer:";
cin>>a;
else
cout<<"Enter 2nd integer:";
cout<<"All values are different";
}
Nested If-else statement

 When an “if-else statement” is


used within another “if-else
statement” ,it is called the
“nested if-else statement”. It is
used for multi-selection.
Nested If-else statement

 Syntax:

if (condition-1)
Statements-1;
else if (condition-2)
Statements-2;
else if (condition-3)
Statements-3;
--------
--------
else Statement –n;
Example
/* Make a program that take student grade & tells him else
what grade he/she has achieved?
if(marks>=80) {
Hint:Use Nestd if-else statement. Grade A (90-100),
Grade B (80-89),Grade C(70-79), Grade D (60-69), if cout<<"\nGrade ****B****\n"; }
marks are less than 60 (you should take this courese else
again)*/ if(marks>=70)
#include<iostream>
{
using namespace std;
cout<<"\nGrade ***C***\n";
main()
}
{ int marks=0;
cout<<"Please enter number obtained out of 100 =\n"; else
cin>>marks; if(marks>=60)
if(marks>100||marks<0) {
cout<<"marks should be greater than 0 and less than cout<<"\nGrade **D**\n";
100”;
}
if(marks>=90)
else
{ cout<<"\nGrade *****A*****\n"; }
cout<<"F\t\t\t You must take this
course again";

You might also like