Chapter 3

18
CSC128 Mahfudzah Othman UiTM Perlis CHAPTER 3 SELECTION CONTROL STRUCTURE Boolean Values Beside the arithmetic values we have the Boolean values TRUE (1) or FALSE (0). Relational Operators There are 6 relational operators that support logical relationships. They are all binary operations that accept 2 operands and compare them The result is logical data, that is, it is always TRUE (1) or FALSE (0). OPERATOR MEANING < Less than <= Less than or equal > Greater >= Greater than or equal == Equal != Not equal Simple Boolean Expressions An expression in which two numbers (arithmetic values) are compared using a single relational operator. Each Boolean expression has the Boolean value TRUE or FALSE according to the arithmetic validity of the expression: <arithmetic value> relational operator <arithmetic value> Boolean value Logical Operators C++ has three logical operators for combining logical values and creating new logical values. OPERATOR OPERATION 1

Transcript of Chapter 3

Page 1: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

CHAPTER 3SELECTION CONTROL STRUCTURE

Boolean ValuesBeside the arithmetic values we have the Boolean values TRUE (1) or FALSE (0).

Relational Operators There are 6 relational operators that support logical relationships. They are all binary operations that accept 2 operands and compare them The result is logical data, that is, it is always TRUE (1) or FALSE (0).

OPERATOR MEANING

< Less than<= Less than or equal> Greater

>= Greater than or equal== Equal!= Not equal

Simple Boolean Expressions An expression in which two numbers (arithmetic values) are compared

using a single relational operator. Each Boolean expression has the Boolean value TRUE or FALSE according to the arithmetic validity of the expression:

<arithmetic value> relational operator <arithmetic value> Boolean value

Logical Operators C++ has three logical operators for combining logical values and creating

new logical values.

OPERATOR OPERATION

! Logical NOT&& Logical AND|| Logical OR

(i) Logical NOT (!)

1

Page 2: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

It is a unary operator. It changes a TRUE value (non zero) to FALSE (zero) and FALSE value

(zero) to TRUE (1).

Logical Truth Table:

X !X

F TT F

C++ Truth Table:

X !X

Zero 1Non Zero 0

(ii) Logical AND (&&)

It is a binary operator. There are 4 distinct possible combinations of values in its operands. Result: If both operands are TRUE, so TRUE and FALSE in all other

cases.

Logical Truth Table:

X Y X && Y

F F FF T FT F FT T T

C++ Truth Table:

X Y X && Y

0 0 00 1 01 0 01 1 1

(iii) Logical OR (||)

2

Page 3: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

It is a binary operator. There are 4 distinct possible combinations of values in its operands. Result: If both operands are FALSE, so FALSE and TRUE in all other

cases.

Logical Truth Table:

X Y X || Y

F F FF T TT F TT T T

C++ Truth Table:

X Y X || Y

0 0 00 1 11 0 11 1 1

Logical Priority

Operation Priority

() 1! 2

&& 3|| 4

The priority list for the complex expressions:

Operation Priority

() 1! 2

*, /, % 3+, - 4

<, <=, >, >=, ==, != 5&& 6|| 7

Example 1:

3

Page 4: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

5 && -3 1 (T) 4 || 1 1 (T)5 && 0 0 (F) -5 && 0 0 (F)5 || 0 1 (T) !3 || !0 1 (T)0 || 5 1 (T) !3 || 0 0 (F)!5 && !0 0 (F) -8 || 3 1 (T)!5 && 0 0 (F) 0 || !2 0 (F)5 && !0 1 -7 && !9 0 (F)

Example 2:

If x = 4, y = 0 and z = 2, what is the output for this code?

If (x!= 0)y = 3;

else z = 2;

Run: y =3

SELECTION STRUCTURE

4

Page 5: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

Statements in selection structure are executed based on selected conditions. The condition is formed by the Boolean expression. There are three types of selection structure:

1. One-way selection (if)2. Two-way selection (if…else)3. Multiway selection (nested if)

One-way Selection

Choose or not to choose. If condition is TRUE, statement 1 is executed; otherwise statement 1 is ignored.

Syntax: if (condition)Statement 1;

Flowchart:

Example: Suppose the passing grade of an exam is 50. Develop the algorithm and write the program to solve the problem.

Algorithm:

Pseudocode:

1. Start2. Input grade3. If grade>50, print pass.4. End

Flowchart:

5

condition

Statement 1

grade>50

Start

Input grade

Print pass

End

TRUE

FALSE

TRUE

Page 6: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

Programming:

void main(){

float grade;

cout<<”Please enter grade:”;cin>>grade;

if (grade>50)cout<<”You have passed the exam”;

}

Two-way Selection

The basic decision statement in the computer is the two ways selection. The decision is describe to the computer as a conditional statement that can be answered either TRUE or FALSE.

If the answer is TRUE, one/more action statement are executed. If the answer is FALSE, then the different action/set of actions is executed.

Regardless of which set of actions are executed, the program continues with the next statement after the selection statement.

