COMPUTER PROGRAMMING SKILLS (4800153-3) CHAPTER 5: REPETITION STRUCTURE Second Term (1437-1438) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah
Table of Contents Objectives & Outline Objectives 1. Use basic problem-solving of selection. 2. Develop algorithms of loop. 3. Use the repetition statements in C. 4. Write a C program using repetition statements: while, for, do...while, and nested loop. Umm al-qura University Chapter 5 (Medical & Administrative) 1/22
Table of Contents Objectives & Outline Outline 1. Repetition structures in C 2. while statement 3. for statement 4. do..while statement 5. Nested loop statement Umm al-qura University Chapter 5 (Medical & Administrative) 2/22
Repetition structures in C Overview Introduction Loops are used to execute a block of code several number of times. A loop statement allows you to specify that an action is to be repeated while some condition remains true. There are three types of loops: for, while, and do..while. Each of them has their specific uses. Umm al-qura University Chapter 5 (Medical & Administrative) 3/22
Repetition structures in C Overview Loop Type while loop do...while loop for loop nested loops Description Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. Like a while statement, except that it tests the condition at the end of the loop body Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. You can use one or more loop inside any another while, for or do... while loop. Umm al-qura University Chapter 5 (Medical & Administrative) 4/22
Repetition structures in C Overview Counter-Controlled Repetition Counter-controlled repetition requires: 1. The name of a control variable (or loop counter). 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop. 4. The condition that tests for the final value of the control variable (i.e., whether looping should continue). Umm al-qura University Chapter 5 (Medical & Administrative) 5/22
while statement Overview Description 1. Initialization of the control variable, which will be used to test the condition, is applied only once. 2. If the condition is true, the statement and the increment are executed, then the whole thing is done again. 3. The statement and the increment are executed repeatedly until the condition becomes false. 4. If the condition starts out false, the while-body will never be executed. 5. Increment is the statement that makes change on the condition, Otherwise, the loop will continue running (Infinite loop). Umm al-qura University Chapter 5 (Medical & Administrative) 6/22
while statement Syntax 1 while ( condition ) { 3 statement ( s ) ; } Listing 1: Syntax of a while loop in C programming language Figure 1: The flowcart of while Statement Umm al-qura University Chapter 5 (Medical & Administrative) 7/22
while statement Example # include < s t d i o. h> 2 i n t main ( void ) { 4 i n t counter = 1 ; // i n i t i a l i z a t i o n while ( counter <= 10 ) { // r e p e t i t i o n condition 6 p r i n t f ( "%d\ t ", counter ) ; // display counter ++counter ; // increment 8 } return 0 ; 10 } Listing 2: A program to print numbers from 1 to 10. Output of Listing.2 1 2 3 4 5 6 7 8 9 10 Umm al-qura University Chapter 5 (Medical & Administrative) 8/22
for statement Syntax f o r ( I n i t i a l i z e ; condition ; increment ) 2 { statement ( s ) ; 4 } Listing 3: Syntax of a for loop in C programming language Umm al-qura University Chapter 5 (Medical & Administrative) 9/22
for statement Overview Definition The Initialize step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). If the condition becomes false, the for loop terminates. Umm al-qura University Chapter 5 (Medical & Administrative) 10/22
for statement Overview Figure 2: The flowcart of for Statement Umm al-qura University Chapter 5 (Medical & Administrative) 11/22
for statement Example # include < s t d i o. h> 2 i n t main ( void ) { 4 i n t counter ; // define counter // i n i t i a l i z a t i o n, r e p e t i t i o n condition, and increment are 6 // a l l included in the f o r statement header. f o r ( counter = 1 ; counter <= 1 0 ; ++counter ) { 8 p r i n t f ( "%d\ t ", counter ) ; } // end f o r 10 return 0 ; } Listing 4: A program to print the numbers from 1 to 10 Output of Listing.4 1 2 3 4 5 6 7 8 9 10 Umm al-qura University Chapter 5 (Medical & Administrative) 12/22
for statement Example Figure 3: for statement header components Umm al-qura University Chapter 5 (Medical & Administrative) 13/22
for statement Example Figure 4: Flowcharting a typical for repetition statement Umm al-qura University Chapter 5 (Medical & Administrative) 14/22
for statement Equivalence of for and while statements 1 f o r ( expression1 ; expression2 ; expression3 ) { statement } Listing 5: General Format of a for Statement expression1 initializes the loop-control variable, expression2 is the loop-continuation condition, expression3 increments the control variable. In most cases, the for statement can be represented with an equivalent while statement as follows: expression1 ; 2 while ( expression2 ) { statement 4 expression3 ; } Listing 6: General Format of a while Statement Umm al-qura University Chapter 5 (Medical & Administrative) 15/22
do...while statement Overview Definition The do... while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition is tested at the beginning of the loop before the body of the loop is performed. The do... while statement tests the loop-continuation condition after the loop body is performed. Therefore, the loop body will be executed at least once. When a do... while terminates, execution continues with the statement after the while clause. do { 2 statement } while ( condition ) ; Listing 7: Syntax of do...while statement Umm al-qura University Chapter 5 (Medical & Administrative) 16/22
do...while statement Overview Figure 5: The flowcart of do...while Statement Umm al-qura University Chapter 5 (Medical & Administrative) 17/22
do...while statement Example 1 # include < s t d i o. h> i n t main ( void ) 3 { i n t counter = 1 ; // i n i t i a l i z e counter 5 do { p r i n t f ( "%d \ t ", counter ) ; // display counter 7 } while ( ++counter <= 10 ) ; // end do... while return 0 ; 9 } Listing 8: Using the do... while repetition statement. Output of Listing.8 1 2 3 4 5 6 7 8 9 10 Umm al-qura University Chapter 5 (Medical & Administrative) 18/22
Nested loop statement Overview Definition C programming allows to use one loop inside another loop. In a nested loop, we can put any type of loop inside any other type of loop. For example, a for loop can be inside a while loop or vice versa. 1 f o r ( i n i t ; condition ; increment ) { f o r ( i n i t ; condition ; increment ) { 3 statement ( s ) ; } 5 statement ( s ) ; } Listing 9: Example of nested loop: A nested for loop statement Umm al-qura University Chapter 5 (Medical & Administrative) 19/22
Nested loop statement Example # include < s t d i o. h> 2 i n t main ( ) { i n t i, j ; 4 f o r ( i = 2 ; i <30; i ++) { f o r ( j = 2 ; j <= ( i / j ) ; j ++) 6 i f (! ( i%j ) ) break ; // i f f a c t o r found, not prime i f ( j > ( i / j ) ) p r i n t f ( "%d i s prime\n", i ) ; 8 } return 0 ; 10 } Listing 10: Use a nested for loop to find the prime numbers from 2 to 30 Umm al-qura University Chapter 5 (Medical & Administrative) 20/22
Nested loop statement Example Output of Listing.10: 2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime Umm al-qura University Chapter 5 (Medical & Administrative) 21/22
References C How to Program, 7th Edition, Paul Deitel, Deitel & Associates, Inc. Harvey Deitel. 2013 Pearson. C Language Tutorials: an easy way of learning C, Ghulam Murtaza Dahar, 2013. Umm al-qura University Chapter 5 (Medical & Administrative) 22/22