Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Bab 3

Program Control Statements


Akhmad Fahruzi

1
There are THREE specific categories of program control
statements

 if
1. Selection Statements
 switch
 for
 while 2. Iteration Statements
 do-while
 continue
 break
3. Jumps Statements
 goto
 return
2
3.1.1 Selection Statements (if ...)
Form of the statements is:
if (condition) statement1;
else statement2;
Where target of the if and else are single statements,
The general form of the if using block of statements is:

if (condition)
{statement_squence1;}
else
{statement_squence2;}

3
Flow Chart of If Statements
Data value

Value is YES
Statements 1
true ?

NO

Statements 2

4
Example : Choose two of the largest numbers

5
A!=0

6
3.1.2 Nested (if ...)
Form of the statements is:
if (condition_1)
{
if (condition_11) statement_11;
if (condition_12) statement_12; .
.
.
if (condition_1n) statement_1n;
}
else statement_2;

Note: C++ allows at least 256 levels of nesting

7
3.1.3 if-else-if ladder
Form of the statements is:
if (condition_1) statement_1;
else if (condition_2) statement_2;
else if (condition_3) statement_3; .
.
.
.
else other_statement;

8
Flow Chart of if-else-if ladder

Data value

NO
Value is
NO
Value is Value is ... NO Other
true ? true ? true ? Statements

YES YES YES

Statements 1 Statements 2 Statements 3

9
Example : Choose of area

10
Result false

11
12
3.2 Selection Statements (switch ...)
Form of the statements is:
switch (expression)
{
case constant1: statement_squence1; break;
case constant2: statement_squence2; break;
case constant3: statement_squence3; break;
.
.
.
default: other_statement_squence;
}

The switch expression must evaluate to either a character or an integer value


(floating-point expressions not allowed).
Standart C++ that a switch can have at least 16,384 case statements
13
There are four important things to know about the
switch statements
1. The switch differs from the if, because the if conditional
expression can be of any type.
2. Constant value only to a character or integer value, this
number of value is not more then one in the one case and
used operator are not allowed.
3. The statement sequence assosiated with each case are not
block.
4. A switch statements is usually more efficient than nested ifs

14
Flow Chart of switch Statements
Data value

Value is YES
Statements 1
true ?

NO

Statements 2

15
Example 1 : Choose of the numbers

16
17
Example 2 : Choose of the character

18
3.3 Iteration Statements (for ...)
The general form of the ‘for’ loop for repeating a single statement is:
for (initialization;expression;operator)
statement;

For repeating a block, the general form is:


for (initialization;expression;operator)
{
statement_sequence;
}

Initialization : The sets the initial value of the loop control variable
Expression : a conditional expression that determines whether or not loop
while repeat.
Operator : used increment or decrement operator
19
Example:
int i;
for (i=0;i<5;i++)
{
cout << i;
}

i value

i++

NO
i>5? Tampilkan i

YES

Selesai
20
21
Single Statement

More than Single Statement

Result False

22
Result True

23
Decrement Operator

24
Faktorial

25
26
3.3.1 The infinite & Time Delay Loop
Infinite loop
for (;;)
{
// statement_squence
}

Time delay loop (example)


for (x=0;x<1000;x++);

27
3.3 Iteration Statements (while...)

The general form of the ‘while’ loop for repeating a single statement is:
while (expression) statement;

For repeating a block, the general form is:


while (expression)
{
statement_sequence;
}

28
29
3.4 Iteration Statements (do-while...)
For repeating a block, the general form is:
do{
statement_sequence;
}while (expression);

30
3.5 Jumps Statements (continue...)
The ‘continue’ statement forces the next itaration of the loop to take place,
skipping code between itself and conditional expression that control the loop.
Example:

31
32
3.6 Jumps Statements (break...)
it is possible to force an immadiate exit from loop, bypassing the loop’s conditional
test by using the ‘break’ statement.
Example:

33
3.7 Jumps Statements (goto...)
The ‘goto’ statement used to divert the process to label a particular operation

34

You might also like