ICTLab11_FlowControlStructures
ICTLab11_FlowControlStructures
(CL1009)
LABORATORY MANUAL
Fall 2024
LAB: 11
FLOW-CONTROL STRUCTURES
______________________________________
Tools needed:
1. PC running Windows Operating System (OS)
2. DevC++ (installed)
Turn on the PC and log into it using your student account information.
Note:
The information and concepts discussed in this
lab manual are in sufficient detail but are
still not exhaustive. It is assumed that all
these concepts are already taught to you in your
ITC theory class.
To know how values of two variables (usually of the same type) are related to each other,
relational operators are used. Relational operators are:
Relational operators are used in conjunction with flow control structures as explained later in
this lab manual.
For now, understand that a 'relational statement' (or condition) evaluates to a result of type
bool. That is, a relational statement is evaluated either as true or false. The value true is
represented by a 1 and false by 0.
int x = 100;
x = x > 10;
cout << x << endl;
Sometimes multiple relational statements are needed to be grouped together to form one
test expression or condition. To do so, logical operators are used. Logical operators are:
While the true essence of grouping relational statements would be clear in subsequent
sections, look at the following chunk of code:
int x = 100;
x = (x > 10) && (x <= 90);
cout << x << endl;
int x = 100;
x = !(x > 10) && (x >= 90);
cout << x << endl;
In order to resolve relational statements grouped together in logical test expressions, this is
how to proceed.
First resolve each relational statement alone to see whether they are true or false. For
example, in the case above, x > 10 is true but !(true) is false. x >= 90 is true. Now (false
AND true) by rules of Boolean logic is false which is represented by value 0.
If the logical AND operator is replaced with the logical OR operator the overall value will be
true because x >= 90 will be true.
To control the flow of execution of the code, we use some control structures, viz., if-else and
switch-case, which are elaborated below.
Using relational and logical operators we prepare a test expression which is tested at
runtime and can either be true or false. Based on whether the test expression is true or false
certain code sections are executed or skipped.
if statement:
The syntax is:
if( testexpr )
{
//statements to execute if testexpr is true
}
The if statement 'evaluates' the testexpr inside the parentheses which evaluates in a bool
value (i.e., either true represented as a bool value of 1 or false represented as 0).
If the testexpr evaluates to true, the statements inside the body of if are executed. If the
testexpr evaluates to false, the statements inside the body of if are skipped.
if-else statement:
The syntax is:
if( testexpr )
{
//statements to execute if testexpr is true
}
else
{
//statements to execute if testexpr is false
}
If the testexpr evaluates to true, the statements inside the body of if are executed. If the
testexpr evaluates to false, the statements inside the body of else if are executed.
Note that the body of an if or else statement is marked by braces { and }. In case no braces
are used, only one statement following the if statement is included in the body.
For example, in the code below:
if( x > 0 )
x += 10;
y --;
if value of variable x is greater than 0, only then value of x is incremented by 10 but the value
of y will always be decremented by 1 whatever the value of x is, since the statement y--; lies
outside the body of if statement despite the indentation.
The same is true for else.
if-else-if-else statements:
The syntax is:
if( testexpr1 )
{
//statements to execute if testexpr1 is true
}
else if ( testexpr2 )
{
//statements to execute if testexpr2 is true
}
else if ( testexpr3 )
{
//statements to execute if testexpr3 is true
}
else
{
//statements to execute if no condition is true
}
The above form is also called Nested if-else because if you look closely 'else if' is not a
single keyword, rather the already taught keywords else and if separately. This means the if
after else is in the body of else, and the subsequent else is the else that belongs to the last
if.
Our usual way of indenting the body of if and else will make this clear. For example, the
chunk of pseudocode below is exactly the same as the previous one but clearly elaborates
the nesting:
if( testexpr1 )
{
//statements to execute if testexpr1 is true
}
else
if ( testexpr2 )
{
//statements to execute if testexpr2 is true
}
else
if ( testexpr3 )
{
//statements to execute if testexpr3 is true
}
else
{
//statements to execute if no condition is
true
}
If the testexpr1 evaluates to true, the statements inside the body of if are executed. If the
testexpr1 evaluates to false, the single statement inside the body of else is executed, which
is itself an if-else statement, and so on.
if( testexpr1 )
{
//statements to execute if testexpr1 is true
if( testexpr11 )
{
//statements to execute if testexpr11 is true
if( testexpr111 )
{
//statements to execute if testexpr111 is true
}
}
}
To evaluate the concepts discussed in this lab manual write C++ code for as many of the
following tasks as possible.
First make flowchart to design the algorithm of the program and then write C++ code.
Task # 5:
A shop will give a discount of 10% if the cost of purchased quantity is more
than 1000. Ask the user for quantity. Assume, one unit will cost 100.
Make a C++ program to calculate and print total cost for user.