Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 50

TOPIC 3:

CONTROL STRUCTURE
OBJECTIVES

In this chapter you will:


 Learn about control structures
 Examine relational and logical operators
 Explore how to form and evaluate logical (Boolean) expressions
 Discover how to use the selection control structures if, if...else,
and switch in a program
INTRODUCTION

 C++ programs are written using three basic structure:


 Sequence
 A sequence is a series of statement that execute one after another

 Selection
 Selection is used to execute different statement depending on certain
conditions
 if, if-else, and switch

 Repetition
 Repetition is used to repeat statement while certain conditions are met
 for, while, and do-while
INTRODUCTION (CONT.)
BOOLEAN EXPRESSION

 A Boolean is an expression that evaluate to true or false.


 2 types of Boolean expression:
 Simple Boolean expression
 Compound Boolean expression: A combination of two or more Boolean expressions
 Boolean expressions can compare data of any type as long as both parts of
the expression have the same basic data type.
 Boolean expressions use relational operators and logical operators.
BOOLEAN EXPRESSION (CONT.)

 Simple Boolean expression:


 expression1 relational-operator expression2
 Example:
 10 > 5
 ‘a’ != ‘A’

 Compound Boolean expression:


 expression1 logical-operator expression2
 Example:
 (10 > 5) && (10 > 6)
 ( x <= z) || (y <= z)
RELATIONAL OPERATOR
RELATIONAL OPERATOR (CONT.)

 Relational operator can be applied to compare the value of the variables.


 Suppose we have the following declarations:
int num1 = 10;
int num2 = 20;
int num3 = 30;
int num4 = 40;

Expression Value of Expression


num1 > num2 False
num2 <= num3 True
num4 != num3 True
num1 > num4 False
RELATIONAL OPERATOR (CONT.)
 Relational operator can be applied to characters.
RELATIONAL OPERATOR (CONT.)

 Relational operators can be applied to strings


 Strings are compared character by character, starting with the first
character, and following by other characters.
 Suppose we have the following declarations:

string str1 = "Hello";


string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";
string str5 = "Big";
RELATIONAL OPERATOR (CONT.)
RELATIONAL OPERATOR (CONT.)
RELATIONAL OPERATOR (CONT.)
LOGICAL OPERATOR

 Logical (Boolean) operators enable you to combine logical expressions


 C++ has three standard of logical operators:

 Logical operators take logical values as operands and yield logical values
as results
 Putting ! in front of a logical expression reverses its value
LOGICAL OPERATOR (CONT.)
LOGICAL OPERATOR (CONT.)
LOGICAL OPERATOR (CONT.)
LOGICAL OPERATOR (CONT.)
LOGICAL OPERATOR (CONT.)

found = 1
flag = 0
Int num=1,
a=5, b=8,
n=20
Double
x=5.2, y=3.4
Char ch=‘B’
LOGICAL OPERATOR (CONT.)

found = 1
flag = 0
Int num=1,
a=5, b=8,
n=20
Double
x=5.2, y=3.4
Char ch=‘B’
LOGICAL OPERATOR (CONT.)
LOGICAL OPERATOR (CONT.)

 Logical expressions can be unpredictable


 The following expression appears to represent a comparison of 0, num,
and 10:
0 <= num <=10
 It always evaluates true because 0 <= num evaluates to either 0 or 1,
and 0 <= 10 is true and 1 <= 10 is true
 A correct way to write this expression is:

(0 <= num) && (num <= 10)


PRECEDENCE OF OPERATOR

 Relational and logical operators are evaluated from left to right


 The associativity is left to right
 Parentheses can override precedence
SHORT – CIRCUIT EVALUATION
 C++ uses short circuit evaluation of logical expressions involving && and ||
 Logical expression with && and || are evaluated left to right and evaluation
stops as soon as the value of the expression is known
 Example:

int age = 25;


int height = 70;
(age > 50 ) && (height > 60)
* Evaluation can stop now because result of && is only true when both
side are true. It is already determined that the entire expression will
be false
SELECTION STRUCTURE

 C++ provides three of selection structure in the form of statements:


 The if selection statement
 Either performs (selects) an action, if a condition is true or skips the
action if the condition is false
 The if…else selection statement
 Performs an action if a condition is true and perform a different
action if the condition is false
 The switch selection statement
 Allow selection among many actions depending on the value of
variable or expression
SELECTION STRUCTURE (CONT.)
ONE -WAY if SELECTION

 Used in a one-way selection


 The syntax of one-way selection is:
if (expression)
statement

 Statement is executed if the value of the expression is true


 Statement is bypassed if the value is false; program goes to the next statement
ONE -WAY if SELECTION (CONT.)
ONE -WAY if SELECTION (CONT.)

 Example:
ONE -WAY if SELECTION (CONT.)
EXAMPLE OF ONE WAY if SELECTION

Example Output 1:

Example Output 2:
TWO -WAY if…else SELECTION

 Two-way selection takes the form:


if (expression)
statement1;

else
statement2;

 If expression is true, statement1 is executed otherwise statement2 is executed


 statement1 and statement2 are any C++ statements
 else is a reserved word
TWO -WAY if…else SELECTION (CONT.)
TWO -WAY if…else SELECTION (CONT.)

 If the statements are more than one, you have to use { } to make them as
the compound statements.
 The selection takes the form:

if (expression)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
TWO -WAY if…else SELECTION (CONT.)
EXAMPLE OF TWO -WAY if…else
SELECTION
NESTED IF

 Multiple selection
 Nesting: one control statement in another
 The rule : Pairing and else with an if
 An else is associated with the most recent if that has not been
paired with an else
NESTED IF (CONT.)
NESTED IF (CONT.)
NESTED IF (CONT.)
EXAMPLE OF NESTED if
EXERCISES

 Write the selection structure based on the following table:

Room Type Room Rate (RM)


Standard 180
Deluxe 250
Supreme 320
EXERCISES (CONT.)

 Write the selection structure based on the following table:

Income (RM) Status


<=3000 Lower Income
3000 < Income <= Middle Income
5000
(else) Supreme Higher Income
switch STRUCTURE

 switch structure: alternate to if-else


 switch expression is evaluated first
 Value of the expression determines which corresponding action is taken
 Expression is sometimes called the selector
 Expression value can be only integral
 A particular case value should appear only once
switch STRUCTURE (CONT.)
switch STRUCTURE (CONT.)
 General syntax for switch structure:
switch STRUCTURE (CONT.)

 One or more statements may follow a case label

 Braces are not needed to turn multiple statements into a single


compound statement
 The break statement may or may not appear after each
statement
 switch, case, break, and default are reserved words
switch STRUCTURE (CONT.)
Rules :
 When value of the expression is matched against a case value,
 Statements execute until break statement is found or the end of
switch structure is reached
 If value of the expression does not match any of the case values
 Statements following the default label execute. If no
default label, and if no match, the entire switch statement
is skipped
 A break statement causes an immediate exit from the switch
structure
EXAMPLE OF switch STATEMENT
EXERCISE

 Based on the following table, design a switch selection structure to


display the type of room and room rate.

Choice Room Type Room Rate (RM)


S Standard 180
D Deluxe 250
P Supreme 320

You might also like