Professional Documents
Culture Documents
Bab 3 Program Control Statements: Akhmad Fahruzi
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;
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
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;
}
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;
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
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
}
27
3.3 Iteration Statements (while...)
The general form of the ‘while’ loop for repeating a single statement is:
while (expression) statement;
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