Unit 5
Unit 5
UNIT 5
OBJECTIVES
General Objective : To Understand the logical structure types and basic instruction
refers to the order of execution of instructions in program.
INPUT
C++ has a set of rich and powerful control structures (statements) that makes it a
popular language. Control structure generally fall into four categories but in this unit
we have to know only three of them, which are:
i. sequence structure
ii. selection structure
iii. repetition or iteration structure
E3062/5/3
The sequence control structure is the simplest of all the structures. The
program instructions are executed one by one, starting from the first
instruction and ending in the last instruction as in the program segment.
Example :
x = 5 (S1)
y = 10 (S2)
Total = x * y (S3)
cout<< “ \n ” << “Total =” <<Total<< “\n”; (S4)
Entry
Exit
S1 S2 S3 S4
a. if Statement
If (expression) statement
E3062/5/4
x >10 No
Yes
Print x
Example 5.1:
If ( gred == ‘A’ )
cout<< “\n PASS ” ;
Example 5.2:
#include<iostream.h>
main()
{
int iVar1;
int iVar2;
iVar1 =20;
iVar2 =10;
20 is greater then 5
b. if – else statement
x >10? No
Yes Increment
x by 1
Print x
Example 5.3:
if ( gred == ‘ E ’ )
cout<< “\n FAIL ” ;
else
cout<< “\n PASS ” ;
Example 5.4:
#include<iostream.h>
main()
{
int iNum;
if (expression_1)
if (expression_2)
if (expression_3)
statement_1;
else
statement_2;
else
statement_3;
else
statement_4;
c. Switch statement
Switch (expression)
{
Case constant_1;
Statement sequence;
break;
case constant_2;
statement sequence;
break;
.
.
default :
statement sequence;
}
Expression
x=?
Example 5.6
#include<iostream.h>
main()
{
char cGrade;
switch (cGrade)
{
case ‘A’
cout << “Minimun marks is 80” <<endl;
break;
case ‘B’:
cout << “Minimun marks is 60” <<endl;
break;
case ‘C’
cout << “Minimun marks is 40” << endl;
break;
case ‘D’:
cout << “Minimun marks is 25” << endl;
break;
default :
cout<< “Marks between 0-24” <<endl;
}
In the example above, A,B,C and D are the possible values that can be
assigned to the variable cGrade.
In each case of the constants, A to D, a statement or sequence of statements
would be executed.
E3062/5/10
You would have noticed that every line has statement under it called “break”.
The break is the only thing that stops a case statement from continuing all the
way down through each case label under it. In fact, if you do not put break in,
the program will keep going down in the case statements, into other case
labels, until it reaches the end of a break.
However, the default part is the codes that would be executed if there were
no matching case of constant’s values.
E3062/5/11
Activity 5A
5.2 Define the basic form of the if statement and if-else statement?
5.3 What are the ‘break’ and ‘default’ statement are needed in the switch
statement ?
E3062/5/12
Feedback To Activity 5A
5.1
i. if statement
ii. if else statement
iii switch statement
5.2
i. if (expression) statement;
5.3
The ‘break’ statement is the only thing that stops a case statement
from continuing.
The default statement is the part of the codes that would be
executed if there were no matching case of constant’s values.
E3062/5/13
INPUT
The while loop repeats the body of the loop as long as the loop condition
holds. The basic form of the while statement is as below:
In this loop, the expression is first evaluated. If it is true (not zero), the
statement (which can be a block is executed; if it is (zero), the statement is
bypassed
E3062/5/14
is Yes
x >10?
No
Print x
x=x+1
Example 5.7
Do
statement
while (expression) ;
E3062/5/15
This do-while loop is quite similar to the while loop except that the
expression is evaluated after the statement is executed. This means the
statement in the do-while will be executed at least once. In the while
statement, the statement will not be executed if the expression is false.
x=x+1
Print x
No is
x >10?
Yes
Example 5.8
In the example 5.8, the program displays the menu and then requests
a selection. If the selection is 1, 2, or 3, the menu is displayed again;
otherwise, the loop is terminated. Note that the loop is repeatedly executed so
long as the selection is 1,2 or 3.
The for loop repeats the body of the loop as long as the loop condition holds.
The basic form of the for loop as below:
The initialization part typically refers to the initial value (if any) given
to a loop variable or counter. But it can also include the initialization
of any other variable. This initialization part is carried out just once at
the beginning of the loop.
The expression part determines whether the loop execution should be
continued. If the expression is zero (false), the for loop is terminated,
if it is not zero (true), the for statement is executed.
The incrementation part typically increments (or decrements) the loop
counter variable. This is done after the for statement is executed. This
part, like the initialization part, can also include the incrementation of
other variables.
E3062/5/17
Example 5.9
#include<iostream.h>
main()
{
int iNum;
The example 5.9, given is a very short program and it is easy for us to
understand the for loop.
First, an integer variable is declared. Then, in the initialization part, the
variable, iNum is set to 1. For the condition checking, iNum is checked to see
whether it is equal to or less than 10. In each cycle of the loop, iNum is
incremented by 1. Once iNum reaches 10, the loop exits. We can see that the
program calculates the square of the first ten natural numbers.
Program output:
1 4 9 16 25 36 49 64 81 100
To run a C++ program, we must first create it, then compile it, then link it with other
modules ( or other compiled programs), and finally run it. You can create a C++
program using the C++ editor. The editor is like a word processor that allows you to
type, edit and save your program. The program you create is called the source
module.
E3062/5/18
After you have created the source program, you compiled it using the C++ compiler.
The compiler translates your C++ instructions into a machine-readable form. The
compiled program is called the object module.
Besides compiling the source module into an object module, the compiler also
generates information necessary for linker. The linker links the object module
generated by the compiler and any other object modules, your program may request
into a final executable module.
Editor Edit
Source module
Compiler Compile
Object module
Run
Result/Output
Feedback To Activity 5B
5.2 State the basic form of the for loop and write the part of program that uses a
for statement?
Feedback To Activity 5B
5.1
i. while loop
ii. do-while loop
iii for loop
5.2
for loop form
5.3
The compiler translates your C++ instructions into a machine-
readable form.
5.4
The linker links the object module generated by the compiler and
any other object modules.
E3062/5/21
KEY FACTS
3. To run a C++ program, we must first create it, then compile it, then link it
with other modules and finally run it.
E3062/5/22
4.
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 5-1
Write the program using selection and repetition structure (if, if else and for) to solve
following problem.
Enter 30 integer values in [1,200]. Find how many of these values fall in the range
1-50, 51-100, 101-150 and 151-200 ?
Question 5-2
Given integer variables x ,y and a character variable ch. Input values for x,y and ch 9
the value for ch must be ‘a’, ‘m’, ‘s’, ‘d’ or ‘r’.
Compute and output
x + y if ch = ‘a’
x *y if ch = ‘m’
x - y if ch = ‘s’
x / y if ch = ‘d’
x % y if ch = ‘r’
E3062/5/23
Feedback To Self-Assessment
** The feedback for this Self-assessment test is based on the student’s creativity as
long as the output meets the criteria required by the question.