Vous êtes sur la page 1sur 37

Introduction To

Computer Programming
(CSC425)
by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
2013

Selection Structures
Chapter 4

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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

4.1

Introduction (cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

4.2

Selection criteria with Boolean expression

(cont.)

Relational operators can be any of the following


operators :

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

4.2

Selection criteria with Boolean expression

(cont.)

Relational operators can be any of the following


operators :

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


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
string
string
string
October 1, 2013

str1
str2
str3
str4

=
=
=
=

"Hello";
"Hi";
"Air";
"Bill";

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

10

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

11

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

12

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

13

4.2

Selection criteria with Boolean expression

(cont.)

Compound Boolean expression :

is formed by combining simple Boolean expressions with


the following logical operator :

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

14

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)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

15

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

16

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

17

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

18

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

19

4.2

Selection criteria with Boolean expression

(cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

20

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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

21

4.3

The if statement (cont.)

Example :

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

22

4.3

The if statement (cont.)

Example :
Exercise 21
Exercise 22

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

23

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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

24

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.

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

25

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)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

26

4.5

The nested if statement (cont.)


pair
pair

pair
pair

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

27

4.5

The nested if statement (cont.)

Consider the following statements :

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

28

4.5

The nested if statement (cont.)

Consider the following case :


Nations Air force has asked you to write a program to
label supersonic aircraft as military or civilian. Your
program is to be given the planes 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.

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

29

4.5

The nested if statement (cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

30

4.6
The switch statement

switch structure: alternate to if..else

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

31

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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

32

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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

33

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

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

34

4.6

The switch statement (cont.)


Example :
- Exercise 28
- Exercise 29

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

35

4.6

The switch statement (cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

36

4.6

The switch statement (cont.)

October 1, 2013

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

37