You are on page 1of 22

Faculty of Computer Science

Introduction to Programming

Lecturer: Lutfullah “Haqnesar”


Introduction to Programming

Chapter 6

Decision Making Statement


Introduction to Programming

Learning outcomes:
 Decision Making Statement
 If statement
 If-else statement
 Nested if else statement
 If else ladder statement
 Switch statement
Decision Making Statement
 Decision making statement are the statements that requires the
programmer to specify a conditions to be tested by the program, along
with a statement to be executed if the condition is true, and another
statements to be executed if the condition is false.
 Some Decision-making statements available in C++ are:
 if statement
 If else statements
 nested if statements
 if-else-if ladder
 switch statements
if Statement
• if statement is the most simple decision-
making statement.
• It is used to decide whether a certain
statement will be executed or not, that is,
if a certain condition is true then a block
of statement is executed otherwise not.
if Statement

Syntax:

If ( condition )
{
// Statements to execute if condition is true
}
if Statement example
// C++ program to display if a number is even
#include <iostream>
using namespace std;

int main () {
int num = 10;
if (num % 2 == 0)
{
cout<<"It is even number";
}
return 0;
}
If-else Statement
• The if statement will execute a block of
statements if a condition is true and if the
condition is false it won’t.
• But what if we want to do something else,
if the condition is false, here comes
the else statement.
• We can use the else statement
with if statement to execute a block of
code when the condition is false.
If-else Statement
Syntax:

if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
If-else Statement example
// C++ program to display if a number is even or odd
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a Number: ";
cin>>num;
if (num % 2 == 0) {
cout<<"It is even number"<<endl;
}
else {
cout<<"It is odd number"<<endl;
} }
Nested-if Statement

• Nested if statements mean an if statement inside another if


statement.
• C++ allow us to nest if statements within if statements, i.e,
we can place an if statement inside another if statement.
Nested-if Statement
Syntax:
if (condition1)
{
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
}
else
{
// Executes when condition1 is false
}
Nested-if Statement example
#include <iostream>
using namespace std;
int main() {
int i = 15;
if (i <100) {
if (i >=10)
{
cout<<"i is between 10 and 100 ";
}
else {
cout<< “ I is smaller than 10”;
}
else {
cout<<"i is greater than 100";
} }
if-else-if ladder
• Here, a user can decide among multiple options using if-else ladder.
• The C++ if statements are executed from the top down.
• As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of the else-if
ladder is bypassed.
• If none of the conditions are true, then the final else statement will be
executed.
if-else-if ladder
if-else-if ladder
Syntax
if (condition) {
statement;
}
else if (condition) {
statement;
}
else if (condition) {
statement;
}
.
.
else {
statement;
}
if-else-if ladder example
#include <iostream> cout<<"C Grade";
using namespace std; }
int main () { else if (num >= 70 && num < 80)
int num; {
cout<<"Enter a number to check grade:"; cout<<"B Grade";
cin>>num; }
if (num <0 || num >100) else if (num >= 80 && num <= 95)
{ {
cout<<"wrong number"; cout<<"A Grade";
} }
else if(num >= 0 && num < 50){ else
cout<<"Fail"; {
} cout<<"A+ Grade";
else if (num >= 50 && num < 70) }
{ }
Switch Statement

 In C++, switch statement is similar to an if-else-if ladder statement.

 It is used for executing one condition from multiple conditions.

 Switch case statement evaluates a given expression and based on the


evaluated value, it executes the statements associated with it.
Switch Statement

Following are some of the rules while using the switch statement:

1. There can be one or more numbers of cases.

2. The values in the case must be unique.

3. Each statement of the case can have a break statement and It is optional.
Switch Statement
Syntax
switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
//code to be executed if all cases are not matched;
break;
}
Switch Statement
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number to check grade:";
cin>>num;
switch (num)
{
case 10: cout<<"It is 10"; break;
case 20: cout<<"It is 20"; break;
case 30: cout<<"It is 30"; break;
default: cout<<"Not 10, 20 or 30"; break;
}
}

You might also like