Control Structures
Control Structures There are three types of loops in java : For loop While loop Do while loop Here is the general form of traditional for loop, For(initialization; condition; iteration){ //body } www.prolearninghub.com
Control Structures Loop While loop Do-while loop For loop Loops in Java are used to execute the same block of code a specified number of times or while a specified condition is true. While loop Do-while loop For loop Break and Continue www.prolearninghub.com
Control Structures WhileLoop While loop Syntax: The while loop is used when one wants the loop to execute and continue executing while the specified condition is true. Syntax: While loop var intilization while (var<=endvalue) { code to be executed } www.prolearninghub.com
Control Structures WhileLoop While loop The while loop is the most basic loop in Java. while (boolean-expression) { statement1; [...] } While loop www.prolearninghub.com
Control Structures WhileLoop While loop The loop body will continue to execute as long as the looping condition is true. The looping condition is tested upon entry and when the loop body is completed. If the loop body consists of a single statement, the curly braces are not necessary. If the looping condition is false upon entry, the loop body will not be executed While loop www.prolearninghub.com
Control Structures WhileLoop Example While loop int x = 0; while(x<10) { System.out.println(x++); } While loop www.prolearninghub.com
Control Structures WhileLoop Example While loop int x = 0; while(x<10) { System.out.println(++x); } While loop www.prolearninghub.com
Control Structures WhileLoop Example While loop int x = 0; do { System.out.println(x); x = x+1; } while(x<10); While loop www.prolearninghub.com
Control Structures Loop Do-while loop The block of do-while will be executed at least once with any condition. For the next execution, it checks the condition. If it is true, then block will be executed. Do-while loop Syntax: do { statement } while(condition) www.prolearninghub.com
Control Structures Loop Do-while loop The do-while loop is identical to the while loop except that the test is evaluated at the end of the loop. Do-while loop do { statement1; [...] } while (boolean-expression); www.prolearninghub.com
Control Structures Do-while loop Because the looping condition is evaluated at the end of the loop body, the loop body is guaranteed to execute at least once. Do-while loop www.prolearninghub.com
Control Structures Loop Example Do-while loop int count = 0; do { System.out.println(count); } while(count<10); Do-while loop www.prolearninghub.com
Control Structures Loop Example Do-while loop int x = 0; do { System.out.println(x); x = x+1; } while(x<10); Do-while loop www.prolearninghub.com
Control Structures For Loop For loop For loop Is same as while loop. All the three statement are included in same line. Syntax: For loop for(var=startvalue;var<=endvalue;var=var+increment) { code to be executed } www.prolearninghub.com
Control Structures For Loop For loop When the loop first start the initialization portion of the loop is executed. Generally there is an expression that sets the value of the loop control variable. which act as a counter that control the loop .It is important to understand the initialization portion is executed just once. Next, condition is executed. This must be Boolean expression. It usually test the loop control variable against the target value. If it is true then the body of the loop is executed and if false then loop terminated. For loop www.prolearninghub.com
Control Structures For Loop Example For loop public static void main (String args[]) { int t=2,i; for(i=1; i<10; i++ ) { System.out.println(t+ ”*” +i+ “ = ” + (t*i)); } For loop www.prolearninghub.com
Control Structures For Loop Example For loop Output: I am so smart for (int i = 1; i <= 4; i++) { // repeat 4 times System.out.println("I am so smart"); } For loop Output: I am so smart www.prolearninghub.com
Control Structures For Loop Example For loop Output: int highTemp = 5; for (int i = -3; i <= highTemp / 2; i++) { System.out.println(i * 1.8 + 32); } For loop Output: 26.6 28.4 30.2 32.0 33.8 35.6 www.prolearninghub.com
Control Structures Loop Break and Continue Break: The current loop is forced to terminate immediately. Continue: It is used to force the flow of control back to the top of the loop. www.prolearninghub.com
Control Structures Work the same as in C / C++ if/else, for, while, do/while, switch i = 0; while(i < 10) { a += 1; i++; } do { a += i; } while(i < 10); if(a > 3) { a = 3; else { a = 0; for(i = 0; i < 10; i++) { switch(i) { case 1: string = “foo”; case 2: string = “bar”; default: string = “”; www.prolearninghub.com
Control Structures (Contd…) Java supports continue & break keywords also Again, works very similar to C / C++ Switch statements require the condition variable to be of char, byte, short or int type for(i = 0; i < 10: i++) { if(i == 5) continue; a += i; } for(i = 0; i < 10: i++) { a += i; if(a > 100) break; } www.prolearninghub.com
Sample Exercise What are the errors in these loops? Answer : int x = 0; while(x<10); { System.out.println(x++); } Answer : this is an endless loop it is valid if x>10 . www.prolearninghub.com
Sample Exercise What will be the output for this program ? Answer : long[] primes = new long[20]; primes[0] = 2; primes[1] = 3; long[] primes2=primes; System.out.println(primes2[0]); primes2[0]=5; System.out.println(primes[0]); Answer : 2,5 www.prolearninghub.com