Download presentation
Presentation is loading. Please wait.
Published byIsabel Lydia Boone Modified over 8 years ago
1
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3 1435/1436 Place photo here
2
The Objectives and the outcomes The Objectives: To understand the basic repetition structure To understand the differences of repetition structure To understand the application of repetition structure and its benefits The Outcomes: Students should be able to use repetition structure; while, do..while and for. Students should be able to understand the different between all type of repetition structure Students should be able to understand the benefits of using repetition structure
3
Overview When programmers need to execute a block of code several number of times. In general, statements are executed sequentially : The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times:
4
Overview Loop TypeDescription while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. do...while loop Like a while statement, except that it tests the condition at the end of the loop body for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. nested loops You can use one or more loop inside any another while, for or do..while loop.
5
While Loop Initialization of the control variable, which will be used to test the condition, is applied only once. If the condition is true, the statement and the increment are executed, then the whole thing is done again. The statement and the increment are executed repeatedly until the condition becomes false. If the condition starts out false, the while-body will never be executed. Increment is the statement that makes change on the condition, Otherwise, the loop will continue running (Infinite loop).
6
While Loop The syntax of a while loop in C programming language is: while(condition) { statement(s); } Flow Diagram
7
While Loop Example Code:Output: #include int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; } return 0; } value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
8
Do...while Loop A do...while loop is similar to a while loop, Except a do...while loop is guaranteed to execute at least one time, even if the condition was wrong Because testing happened at the end.
9
Do...while Loop The syntax of a do...while loop loop in C programming language is: do { statement(s); } while( condition ); Flow Diagram
10
Do...while Loop Example Code:Output: #include int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { printf("value of a: %d\n", a); a = a + 1; }while( a < 20 ); return 0; } value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
11
For Loop A for loop is a repetition control structure. It is allows you to efficiently write a loop that needs to execute a specific number of times. Here is the flow of control in a for loop: 1.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.
12
For Loop 2.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. 3.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. 4.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). After the condition becomes false, the for loop terminates.
13
For Loop The syntax of a for loop loop in C programming language is: for (Initialize ; condition; increment ) { statement(s); } Flow Diagram
14
For Loop Example Code:Output: #include int main () { /* for loop execution */ for( int a = 10; a < 20; a = a + 1 ) { printf("value of a: %d\n", a); } return 0; } value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.