Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab5 PROGRAMMING 1 Loop chapter4.

Similar presentations


Presentation on theme: "Lab5 PROGRAMMING 1 Loop chapter4."— Presentation transcript:

1 Lab5 PROGRAMMING 1 Loop chapter4

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

3 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 . Ins.Ebtesam AL-etowi

4 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 if…else true false loop? code block true false Ins.Ebtesam AL-etowi

5 Introduction to Loop 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 False Two kind of loops: Body of the loop Condition True Finite loop The statement in the body will executed number of times, from zero to finite number Infinite loop A loop that’s continuo forever Ins.Ebtesam AL-etowi

6 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. 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

7 The while Loop The syntax for the while loop is as follows: 1
while (loop-continuation-condition) { // Loop body Statement(s); Change; } initialization ; 2 loop body 3 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. Ins.Ebtesam AL-etowi

8 The While loop }// end while int Number = 10;
while ( Number >= 0 ) { System.out.println( "Number is " + Number ); Number--; }// end while System.out.println( "Loop ended"); Initialize the number to 10 . Test whether "Number >= 0". 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. 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

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

10 The do-while Loop The do-while loop is a variation of the while loop.
Its syntax is given below: 1-Initialization: the variable tested in the condition must be initialize to some value. 2-Change: the variable that tested must be change within the body loop. 3-Condition: the condition that 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. 1 do { // Loop body; Statement(s); Change; } while (loop-continuation-condition); initialization ; 2 3 Ins.Ebtesam AL-etowi

11 The do-while Loop int i = 0; do { System.out.println(i); i++;
} while ( i < 10 ); System.out.println(“Loop ended”); 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. If the result of “i<10" is TRUE, return to step (ii) above. Ins.Ebtesam AL-etowi

12 Exercise 2 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"); 4 End Ins.Ebtesam AL-etowi

13 The for Loop In general, the syntax of a for loop is shown as below:
for ( initialization; condition ; Change ) { statements; } 2 3 1 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) Ins.Ebtesam AL-etowi

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

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

16 Break Statement Or used in for, while, do-while loop,
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: int HOUR = 3; switch(HOUR) { case 1: break; case 2: break; case 3: break; case 4: break; default: break; } Or used in for, while, do-while loop, for example: for(int i=0; i<10; i++) { System.out.print(i + " "); if(i == 3) break; } (Which printed: [ ]) Ins.Ebtesam AL-etowi

17 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"); 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

18 Continue Statement 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 var1 =0; var1 < 5 ; var1++)         for(int var2=0 ; var2 < 5 ; var2++)         {                 if(var2 == 2)                         continue;                         System.out.println(“var1:” + var1 + “, var2:”+ var2); } In this example, when var2 becomes2, the rest of the inner for loop body will be skipped. Ins.Ebtesam AL-etowi

19 Continue Statement Example: Outer: 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 var1 =0; var1 < 5 ; var1++) {         for(int var2=0 ; var2 < 5 ; var2++)         {                 if(var2 == 2)                         continue Outer; System.out.println(“var1:” + var1 + “, var2:”+ var2); } } In this example, when var2 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

20 Thank You !


Download ppt "Lab5 PROGRAMMING 1 Loop chapter4."

Similar presentations


Ads by Google