0% found this document useful (0 votes)
57 views22 pages

Lecture # 6: Conditional Structures

Cs 291

Uploaded by

Zaynah Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views22 pages

Lecture # 6: Conditional Structures

Cs 291

Uploaded by

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

Lecture # 6

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

• The relational operators are used to specify


conditions in programs.
• A relational operator compares two values.
• Produces results as true and false.
• Also called as conditional operators or
comparison operators as they test conditions that
is either true or false.

4-4
Operators Used in Conditions

Operator Meaning Type


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

4-5
Examples of Conditions

Operator Condition Meaning


<= x <= 0 x less than or equal
to 0
< Power < MAX_POW Power less than
MAX_POW
== mom_or_dad == ‘M’ mom_or_dad
equal to ‘M’
!= 5!=5 5 not equal to 5

4-6
Comparing Characters
• We can also compare characters in C using the
relational and equality operators.
Expression Value

‘9’ >= ‘0’ 1 (true)

‘a’ < ‘e’ 1 (true)

‘Z’ == ‘z’ 0 (false)

‘a’ <= ‘A’ system dependent

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

• Flowchart is a diagram that shows the step-by-


step execution of a control structure.
– A diamond-shaped box represents a decision.
– A rectangular box represents an assignment
statement or a process.

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

• There are three kinds of logical operators.


– &&: and
– ||: or
– !: not
• Logical expression is an expression which uses
one or more logical operators, e.g.,
– (temperature > 90.0 && humidity > 0.90)
– !(n <= 0 || n >= 100).

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 4-20


The switch Statement
• The switch statement is used to select one of several
alternatives when the selection is based on the value of
a single variable or an expression.
switch (controlling expression) {
case label1:
If the result of this controlling
statement1
break; expression matches label1, execute
case label2: staement1 and then break this switch
statement2 block.
break;

case labeln:
statementn
break; If the result matches none of all
default: labels, execute the default statementd.
statementd;
}
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 4-21
An Example of a switch Statement
with Type char Case Labels
class is a char variable.

Two or more cases can execute


the same statement.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 4-22

You might also like