Download presentation
Presentation is loading. Please wait.
Published byRaymond Lamb Modified over 9 years ago
2
Java Prepared by Gary Langner
3
Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled –do an action while a condition is true u do…..while (exit controlled) –do an action until a condition is true
4
Boolean Operators
5
For Loop (with single statement) For ( ; ; ) for (i = 1; i <= 10; i++) System.out.println(i); 1<---------------------------Output 2 3 4 5 6 7 8 9 10 No semicolon! No braces “{“ on single statement
6
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 Note: the indenting for readability Note: the bracket alignment Loop Body
7
For Loop (downward counting loop) for (int k = 20; k >= 15; --k) System.out.print( “K = “+EasyFormat.format (k,9);
8
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 “)
9
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 int loopLimit; loopLimit=SavitchIn.readInt() OR
10
While Loop While (boolean expression) loop statement; Entrance controlled (pretest) While Statements after loop Loop Body True False
11
While Loop 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 see next page No semicolon Multiplies power2 by 2 with each iteration
12
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
13
While Loop (compound conditions) int a=SavitchIn.readInt(); int b=SavitchIn.readInt(); while ((a > 0) && (b>0)) { statements; } //end while
14
Do Loop Post test or entrance controled loop Do body of loop While <boolean expression> True False
15
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. j=0; do { j+=2; System.out.println( j); } while (j != 5); No semicolon next pg BAD! j must become five for loop to terminate. This is infinite—Must Ctl Alt Del to stop
16
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 234345456567678234345456567678 output
17
Loops-writing style u Each loop should have its own level of indenting. u The { and } should start in the same column u Comments should describe what a loop does. (place inside of loop) u Comments should be used at end of loop i.e.. //end of for loop u Isolate loops by skipping a line before and after the loop.
18
Now on to some programming practice... Go back to the assignments page and download the first looping program. Good Luck!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.