LOOPING AND BRANCHING Looping q In computer programming
LOOPING AND BRANCHING
Looping q In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number. q Loops execute a block of code a specified number of times, or while a specified condition is true. q A loop lets you write a very simple statement to produce a significantly greater result simply by repetition.
Looping q In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. q A loop statement allows us to execute a statement or group of statements multiple times. q Types of loop: o The While Loop o The For Loop o The Do. . . While Loop
LOOPS => WHILE LOOP q A while loop statement repeatedly executes a target statement as long as a given condition is true. q Syntax: while(condition) { statement(s); } q Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.
LOOPS => WHILE LOOP q Example: // Local variable declaration: $a = 10; // while loop execution while( $a < 20 ) { echo “value of a: ”. $a; echo “ ”; $a++; }
LOOPS => WHILE LOOP q Example: When the above code is executed, it produces the following result: 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
LOOPS => FOR LOOP q A for loop allows a statement to be executed a specified number of times. q Syntax: for ( init; condition; increment /decrement) { statement(s); } q The init 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.
LOOPS => FOR LOOP q 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. q 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. q 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.
LOOPS => FOR LOOP q Example: // for loop execution for( int $a = 10; $a < 20; $a = $a + 1 ) { echo “value of $a: ”. $a; echo “ ”; }
LOOPS => FOR LOOP q Example: When the above code is executed, it produces the following result: 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
- Slides: 10