PF-CE Lab02_Flow Control
PF-CE Lab02_Flow Control
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 02
FLOW CONTROL STRUCTURES
______________________________________
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
PF theory class.
In C++, a nested if-else statement is used to create a conditional structure within another
conditional structure. This allows you to check multiple conditions and execute different code
blocks based on the results of these conditions.
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
in itself is 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
}
}
}
2. ternary operator (? :)
Sometimes for simpler conditions ternary operator is used for better readability. For
example, look at the code:
max = a < b ? b : a;
if-else statement allows us to check multiple test expressions or conditions using logical
operators. However, in cases where we need to check the value of a single variable instead
of nested if-elses, use of a switch-case control structure is preferrable.
For example, to test a variable n for various values, switch-case syntax is as follows:
switch( n )
{
case constant1:
//code to be executed if n is equal to constant1
break;
case constant2:
//code to be executed if n is equal to constant2
break;
case constant3:
//code to be executed if n is equal to constant3
break;
.
.
.
default:
//code to be executed if n does not match any
constant
}
When a case constant is found that matches the switch expression, control of the program
passes to the block of code associated with that case.
In the above pseudocode, suppose the value of n is equal to constant2. The compiler will
execute the block of code associated with the case statement until the end of switch block,
or until the break statement is encountered.
The break statement is used to prevent the code running into the next case.
If break statement is not used, all cases after correct case are executed. Sometimes this
may be desirable.
Operator precedence:
In case multiple operators are used in a single statement without parentheses then the order
in which the operations will be performed depends on the precedence of the operators.
The following table illustrates operators in the order of their precedence.
Operators Preceden
ce
!, +, - (unary First
operators)
*, /, % Second
+, - Third
<, <=, >, >= Fourth
==, != Fifth
&& Sixth
|| Seventh
= (assignment operator) Last
Firstly, the unary operation of logical NOT will be performed on the variable x. Since the
value of x is 100 (true in bool), !x will result in false (i.e., 0 in int). All this is because x taken
as a relational operation implicitly typecasts int to bool.
After !, the precedence of > and >= is same so the operator on the left will be evaluated.
Below is given a step-by-step procedure to highlight evaluation of operators in order.
x =
!x > 10 && x >= 90;
x =
!true > 10 && x >= 90;
x =
false > 10 && x >= 90;
x =
0 > 10 && x >= 90;
x =
false && x >= 90;
x =
false && true; // the second term is actually not
evaluated
// since the first term is false (short-
circuit
// evaluation since false && anything is
false)
x = false;
x = 0;
if ( 0 < x < 10 )
First try to find the values (or range of values of variable x) for which the condition would be
true. Then write a sample code and verify your answer.
Verify!
Thus, trying to check a whether a variable f results into the value of after some
computation as:
if ( f == 3.14159 )
would produce unexpected results if the value of f is not exactly 3.14159 or not stored as
3.14159!
Based on the precision requirements of the problem, instead of trying to match for a specific
value, check whether the value falls within a range of acceptable values or not. For example,
expecting the answer to be correct to three decimal places, the following code should be
written to check if f equals the desired value or not.
Exercises
Write a C++ program that takes a year as input and uses the ternary operator to
determine and print whether it is a leap year or not. (Use ternary operator).
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 switch-case.
If the user enters any other char, the program outputs an error message and quits.
Write a program that take marks obtained (out of 75) by the student as an input, and
output the grade obtained by the student. The program should also display an error
message to the user when the marks entered exceeds the total marks i.e. marks
obtained should be less or equal to the 75. (Use else if)
Grade A+ A B C D F
Write a program that will take the Direction from user (E, W, N, S) and return the
coordinates as x and y. The program should initially request your current
coordinates i.e. value of x and y and then will ask you which direction you want to go
to. For east just increment x and for west just decrement x. for north increment y just
and vice versa for south. Use switch statement to implement this.
Write a small program to take 4 values (a, b, c and d) from user and return the
following result.
• If A is greater than B, then program will then check for values of C and D. if
C is greater than D then result= c – d or result = c +d
• If A is smaller than B, then program will again check the value of C and D. If
C is Greater than D, result = 4*C or result = D*3
Do this in single line using conditional operator.
Hint: Use #define in the start of program for setiosflags(ios::left) and use that after every
setw () function if left justification of text is required