0% found this document useful (0 votes)
35 views

Salinan 03 ALPRO

This document discusses repetition control structures in programming, including counter-controlled loops (for, while), sentinel-controlled loops (do-while), and flow control statements (break, continue). It introduces the for loop syntax and components. Examples are provided to illustrate the for, while, do-while, break and continue statements. Logical operators like AND, OR, and NOT are also covered. Flowcharts are included to visualize the flow of control in loops. The document is presented as part of a lecture on algorithms and programming.

Uploaded by

rendidwirusti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Salinan 03 ALPRO

This document discusses repetition control structures in programming, including counter-controlled loops (for, while), sentinel-controlled loops (do-while), and flow control statements (break, continue). It introduces the for loop syntax and components. Examples are provided to illustrate the for, while, do-while, break and continue statements. Logical operators like AND, OR, and NOT are also covered. Flowcharts are included to visualize the flow of control in loops. The document is presented as part of a lecture on algorithms and programming.

Uploaded by

rendidwirusti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

ALGORITMA DAN PEMPROGRAMAN

Program Studi Teknik Elektro


Universitas Pertahanan RI
Wibby Aldryani S.ST., M.T., M.Eng.
[email protected] / [email protected]

03
1 Introduction

• This chapter introduces


–Additional repetition control structures
• for
• do…while
–switch multiple selection statement

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

• A condition that tests for the final value of the


control variable (i.e., whether looping should
continue)
• Example:
int counter = 1; // initialization
while ( counter <= 10 ) { // repetition condition
printf( "%d\n", counter );
++counter; // increment
}
– The statement
– int counter = 1;
• Names counter
• Defines it to be an integer
• Reserves space for it in memory
• Sets it to an initial value of 1

6
Outline

fig04_01.c

Definition and assignment are


performed simultaneously

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

for loop begins by setting counter


to 1 and repeats while counter <=
10. Each time the end of the loop is
reached, counter is incremented by
1.

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

Note that number has a different


value each time this statement is
executed

15
Outline
additional
header
fig04_06.c

(1 of 2 )

pow function calculates the value of


the first argument raised to the
power of the second argument

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 )

EOF stands for “end of file;” this character


varies from system to system
switch statement checks each of its
nested cases for a match

break statement makes program skip to end of


19 switch
Outline

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

increments counter then checks if it


is less than or equal to 10

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

break immediately ends for


loop

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

continue skips to end of for


loop and performs next
iteration

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]

You might also like