SFTW120-LectureNotes-06

SFTW120-LectureNotes-06 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 6: C: Repetition Structures (Textbook Chapter 5) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 6 Repetition structures s When a program repeats something, this is called iteration , repetition , or looping s In C, there are 3 different repetition structures: 1. while 2. for 3. do-while b Each repetition structure can repeat a single command, or a block of commands, many times b Each repetition structure has a condition to decide if the loop body needs to be repeated again b Each repetition structure is especially suitable for certain kinds of loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 6 Different kinds of loops s Counting loop b The number of loop executions is known in advance s Sentinel-controlled loop b The loop continually inputs data until a special data value is found s Endfile-controlled loop b The loop inputs data from a file until the end of file is reached s Input validation loop b The loop reads a data input value repeatedly until a valid input is read s General conditional loop b The loop executes repeatedly until an end condition is met SFTW120 - Programming Science 4 Lecture Notes 6 Counting loops s Counting loops have a loop control variable to count the number of times they need to be executed s The loop control variable is set to an initial value before the beginning of the loop s The loop contains a loop repetition condition which tests if the loop control variable has reached its final value s Within the loop, the loop control variable is either incremented (increased) or decremented (decreased) s C statements for counting loops: while , for
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 6 Flowchart for counting loops s Before the loop starts, the loop control variable (LCV) is set to an initial value s At the beginning of each loop iteration, the loop repetition condition tests the LCV to see if the loop is finished or not s If the loop is not finished, the loop body is executed, otherwise the loop is exited test LCV loop body set LCV not finished finished Loop SFTW120 - Programming Science 6 Lecture Notes 6 Example of a counting loop with while s Loop controlling code shown in blue : int num, fac, count; printf("Enter a positive integer: "); scanf("%d", &num); fac = 1; count = 1; while (count <= num) { fac = fac * count; count = count + 1; } s This code calculates the factorial of a number; the number of loop executions is known in advance: for n !, the loop needs to execute n times
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 6 What happens in the while loop? Example for num = 5 Before the loop: s fac = 1 s count = 1 Within the loop: s fac and count are updated (see table on the right) After the loop: s fac = 120 s count = 6 120 false 6 120 true 5 24 true 4 6 true 3 2 true 2 1 true 1 fac count <= num count SFTW120 - Programming Science 8 Lecture Notes 6 Format of the while statement while ( loop repetition condition ) statement s Loop repetition condition
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

SFTW120-LectureNotes-06 - SFTW120 Programming Science...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online