Вы находитесь на странице: 1из 14

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
! Logical NOT
&& Logical AND
|| Logical OR

1
CSC128
Mahfudzah Othman
UiTM Perlis

(i) Logical NOT (!)

 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 T
T F

C++ Truth Table:

X !X
Zero 1
Non 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 F
F T F
T F F
T T T

C++ Truth Table:

X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1

2
CSC128
Mahfudzah Othman
UiTM Perlis

(iii)Logical OR (||)

 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 F
F T T
T F T
T T T

C++ Truth Table:

X Y X || Y
0 0 0
0 1 1
1 0 1
1 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

3
CSC128
Mahfudzah Othman
UiTM Perlis

Example 1:

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

4
CSC128
Mahfudzah Othman
UiTM Perlis

SELECTION STRUCTURE

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:

TRUE
condition Statement 1

FALSE

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

Algorithm:

Pseudocode:

1. Start
2. Input grade
3. If grade>50, print pass.
4. End

5
CSC128
Mahfudzah Othman
UiTM Perlis

Flowchart:
Start

Input grade

TRUE
grade>50
Print pass

FALSE

End

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.

6
CSC128
Mahfudzah Othman
UiTM Perlis

Syntax: if (expression)
Statement 1;
else
Statement 2;

Flowchart:

TRUE
expression Statement 1

FALSE

Statement 2

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.

7
CSC128
Mahfudzah Othman
UiTM Perlis

Algorithm:

Pseudocode:

1. Start
2. 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:
Start

Input no1, no2

TRUE
If
(no1>no2) Number 1 is bigger
than Number 2

FALSE

Number 2 is bigger
than Number 1

End

8
CSC128
Mahfudzah Othman
UiTM Perlis

Programming:

void main()
{
int no1, no2;

cout<<”Please enter two integers:”;


cin>>no1>>no2;

if (no1>no2)
cout<<”Number 1 is bigger than Number 2”;
else
cout<<”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;

9
CSC128
Mahfudzah Othman
UiTM Perlis

Flowchart:

TRUE expression TRUE


expression 2 Statement 1
1

FALSE
FALSE
Statement 2

Statement 3

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. Start
2. 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”.
else
print “Number 2 is bigger than number 1”
4. End

10
CSC128
Mahfudzah Othman
UiTM Perlis

Flowchart:
Start

Input no1, no2

TRUE FALSE
If If
(no1>=no2) (no1>no2) These numbers are
equal

FALSE
TRUE

Number 2 is bigger Number 1 is bigger


than Number 1 than Number 2

End

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”;
}

11
CSC128
Mahfudzah Othman
UiTM Perlis

Compound Statements

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; compound
statement
x = y;
y = temp;
}
cout<<x<<” ”<<y<<endl;
}

Run: Enter two integers: 66 44


44 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;
}

12
CSC128
Mahfudzah Othman
UiTM Perlis

Flowchart:

Multi way
selection

Value 1 Value 2 Value 3

V1 action V2 action V3 action

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.

13
CSC128
Mahfudzah Othman
UiTM Perlis

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

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

Вам также может понравиться