170 likes | 281 Views
Java. Prepared by Gary Langner. LOOPS. Types of Loops. for loop (entrance controlled) do an action for a definite number of times while loop (entrance controlled do an action while a condition is true do…..while (exit controlled) do an action until a condition is true. Boolean Operators.
E N D
Java Prepared by Gary Langner LOOPS
Types of Loops • for loop (entrance controlled) • do an action for a definite number of times • while loop (entrance controlled • do an action while a condition is true • do…..while (exit controlled) • do an action until a condition is true
For Loop (with single statement) For (<Initialization expression>;< termination condition>; <update expression>) for (i = 1; i <= 10; i++) System.out.println(i); No semicolon! 1 <---------------------------Output 2 3 4 5 6 7 8 9 10 No braces “{“ on single statement
Note: the bracket alignment For Loop (with multiple statement) Int amt, num,total1=0, total2=0; for (int j=1; j <= 10; j++) { //begin for loop amt = SavitchIn.readInt(); // or Scanner class num = SavitchIn.readInt(); total1+= amt; //accumulator statement adds up all amt and stores answer in total1 total2+= num; //accumulator statement adds up all num and stores answer in total2 System.out.println( “The number is” + EasyFroamt.format (num,6)); } //end for loop Loop Body Note: the indenting for readability
For Loop (downward counting loop) for (int k = 20; k >= 15; --k) System.out.print( “K = “+EasyFormat.format (k,9);
For Loop (counting by more than one) int sum = 0; //add the EVEN numbers 2-10 for (int j = 2; j <=10; j += 2) sum+ = j; System.out.println(“sum “)
For Loop (style tips) //loop limits can be defined as constants or as variables and //then have assigned values final int LOOPLIMIT = 500; int lcv; //allows reuse in the program for (lcv = 1; lcv <= LOOKLIMIT; lcv++) //loop body OR int loopLimit; loopLimit=SavitchIn.readInt()
While Loop Entrance controlled (pretest) While (boolean expression) loop statement; While <Boolean expression> True Loop Body False Statements after loop
While Loop see next page • Often used for sentinel value (terminal value) • The boolean expression MUST have a value prior to the loop • Loop control variable must change IN THE loop and become TRUE (otherwise infinite loop) power2 = 1; while (power2 < 100) { //begin loop System.out.println( power2); power2*=2; } //end loop No semicolon Multiplies power2 by 2 with each iteration
While Loop (sentinel value) int numscores = 0; int sum = 0; int score; System.out.print( “Enter a score (-999 to quit) ”); int score = SavitchIn.readInt(); //or scanner class while (score != -999) { numscores++; //simple counter (counts by 1’s) sum += score; //adds up all scores and stores in sum System.out.print( “Enter a score (-999 to quit) ”); score=SavitchIn.readInt() //or scanner class } //end while System.out.print(“sum of scores= “) + sum); Double Prompt and input required
While Loop (compound conditions) int a=SavitchIn.readInt(); int b=SavitchIn.readInt(); while ((a > 0) && (b>0)) { statements; } //end while
Do Loop Post test or entrance controled loop Do body of loop While <boolean expression> True False
Do Loop • Used when it is necessary to loop at least once. • The boolean expression MUST have a value before it is used at the end of the loop. • Loop control variable must change IN THE loop and become FALSE (otherwise infinite loop) • Generally used less frequently except for data validation. next pg j=0; do { j+=2; System.out.println( j); } while (j != 5); No semicolon BAD! j must become five for loop to terminate. This is infinite—Must Ctl Alt Del to stop
Nested Loops for (int k = 1; k <=5; ++k) { for (int j = 1; j <=3; ++j) System.out.print(EasyFormat.format((k+j),4)); System.out.println();; } //end for loop 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 output
Loops-writing style • Each loop should have its own level of indenting. • The { and } should start in the same column • Comments should describe what a loop does. (place inside of loop) • Comments should be used at end of loop i.e.. //end of for loop • Isolate loops by skipping a line before and after the loop.
Now on to some programming practice...Go back to the assignments page and download the first looping program. Good Luck!