1 / 17

Java

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.

Download Presentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Prepared by Gary Langner LOOPS

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

  3. Boolean Operators

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

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

  6. For Loop (downward counting loop) for (int k = 20; k >= 15; --k) System.out.print( “K = “+EasyFormat.format (k,9);

  7. 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 “)

  8. 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()

  9. While Loop Entrance controlled (pretest) While (boolean expression) loop statement; While <Boolean expression> True Loop Body False Statements after loop

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

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

  12. While Loop (compound conditions) int a=SavitchIn.readInt(); int b=SavitchIn.readInt(); while ((a > 0) && (b>0)) { statements; } //end while

  13. Do Loop Post test or entrance controled loop Do body of loop While <boolean expression> True False

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

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

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

  17. Now on to some programming practice...Go back to the assignments page and download the first looping program. Good Luck!

More Related