CHAPTER 3.
0
PROGRAM CONTROL
3.1 Solve Problems using selection
control structures
Course Learning Outcomes:
CLO1:
Apply program structure and debugging process in
C++ programming language accordingly.
CLO2:
Design programs using appropriate control
structures, arrays, structures, functions and
pointers.
CLO3:
Solve problems using C++ programming language
environment with proper coding style guidelines
and take in the security issues into consideration.
Introduction
• Program control is how a program makes decisions or
organizes its activities.
• Program control typically involves executing particular
code based on the outcome of a prior operation or a user
input.
• C++ provides control structures that serve to specify
what has to be done by our program, when and under
which circumstances (keadaan).
Introduction
• There are two types of control structures :
a) Selection control structure
The selection control structure allows one set of
statements to be executed if a condition is true
and another set of actions to be executed if a
condition is false.
b) Loops control structure
Programmer specifies an action to be
repeated while some condition remains true.
Selection Control Structures
• C++ has four selection statements.
a) if
b) if..else
c) nested if
d) switch..case
Selection Control Structures
(a) If statement
• The if keyword is used to execute a statement
or block only if a condition is fulfilled.
• The form of an if statement is :
if (condition)
statement;
• Condition is the expression that is being evaluated.
• If this condition is true, statement is executed.
• If it is false, statement is ignored (not executed)
and the program continues right after this
conditional structure.
Selection Control Structures
true
condition
statement
false
Selection Control Structures
• Example 1:
if (x == 100)
cout << "x is 100";
• Example 2:
If we want more than a single statement to be executed in
case that the condition is true we can specify a block using
braces { }:
if (x == 100)
{
cout << "x is ";
cout << x;
}
Selection Control Structures
(b) If-else statement
• The form of an if-else statement is:
if (condition)
statement 1;
else
statement 2;
• In this form, condition expression is first
evaluated. If it is evaluated to true, statement 1 is
executed, otherwise statement 2 is executed.
Selection Control Structures
false true
condition
Statement2 Statement1
Selection Control Structures
• Example 1:
#include<iostream.h>
int main( )
{
int number;
cout<<“Please enter one number:”;
cin>>number;
if(number%2==0)
cout<<“This number is even number!”<<endl;
else
cout<< “This number is odd number!”<<endl;
return 0;
}
Selection Control Structures
(c) Nested if statement
• Nested if statements are used to choose between
several alternatives.
• If an IF statement contains an IF statement as
one of its possible branches, the IF statements are
said to be nested.
Selection Control Structures
• The simple form of a nested if statement:
if ( condition1 )
statement1 ;
else if ( condition2 )
statement2 ;
. . .
else if ( condition-n )
statement-n ;
else
statement-e ;
Selection Control Structures
• Another form of a nested if statement:
if (condition1)
if (condition2)
if (condition3)
statement1;
else
statement2;
else
statement3;
else
statement4;
Selection Control Structures
true
Condition 1 Statement 1
false
true
Condition 2 Statement 2
false
.
.
.
true
Condition -n Statement n
false
Statement -e
Selection Control Structures
• Example 1:
#include<iostream.h>
void main()
{
int mark;
cout<<"Enter mark's of student:";
cin>>mark;
if(mark<40)
cout<<"Student's grade = F";
else if(mark<50)
cout<<"Student's grade = E";
else if(mark<60)
cout<<"Student's grade = D";
else if(mark<70)
cout<<"Student's grade = C";
else if(mark<80)
cout<<"Student's grade = B";
else
cout<<"Student's grade = A";
}
Selection Control Structures
#include<iostream.h>
int main( )
{
int first,second;
cout<<“Enter two numbers.”<<endl;
cout<<“First :”;
cin>>first;
cout<<“Second :”;
cin>>second;
if (first>=second)
{
if( (first%second)== 0)
{
if( first == second)
cout<<“They are the same!”<<endl;
else
cout<<“They are evenly divisible!”<<endl;
}
else
cout<<“They are not evenly divisible!”<<endl;
}
else
cout<<“Hey the second one is larger!”<<endl;
return 0;
}
Selection Control Structures
(d) switch…case statement
• Useful when variable or expression is tested for multiple
values
• Consists of a series of case labels and an optional default
case
• break is (almost always) necessary
• Function of break statement
- the "break" statement causes program execution to
"break out" of the nearest enclosing braces.
- is used to alter the flow of control
- causes immediate exit from switch structure
- will skip the remainder of a switch structure
Selection Control Structures
switch (expression)
{
case val1:
statement
break;
case val2:
statement
break;
….
case valn:
statement
break;
default:
statement
break;
}
Selection Control Structures
case a true
case a action(s) break
false
case b true
case b action(s) break
false
.
.
.
case z true
case z action(s) break
false
default action(s)
Selection Control Structures
Switch statement without break Switch statement with break
cout<< “Enter a number : ”; cout<<“Enter a number : ”;
cin>> num; cin>>num;
switch(num) switch(num)
{ {
case 1 : case 1 :
cout <<“WELCOME”; cout <<“WELCOME”;
case 2 : break;
cout <<“BYE”; case 2 :
default : cout <<“BYE”;
cout <<“Wrong Number”; break;
} default :
cout <<“Wrong Number”;
}
Selection Control Structures
Exercise:
Based on the algorithm given, write the complete C++ program.
1. Input height and weight
2. Calculate the BMI using formula:
BMI = weight / (height * height)
3. Check BMI
3.1 If (BMI < 18)
If true, print “you are under weight”
If false, go to step 3.2
3.2 Else if ((BMI >= 18) && (BMI <= 24))
If true, print “you are at a healthy weight”
If false, go to step 3.3
3.3 Else if ((BMI > 24) && (BMI <= 30))
If true, print “you are overweight”
If false, go to step 3.4
3.4 Else
print “you are obese”
4. Print status
#include<iostream.h>
void main()
{
int height, weight, BMI;
cout<<"Enter your height:";
cin>>height;
cout<<"Enter your weight:";
cin>>weight;
if(BMI<18)
cout<<"you are under weight";
else if((BMI >= 18) && (BMI <= 24))
cout<<"you are at a healthy weight";
else if((BMI > 24) && (BMI <= 30))
cout<<"you are overweight";
else
cout<<"you are obese";
}
Selection Control Structures
Exercise:
Based on the program given, convert it to switch-case statement.
#include<iostream.h>
int main()
{
int a,x=0;
cout<<"Please enter the value for a(1 to 4):";
cin>>a;
if (a==1)
x += 6;
else if (a == 2)
x += 10;
else if (a == 3)
x += 16;
else if (a == 4)
x += 34;
cout<<"The value for x is :"<<x;
return 0;
}
#include<iostream.h>
int main()
{
int a,x=0;
cout<<"Please enter the value for a(1 to 4):";
cin>>a;
switch(a)
{
case 1:
x += 6;
break;
case 2:
x += 10;
break;
case 3:
x += 16;
break;
case 4:
x += 34;
break;
}
cout<<"The value for x is :"<<x;
return 0;
}