Salinan 03 ALPRO
Salinan 03 ALPRO
03
1 Introduction
2
1 Introduction
– break statement
• Used for exiting immediately and
rapidly from certain control structures
– continue statement
• Used for skipping the remainder of
the body of a repetition structure and
proceeding with the next iteration of
the loop
2 Repetition Essentials
• Loop
– Group of instructions computer executes repeatedly while
some condition remains true
• Counter-controlled repetition
– Definite repetition: know how many times loop will execute
– Control variable used to count repetitions
• Sentinel-controlled repetition
– Indefinite repetition
– Used when number of repetitions not known
– Sentinel value indicates "end of data"
4
3 Counter-Controlled Repetition
• Counter-controlled repetition requires
– The name of a control variable (or loop counter)
– The initial value of the control variable
– An increment (or decrement) by which the control
variable is modified each time through the loop
5
3 Counter-Controlled Repetition
6
Outline
fig04_01.c
7
3 Counter-Controlled Repetition
• Condensed code
– C Programmers would make the program more
concise
– Initialize counter to 0
• while ( ++counter <= 10 )
printf( “%d\n, counter );
8
Outline
fig04_02.c
9
Fig. 4.3 | for statement header components.
10
4 for Repetition Statement
• Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
• Example:
for( int counter = 1; counter <= 10; counter++ )
printf( "%d\n", counter );
– Prints the integers from one to ten
11
4 for Repetition Statement
• For loops can usually be rewritten as while loops:
initialization;
while ( loopContinuationTest ) {
statement;
increment;
}
• Initialization and increment
– Can be comma-separated lists
– Example:
for (int i = 0, j = 0; j + i <= 10; j+
+, i++)
printf( "%d\n", j + i );
12
5 for Statement : Notes and Observations
• Arithmetic expressions
– Initialization, loop-continuation, and increment can contain
arithmetic expressions. If x equals 2 and y equals 10
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
• Notes about the for statement:
– "Increment" may be negative (decrement)
– If the loop continuation condition is initially false
• The body of the for statement is not performed
• Control proceeds with the next statement after the for statement
– Control variable
• Often printed or used inside for body, but not necessary
13
Fig. 4.4 | Flowcharting a typical for repetition statement.
14
Outline
fig04_05.c
15
Outline
additional
header
fig04_06.c
(1 of 2 )
16
Outline
fig04_06.c
(2 of 2 )
17
7 switch Multiple-Selection Statement
• switch
– Useful when a variable or expression is tested for all the
values it can assume and different actions are taken
• Format
– Series of case labels and an optional default case
switch ( value ){
– case '1':
actions
– case '2':
actions
– default:
actions
– }
– break; exits from statement
18
Outline
fig04_07.c
(1 of 4 )
fig04_07.c
(2 of 4 )
20
Outline
default case occurs if none of the
cases are matched
fig04_07.c
(3 of 4 )
21
Outline
fig04_07.c
(4 of 4 )
22
Fig. 4.8 | switch multiple-selection statement with breaks.
23
4.8 do…while Repetition Statement
• The do…while repetition statement
– Similar to the while structure
– Condition for repetition only tested after the body
of the loop is performed
• All actions are performed at least once
– Format:
– do {
– statement;
– } while ( condition );
24
4.8 do…while Repetition Statement
• Example (letting counter = 1):
– do {
– printf( "%d ", counter );
– } while (++counter <= 10);
– Prints the integers from 1 to 10
25
Outline
fig04_09.c
26
Fig. 4.10 | Flowcharting the do...while repetition statement.
27
9. break and continue Statements
• break
– Causes immediate exit from a while, for, do…
while or switch statement
– Program execution continues with the first
statement after the structure
– Common uses of the break statement
• Escape early from a loop
• Skip the remainder of a switch statement
28
Outline
fig04_11.c
29
9. break and continue Statements
• continue
– Skips the remaining statements in the body of a
while, for or do…while statement
• Proceeds with the next iteration of the loop
– while and do…while
• Loop-continuation test is evaluated immediately after the
continue statement is executed
– for
• Increment expression is executed, then the loop-
continuation test is evaluated
30
Outline
fig04_12.c
31
10 Logical Operators
• && ( logical AND )
– Returns true if both conditions are true
• || ( logical OR )
– Returns true if either of its conditions are true
• ! ( logical NOT, logical negation )
– Reverses the truth/falsity of its condition
– Unary operator, has one operand
• Useful as conditions in loops
Expression Result
true && false false
true || false true
!false true
32
TERIMA
KASIH
Wibby Aldryani S.ST., M.T., M.Eng.
[email protected] / [email protected]