0% found this document useful (0 votes)
6 views

ICTLab11_FlowControlStructures

The document is a laboratory manual for a course on flow-control structures in C++, detailing the use of relational and logical operators, as well as control structures like if, if-else, and nested if statements. It includes learning objectives, necessary tools, and several programming tasks for students to complete using DevC++. The manual emphasizes the importance of understanding these concepts as foundational for programming in C++.

Uploaded by

Iqra Inayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ICTLab11_FlowControlStructures

The document is a laboratory manual for a course on flow-control structures in C++, detailing the use of relational and logical operators, as well as control structures like if, if-else, and nested if statements. It includes learning objectives, necessary tools, and several programming tasks for students to complete using DevC++. The manual emphasizes the importance of understanding these concepts as foundational for programming in C++.

Uploaded by

Iqra Inayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Application of ICT Lab

(CL1009)
LABORATORY MANUAL
Fall 2024

LAB: 11
FLOW-CONTROL STRUCTURES

Iqra Inayat 24I-6515 CE-A

STUDENT NAME ROLL NO SEC

______________________________________

LAB ENGINEER'S SIGNATURE & DATE


Muhammad Hammad

MARKS AWARDED: /10


______________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir Version: 1.0
Engr. Azhar Rauf, Engr. Aamer Munir, Date last
Last edited by: Sep 20, 2018
Engr. Sana Saleh, Engr Ifrah Maqsood edited:
Date last
Verified by: Engr. Azhar Rauf Sep 17, 2023
edited:

Lab Learning Objectives:


Flow-Control Structures LAB: 11
In this lab session you will learn about:
1. Relational and Logical Operators
2. if, if-else, if-else-if-else control structure
3. Nested 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.

1. Relational and Logical Operators

To know how values of two variables (usually of the same type) are related to each other,
relational operators are used. Relational operators are:

< Less than


<= Less than or equal to
> Greater than
>= Greater than or equal
to
== Equal to
!= Not equal to

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.

For example, look at the following unusual chunk of code:

int x = 100;
x = x > 10;
cout << x << endl;

Guess what value would be printed on the console window.

Write code in DevC++ and verify!

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:

&& Logical AND


|| Logical OR

ICT LAB NUCES, ISLAMABAD Page 2 of 11


Flow-Control Structures LAB: 11
! Logical Not

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;

Guess what value would be printed on the console window now.

Write code in DevC++ and verify!

How about this one:

int x = 100;
x = !(x > 10) && (x >= 90);
cout << x << endl;

Guess what value would be printed on the console window now.

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.

2. if, if-else, if-else-if-else control structure

ICT LAB NUCES, ISLAMABAD Page 3 of 11


Flow-Control Structures LAB: 11
A C++ code is executed in series one statement (or line) at a time. At times we do not wish
some statements to get executed at all times but only when a certain condition is met during
the running of the code.

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.

ICT LAB NUCES, ISLAMABAD Page 4 of 11


Flow-Control Structures LAB: 11

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
}

3. Nested control structures

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.

ICT LAB NUCES, ISLAMABAD Page 5 of 11


Flow-Control Structures LAB: 11

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
}

Note how the if-else’s are nested within else’s!

Nesting can go as deep as is required. There is no limit on the level of nesting.

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.

Nesting can be done within if body as well, if required.

For example, look at the case below:

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.

ICT LAB NUCES, ISLAMABAD Page 6 of 11


Flow-Control Structures LAB: 11
Practice Tasks:

First make flowchart to design the algorithm of the program and then write C++ code.

ICT LAB NUCES, ISLAMABAD Page 7 of 11


Flow-Control Structures LAB: 11

Task # 1: Code file name lab11_1.cpp


Write a program that can check whether the number entered by user is positive,
negative or zero. Perform this task using if/else statement.

ICT LAB NUCES, ISLAMABAD Page 8 of 11


Flow-Control Structures LAB: 11

Task # 2: Code file name lab11_2.cpp


Write a program to check whether an integer value entered by user is even or odd.
Perform this task using if/else statement.

Task # 3: Code file name lab11_3.cpp


Write a program that can find maximum between three numbers entered by user.
Perform this task using if/else statement.

Task # 4: Code file name lab11_4.cpp

ICT LAB NUCES, ISLAMABAD Page 9 of 11


Flow-Control Structures LAB: 11
Write a program to ask user to enter an operator (either +, -, * or /) and then two
floating point numbers. Then based on which operator is entered by the user, output
the result of the operation. Use if/else.
If the user enters some other char, the program outputs an error message and quits.

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.

ICT LAB NUCES, ISLAMABAD Page 10 of


11
Flow-Control Structures LAB: 11

ICT LAB NUCES, ISLAMABAD Page 11 of


11

You might also like