Syntax: if (expression)Statement 1;

6

FALSE

Page 7: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

elseStatement 2;

Flowchart:

C++ implements two-ways selection with the if…else statement. An if…else statement is a composite statement use to make a decision between two alternatives.

Syntatical rules for if…else:

1. The expression must ended in parentheses.2. No semicolon (;) is needed in for an if…else statement. But, statement 1

and statement 2 in the flowchart above may have a semicolon as required by their type.

3. Both the TRUE and FALSE statements can be any statements.4. Both statement 1 and statement 2 must be one and only one statement.

However, if there are multiple statements, it can be combined into a compound statement through the use of braces { }.

Example: Develop the algorithm and write a program that accepts two integers and determine which integer is bigger between them.

Algorithm:

7

expression

Statement 2

Statement 1

FALSE

TRUE

Page 8: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

Pseudocode:

1. Start2. Input two integers; no1 and no2.3. If (no1>no2)

print “Number 1 is bigger then Number 2”else

print “Number 2 is bigger than Number 1”.4. End

Flowchart:

Programming:

8

If (no1>no

2)

Start

Input no1, no2

Number 1 is bigger than Number 2

End

TRUE

FALSE

Number 2 is bigger than Number 1

Page 9: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

void main(){

int no1, no2;

cout<<”Please enter two integers:”;cin>>no1>>no2;

if (no1>no2)cout<<”Number 1 is bigger than Number 2”;

elsecout<<”Number 2 is bigger than Number 1”;

}

Multiway Selection

For the if…else, the statement may be any statement, including another if…else.

When an if…else is included within an if…else, it is known as a nested if.

There is no limit as to how many levels can be nested, but if there are more than three, they can become difficult to read.

Syntax: if (expression 1) If (expression 2)

Statement 1; else

Statement 2; else

Statement 3;

Flowchart:

9

Page 10: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

Example: Develop the algorithm and write a program that accepts two integers and determine whether the integers are equal or one of them is smaller and bigger than the other.

Algorithm:

Pseudocode:

1. Start2. Input two integers; no1 and no2.3. If (no1>=no2)

If (no1>no2)print “Number 1 is bigger than Number 2”

else print “These numbers are equal”.

elseprint “Number 2 is bigger than number 1”

4. End

Flowchart:

10

expression 1

Statement 2

Statement 1

FALSE

TRUE expression 2

TRUE

FALSE

Statement 3

TRUE

If (no1>=no

2)

Start

Input no1, no2

End

TRUE

FALSE

Number 2 is bigger than Number 1

If (no1>no2

)

These numbers are equal

Number 1 is bigger than Number 2

FALSE

Page 11: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

Programming:

void main(){

int no1, no2;

cout<<”Please enter two integers:”;cin>>no1>>no2;

if (no1>=no2)if (no1>no2)

cout<<”Number 1 is bigger than Number 2”;else

cout<<”These numbers are equal”;else

cout<<”Number 2 is bigger than Number 1”;}

Compound Statements

11

Page 12: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

A compound statement is a sequence of statements that is treated as single statement. C++ identifies a compound statement by enclosing its sequence of statement in curly braces { }.

Example:

void main (){

int x, y;cout<<”Enter two integers:”;cin>>x>>y;

if (x>y){

int temp = x;x = y;y = temp;

}cout<<x<<” ”<<y<<endl;

}

Run: Enter two integers: 66 4444 66

Switch Statements

Switch is a composite statement used to make a decision between many alternatives. The selection condition must be one of the C++ integral types.

Syntax: switch (expression) {

case constant-1 : statement; statement;

case constant-2 : statement; statement;

case constant-n : statement; statement;

default : statement; statement;

}

Flowchart:

12

compound statement

Page 13: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

The switch expression contains condition that is evaluated. For every possible value that can result from the condition, a separate case constant is defined.

There must be at least one case statement. Each case expression is associated with a constant.

The case expression is followed by a colon (:) and then the statement with which it is associated. There may be one or more statements for each case.

Everything from a case-labeled statement to the next case statement is a sequence.

The default label is a special form of the labeled statement. It is executed whenever none of the previous case values matched the value in the switch expression. Note, however, that default is not compulsory.

If you do not provide a default, the computer will simply continue with the statement after closing brace in the switch.

If we want to execute only one of the cases, we must use break statement. The break statement causes the program to jump out of the switch statement.

Two or more case expression values can belong to the same statement.

Example: Write a program using switch statement that will displays numbers between 1 to 3.

13

Multi way

selection

V2 action V3 actionV1 action

Value 1 Value 2 Value 3

Page 14: Chapter 3

CSC128Mahfudzah Othman

UiTM Perlis

Programming:

void main(){

int no;

cout<<”Enter an integer between 1-3: ”;cin>>no;

switch (no){

case 1 : cout<<”Number: 1”; break;

case 2 : cout<<”Number: 2”; break;

case 3 : cout<<”Number: 3”; break;

default : cout<<”Sorry…please enter 1-3 only”; break;

}}

14