1 / 15

Loops

Loops. Chapter 4. “while” structures. It repeats a set of statements while a condition is true. while ( condition ) { execute these statements ; }. “while” structures. It repeats a set of statements while a condition is true. while ( condition ) {

tori
Download Presentation

Loops

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

  2. “while” structures • It repeats a set of statements while a condition is true. • while (condition) • { • execute these statements; • }

  3. “while” structures • It repeats a set of statements while a condition is true. • while ( condition ) • { • execute these statements; • } 1 2 3 • The dynamics of “while” • Evaluate condition: • if TRUE go to 2 • If FALSE go to 3 • Execute statements, and then go to 1 • Continue with next statement. 1. 2. 3.

  4. “while” structures • It repeats a set of statements while a condition is true. • int speedLimit = 55; • int speed = 0; • while ( speed <= speedLimit ) • { • speed = speed + 1; • } • // since we don’t want a ticket… • speed = speed - 1; • What is the value of “speed” after the • last statement? • 55 • 54 • 56 • 0 • none of the above

  5. “while” structures • It repeats a set of statements while a condition is true. • int speedLimit = 55; • int speed = 0; • while ( speed < speedLimit ) • { • speed = speed + 1; • } • // since we don’t want a ticket… • speed = speed - 1; 1 initialize variables in conditional 2 modify variables in conditional

  6. “while” structures: Exercises • Determine the output of the following methods: • public void foo1() { • int i=0; • while (i <= 20) { • System.out.println( i ); • i = i + 4; // or i+= 4; • } • } • public void foo2() { • int i = 20; • while (i > 0) { • i = i / 2; • System.out.println( i ); • } • } • 0 4 8 12 16 20 • 4 8 12 16 • 0 4 8 12 16 20 24 • 0 4 8 12 16 • none of the above • (on separate lines) • 20 10 5 2.5 1.25 • 10 5 2 1 0 • 20 5 2 1 • 20 5 2 1 0 • none of the above • (on separate lines)

  7. Computer Work • Write a program that reads an integer, and displays (using System.out.println) all numbers from the number down to 0. • For example, if 8 is entered, the program should display numbers 7, 6, 5, 4, 3, 2, 1, 0, in this order and with each number in one line. • Write a program that reads an integer, and displays (using System.out.println) all even numbers between 0 and the number received, and finally the number of numbers displayed. • For example, if the parameter was 8, the method should display numbers 2, 4 and 6, in this order and with each number in one line, and return a value of 3 (which is how many even numbers were between 0 and 8).

  8. “for” structures • It (also) repeatsstatements while a condition is true. Works best when loop will be repeated a known number of times 1 2 4 loop condition initial statement modify statement for ( ; ; ) { statements; } 3 5 • The dynamics of “for” • Initialize condition variables. • Evaluate loop condition: • if TRUE go to 3 • If FALSE go to 5 • Execute statements; then go to 4 • Modify condition variables; then go to 2 • Continue with next statements.

  9. prime using for Write a program that prints “yes” or “no” to indicate whether an integer parameter named number is a prime number. Use a for loop. How to write Java: Answer the questions: Exactly how do you do this? What is a step by step description? WRITE IN ENGLISH. NOT Java or Draw pictures. or both Debug the English: Trace through with numbers. especially with going through the loop 0 times, 1 time, >= 1 times. Fix the ENGLISH.

  10. Brainstorm. How do you figure out if a number is prime? prime(11) prime(12). Talk to each other. • Write an algorithm for that • Translate the algorithm to Java

  11. Prime Number Program import java.util.Scanner; public class Prime { public static void main(String[ ] args) { Scanner kbd = new Scanner(System.in); System.out.print("Enter a number: "); int nbr = kbd.nextInt( ); if (nbr < 2) // only look at positive numbers { System.out.println("no"); System.exit(0); // means leave the program right now } for (int i=2;i<nbr;i++) { if (nbr % i == 0) { System.out.println("no"); System.exit(0); } } System.out.println("yes"); } } for loop Leaves loop and program outside the loop

  12. Embedded loops • Write a program to draw a square box after reading in its size • Write the first line (for loop printing n stars) • println • for each internal line (n-2 of them) • print a * • print n-2 spaces • println a * • print the last line

  13. import java.util.Scanner; public class DrawStars { public static void main(String[ ] args) { Scanner kbd = new Scanner(System.in); System.out.print("Enter a number: "); int nbr = kbd.nextInt( ); if (nbr < 2) // only look at positive numbers System.exit(0); // means leave the program right now // do first line for (int i=0;i<nbr;i++) System.out.print("*"); // add enter System.out.println( ); // nothing in parentheses // continued on next page

  14. // print n-2 internal lines for (int i=0;i<nbr-2;i++) // i is never used. it only counts { // nbr of times through loop // print first character on line System.out.print("*"); // print number of spaces minus one * on each end for (int j=0;j<nbr-2;j++) // embedded loop for one internal line System.out.print(" "); // single space in between quotes // print last * on line -- also put enter in there System.out.println("*"); } // and outer for loop that does all internal lines // print last line. same as first line for (int i=0;i<nbr;i++) System.out.print("*"); System.out.println( ); // nothing in parentheses } // end main } // end class

  15. Embedded Loop Practice • Write a loop that prints a times table 0-2: 0 0 1 2 0 0 0 0 1 0 1 2 2 0 2 4 • Write the first line • for each number on the left (0, 1, 2) • print the left number (the LHS of the table) • for each number on the right (0, 1, 2) • print the product • println. 2a

More Related