Lecture # 6: Conditional Structures
Lecture # 6: Conditional Structures
Conditional
Structures
Control Structures in C++
• Control structures control the flow of execution
in a program or function.
• There are four kinds of execution flow:
Sequence
• the execution of the program is sequential.
Selection:
• A control structure which chooses alternative to execute.
Repetition:
• A control structure which repeats a group of statements.
Function call :
• A type of statement that moves the control to another block
of code
4-2
Conditions
• A program may choose among alternative
statements by testing the value of key variables.
– e.g., if( your_grade > 60 )
cout<< “you are passed!”;
• Condition is an expression that is either false
(represented by 0) or true (represented by 1).
– e.g., “your_grade > 60” is a condition.
• Conditions may contain relational or equality
operators, and have the following forms.
– variable relational-operator variable (or constant)
– variable equality-operator variable (or constant)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 4-3
Relational Operators
4-4
Operators Used in Conditions
4-5
Examples of Conditions
4-6
Comparing Characters
• We can also compare characters in C using the
relational and equality operators.
Expression Value
4-7
‘if’ Statement
• It is decision making statement.
• if is a keyword in C++ language.
• Used to execute a statement or set of statements
by checking a condition.
• Condition is given as a relational expression.
• If the condition is true, the statement or set of
statements after if statement is executed.
• If the condition is false, the statement or set of
statements after if statement is not executed.
4-8
if statement (continue)
• The syntax of if statement is as follows:
if (condition)
statement;
if (condition)
{
statement 1;
statement 1;
….
statement n;
} 4-9
The if – else Statement
• The if statement is the primary selection control
structure.
• Syntax: if (condition) statement;
else statement;
• An example of two alternatives:
if ( rest_heart_rate > 56 )
cout<<“Keep up your exercise program!\n”;
else
cout<<“Your heart is in excellent health!\n”;
• An example of one alternative:
if ( x != 0.0 )
product = product * x;
4-10
Flowchart
4-11
Flowcharts of if Statements with (a) Two
Alternatives and (b) One Alternative
Decision Decision
4-12
Limitation of simple ‘if’ statements
• If statement is the simplest selection structure
but it is very limited in its use.
• The statement or set of statements are executed
if the condition is true.
• But if the condition is false then nothing
happens.
• Example: a program should display ‘PASS’ if the
student gets 40 or more marks. It should display
‘fail’ if the student gets less than 40 marks.
4-13
Compound condition
• A type of condition in which more than one
condition are evaluated is called compound
condition.
• Used to execute a statement or set of statements
by testing many conditions.
• Example : a program input two numbers and
displays OK if one number is greater than 100
and second number is less than 100.
• Compound condition is executed by using
logical operators
4-14
Logical Operators
4-15
The Truth Table of Logical Operators
Op 1 Op 2 Op 1 && Op2 Op 1 || Op2
nonzero nonzero 1 1
nonzero 0 0 1
0 nonzero 0 1
0 0 0 0
Op 1 ! Op 1
nonzero 0
0 1
4-16
Nested if Statements
• Nested if statement is an if statement with
another if statement as its true task or false
task.
• e.g.,
if (road_status == ‘S’)
if (temp > 0) {
printf(“Wet roads ahead!\n”);
}else{
printf(“Icy roads ahead!\n”);
}
else
printf(“Drive carefully!\n”);
4-17
An Example for the Flowchart of
Nested if Statements
Another if statement
Main if statement
4-18
Multiple-Alternative Decisions
• If there are many alternatives, it is better to use the
syntax of multiple-alternative decision.
• Syntax:
if (condition1)
statement1
else if (condition2)
statement2
…
else if (conditionn)
statementn
else
statemente
4-19
An Example of Multiple-Alternative
Decisions