Lab 5 PROGRAMMING 1 Loop chapter 4 LOGO

Lab 5 PROGRAMMING 1 Loop chapter 4 LOGO

BEFORE WE START Your Cell Phone Silent Please Keep Quite Find Your Computer and Switch On 2 Ins. Ebtesam AL-Etowi

content →. Loops & Selection Statements. → introduction to Loop →While, Do-while and For. →Differences between While, Do-while and for. →To implement program control with break and continue. 3 Ins. Ebtesam AL-etowi

Loop &&selection statement Loop & selection statement Selection statements, like if…else, allow for conditional execution of code blocks in a single iteration Loops are structures that control repeated execution of the same block of statements code block false loop? true code block if…else fals e code block 4 Ins. Ebtesam AL-etowi

Introduction to Loop consist of : ØBody of the loop : the code which want to do more than one time ØControl statement (conditions ): A Boolean expression controls the execution of the body of the loop üExecute the loop again if true üTerminate the loop if false Two kind of loops: Fals Condition Finite loop The statement in the body will executed number of times, from zero to finite number True Body of the loop Infinite loop A loop that’s continuo forever 5 Ins. Ebtesam AL-etowi

Loop control structure Entry control loop Exit control loop The control conditions are tested before the start of the loop execution. If the conditions are not satisfied , then the body of the loop will not be executed. 6 The Test is performed at the end of the body of the loop and there fore the body is executed unconditionally for the first time. Ins. Ebtesam AL-etowi

The while Loop The syntax for the while loop is as follows: initialization ; while (loop-continuation-condition) { loop body // Loop body Statement(s); Change; 3 } 1 2 1 -Initialization: the variable tested in the condition must be initialize to some value. 2 -condition: the condition that tested before any iteration. If it false, then the program will not enter to loop and continue to the next statement after the loop 3 -Change: the variable that tested must be change within the body loop. 7 Ins. Ebtesam AL-etowi

The While loop int Number = 10; while ( Number >= 0 ) { System. out. println( "Number is " + Number ); Number--; }// end while System. out. println( "Loop ended"); 8 i. Initialize the number to 10. ii. Test whether "Number >= 0". iii. If it is FALSE, end the loop, and continue with the statements after the loop. In this case, the next statement to be executed would print out the "Loop ended" message. iv. If the result of "Number >= 0" is TRUE, execute the statements in the body of the loop (in this case first print a message, and then decrement the value of "Number" by 1 ), and then return to step (ii) above. Ins. Ebtesam AL-etowi

Exercise 1 What is the output of the following code segment? Total 55 int Counter = 0, total=0; while (Counter<=10) { Total += Counter; Counter++; } // end while System. out. println( "Total " + T); 9 Ins. Ebtesam AL-etowi

The do-while Loop The do-while loop is a variation of the while loop. Its syntax is given below: 1 1 -Initialization: the variable tested in the condition must be initialize to some value. initialization ; do { // Loop body; Statement(s); Change; } while (loop-continuation-condition); 2 -Change: the variable that 2 tested must be change within the body loop. 3 -Condition: the condition that 3 tested After the first iteration. If it false, then the program will not enter to loop and continue to the next statement after the loop. 10 Ins. Ebtesam AL-etowi

The do-while Loop int i = 0; do { System. out. println(i); i++; } while ( i < 10 ); System. out. println(“Loop ended”); i. iii. iv. Initialize the i to 1. Print the i and increment by 1 Test whether “i<10". If it is FALSE, end the loop, and continue with the statements after the loop. In this case, the next statement to be executed would print out the "Loop ended" message. v. If the result of “i<10" is TRUE, return to step (ii) above. 11 Ins. Ebtesam AL-etowi

Exercise 2 What is the output of the following code segment? int Counter = 10, i=2; do{ System. out. println( (i*i) ); Counter++; i++; } while (Counter<=10); // end while System. out. println( "End"); 12 4 End Ins. Ebtesam AL-etowi

The for Loop In general, the syntax of a for loop is shown as below: for ( initialization; condition ; Change ) { statements; } 1 -Initialization: is used to initialize variables used in the loop and is run only once at the start. 2 -Condition: The Boolean expression is checked before each iteration. If it is false the loop will terminate. 3 -Change: The increment/decrement of the loop control variable and is run after each successful iteration(after executed the body) 13 Ins. Ebtesam AL-etowi

Exercise 3 What is the output of the following code segment? for ( int Count = 0; Count < 10; Count++ ) { System. out. print(Count+" "); } 0123456789 14 Ins. Ebtesam AL-etowi

Differences between While, Do-while and For While Initialization; while (condition) { // loop body statement(s); Change; } Use when number of repetitions is not known Do-While Initialization; do { // loop body statement(s); Change; } while (condition); Use when loop body must be executed at least once and number of repetitions is not known 15 For for (initialization ; condition; Change) { // loop body statement(s); } Use when number of repetitions is known (print 100 times Ins. Ebtesam AL-etowi

Break Statement : The break statement in Java has two forms: labeled and unlabeled 1 - Unlabeled break Normally, we knew unlabeled break used in switch-case statement, to break the following execution in the block. Example: Or used in for, while, do-while loop, for example: int HOUR = 3; switch(HOUR) { case 1: break; case 2: break; case 3: break; case 4: break; default: break; } for(int i=0; i<10; i++) { System. out. print(i + " "); if(i == 3) break; } (Which printed: [0 1 2 3 ]) 16 Ins. Ebtesam AL-etowi

Break statement 2 - Labeled break There is another type of break called labeled break. We can use labeled break to break the following execution and transfer the of control back to the labeled statement immediately. Example: int input = 3; validation: { System. out. println("start"); if(input == 4) System. out. println("pass"); if(input < 4) break validation; System. out. println("the input is not equal 4 and not smaller than 4"); if(input > 4) break validation; System. out. println(“end”); } System. out. println("another end"); 17 It should print: start another end The program execution will break from the validation block (declared curly braces) and jump to the end followed by the block. Ins. Ebtesam AL-etowi

Continue Statement : Continue statement is used when we want to skip the rest of the statement in the body of the loop and continue with the next iteration of the loop. There are two forms of continue statement in Java: 1. Unlabeled Continue Statement 2. Labeled Continue Statement 1. Unlabeled Continue Statement This form of statement causes skips the current iteration of innermost for, while or do while loop. Example: for(int var 1 =0; var 1 < 5 ; var 1++) for(int var 2=0 ; var 2 < 5 ; var 2++) { if(var 2 == 2) continue; System. out. println(“var 1: ” + var 1 + “, var 2: ”+ var 2); } 18 In this example, when var 2 becomes 2, the rest of the inner for loop body will be skipped. Ins. Ebtesam AL-etowi

Continue Statement 2. labeled Continue Statement Labeled continue statement skips the current iteration of the loop marked with the specified label. This form is used with nested loops. Example: Outer: for(int var 1 =0; var 1 < 5 ; var 1++) { for(int var 2=0 ; var 2 < 5 ; var 2++) { if(var 2 == 2) continue Outer; System. out. println(“var 1: ” + var 1 + “, var 2: ”+ var 2); } } 19 In this example, when var 2 becomes 2, rest of the statements in body of inner as well outer for loop will be skipped, and next iteration of the Outer loop will be executed. Ins. Ebtesam AL-etowi

LOGO
- Slides: 20