You are on page 1of 37

Fundamentals Of Computer

Problem Solving
(CSC415)

by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA

1
Selection Structures

Chapter 4

2
Contents

4.1 Introduction
4.2 Selection criteria with Boolean expression
4.3 The if statement
4.4 The if..else statement
4.5 Nested if statement / if..else chain
4.6 The switch statement

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
4.1
Introduction
 A computer can proceed:
 In sequence
 Selectively (branch) - making a choice
 Repetitively (iteratively) - looping
 Some statements are executed only if
certain conditions are met
 A condition is represented by a logical
(Boolean) expression that can be true or
false
 A condition is met if it evaluates to true
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 4
PROGRAMMING
4.1
Introduction (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 5


PROGRAMMING
4.2
Selection criteria with Boolean expression

 Boolean expression is a sequence of


operands and operators that combine to
produce one of the Boolean values, true or
false.
 2 types of Boolean expression :
- simple Boolean expression
- compound Boolean expression

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

Simple Boolean expression :


 is of the form

expression1 relational-operator expression2

 Relational operators:
 Allow comparisons
 Require two operands
 Return 1 if expression is true, 0 otherwise

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 7


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)
 Relational operators can be any of the following
operators :

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)
 Relational operators can be any of the following
operators :

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 9


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

Comparing string Types :


 Relational operators can be applied to strings
 Strings are compared character by character,
starting with the first character
 Suppose we have the following declarations:
string str1 = "Hello";
string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 10
PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 11


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 12


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 13


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)
Compound Boolean expression :
 is formed by combining simple Boolean expressions with
the following logical operator :

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 14


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)
 For example, the expression such as
5 <= x <= 10

can be written as the following compound


Boolean expression:
(5 <= x) && ( x <= 10)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 17


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 19


PROGRAMMING
4.2
Selection criteria with Boolean expression (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING
4.3
The if statement
 Used in a one-way selection
 The syntax :
if (condition)
statement;

 statement is executed if the value of the


condition is true

 statement is bypassed if the value of the condition


is false; program goes to the next statement
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 21
PROGRAMMING
4.3
The if statement (cont.)
 Example :

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 22


PROGRAMMING
4.3
The if statement (cont.)
 Example :
Exercise 21
Exercise 22

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 23


PROGRAMMING
4.4
The if..else statement
 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
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 24
PROGRAMMING
4.4
The if..else statement (cont.)
 Example :
Exercise 23
Exercise 24 :
Consider the following statements :

if final_score >= 60
status = pass
else
status = fail

Write in C++ statement.


November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 25
PROGRAMMING
4.5
The nested if statement
 Multiple selection
 When one control statement is located within
another (nested)
 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
( an else is always belongs to the
closest if)
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 26
PROGRAMMING
4.5
The nested if statement (cont.)

pair

pair

pair

pair

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 27


PROGRAMMING
4.5
The nested if statement (cont.)
 Consider the following statements :

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 28


PROGRAMMING
4.5
The nested if statement (cont.)
 Consider the following case :
Nation’s Air force has asked you to write a program to
label supersonic aircraft as military or civilian. Your
program is to be given the plane’s observed speed in
km/h and its estimated length in meters. For planes
traveling in excess of 1100km/h, you will label those
longer than 52 meters “civilian” and shorter aircraft as
“military”. For planes traveling at slower speeds, you
will issue an “aircraft type unknown” message.

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 29


PROGRAMMING
4.5
The nested if statement (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 30


PROGRAMMING
4.6
The switch statement
 switch structure: alternate to if..else

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 31


PROGRAMMING
4.6
The switch statement
 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
 Its value determines which statement is
selected for execution
 A particular case value should appear only
once
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 32
PROGRAMMING
4.6
The switch statement (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
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 33
PROGRAMMING
4.6
The switch statement (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
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 34
PROGRAMMING
4.6
The switch statement (cont.)
Example :
- Exercise 28
- Exercise 29

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 35


PROGRAMMING
4.6
The switch statement (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 36


PROGRAMMING
4.6
The switch statement (cont.)

November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 37


PROGRAMMING

You might also like