Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sophomore Scholars Java

Similar presentations


Presentation on theme: "Sophomore Scholars Java"— Presentation transcript:

1 Sophomore Scholars Java
Loops

2 Loops – The Idea What if you wanted a block of code to execute more than once? You could, of course, copy and paste the code over, and over, and over, and … but what if the code needed to be repeated for an unknown amount of times (like based on an unknown data set)? You could use loops. A loop is a block of code which repeats until a certain condition is met. There are two types of loops that you will be working with in this class.

3 While Loops Here is a snippet of code which contains a while loop:
boolean keepGoing = true; int i = 0; while (keepGoing) { System.out.println(“I am inside the loop and i is ” + i); i++; //increase i by 1 if (i == 6) { keepGoing = false; }

4 Examining the Loop That looks like an if statement!
while (keepGoing) { System.out.println(“I am inside the loop and i is ” + i); i++; //increase i by 1 if (i == 6) { keepGoing = false; } That looks like an if statement! It works in the same way, although an if statement will only run once if the condition is met.

5 While Cont… while (keepGoing) { System.out.println(“I am inside the loop and i is ” + i); i++; //increase i by 1 if (i == 6) { keepGoing = false; } The variable i, in this case, is something referred to as a counter. It is a variable which is changed somehow in the loop, and is used to stop the loop eventually. Without the i in this loop, the loop would run forever (infinite loop). i++ means i = i + 1; This is why a language is called C++ (it was like C + 1) i– means i = i - 1;

6 While Cont… while (keepGoing) { System.out.println(“I am inside the loop and i is ” + i); i++; //increase i by 1 if (i == 6) { keepGoing = false; } Once i is equal to 6, the keepGoing variable is set to false. This will end the loop. The output for this loop is: I am inside the loop and i is 0 I am inside the loop and i is 1 I am inside the loop and i is 2 I am inside the loop and i is 3 I am inside the loop and i is 4 I am inside the loop and i is 5 The loop will run 6 times.

7 For Loops for loops are another type of loop that is used in computer science. Most languages have their own version of a for loop. In Java, the syntax will seem unfamiliar to you. A for loop looks like: for (<variable declaration>;<condition>;<counter update) { Block of Code } Unlike a while loop, the counter is updated implicitly in the for statement. You do not need to keep track of it in the block of code.

8 For Loop Example for (int i=0;i<6;i++) { }
System.out.println(“I am inside the loop and i is” + i); } This will output to the screen: I am inside the loop and i is 0 I am inside the loop and i is 1 I am inside the loop and i is 2 I am inside the loop and i is 3 I am inside the loop and i is 4 I am inside the loop and i is 5 This does exactly the same thing as the previous while loop!

9 Loops – Another Example
The ! in math means factorial. A factorial is the product of the original number and all of the numbers prior to it, except for 0. You have been tasked to write a program that prints out the factorial of a number entered from input. You can do this using either of the two loops types!

10 Using a While Loop import java.util.Scanner; class FactorialWhile { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); System.out.print("Please enter the number to be factorial-ed: "); int originalNumber = myScanner.nextInt(); int outputOriginalNumber=originalNumber; int product=1; //do not set to 0 System.out.println("The original product is " + product); while (originalNumber > 0) { System.out.print("originalNumber is " + originalNumber + " and "); product = product * originalNumber; System.out.println("the new product is " + product); originalNumber--; } System.out.println(outputOriginalNumber + "! is " + product);

11 Some Things To Talk About
int originalNumber = myScanner.nextInt(); int outputOriginalNumber=originalNumber; This may seem redundant. Why would I have two variables that are essentially the same thing? Answer: I edit the original number in the while loop, so I would not be able to output it in a println statement at the end of the program with the correct value. System.out.println(originalNumber + “!”); - would always print 0 for originalNumber if I did not save the original value elsewhere.

12 More Things To Talk About
int product=1; //do not set to 0 What would have happened if I set it to 0? Let’s say the person wanted to get 3! (which is 6). Product = product * originalNumber is the line that calculates the product as the loop executes. If the value of product is originally 0, you will get: product = 0 * 3 (which is 0) product = 0 * 2 (which is 0) product = 0 * 1 (which is 0) The final answer would be 0…which is bad.

13 The Correct Way If you set product equal to 1 in the beginning, you get: product = 1 * 3 (which is 3) product = 3 * 2 (which is 6) product = 6 * 1 (which is 6) The final answer is 6 – Yay! Since 1 is the multiplicative identity, set product originally to 1. What if you were doing addition?! Set it to the additive identity, which is 0.

14 Example Execution Please enter the number to be factorial-ed: 6 The original product is 1 originalNumber is 6 and the new product is 6 originalNumber is 5 and the new product is 30 originalNumber is 4 and the new product is 120 originalNumber is 3 and the new product is 360 originalNumber is 2 and the new product is 720 originalNumber is 1 and the new product is 720 6! is 720

15 Factorial Using a For Loop
import java.util.Scanner; class FactorialFor { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); System.out.print("Please enter a number to be factorial-ed: "); int originalNumber = myScanner.nextInt(); int product = 1; System.out.println("The original product is " + product); for (int i=originalNumber;i>0;i--) { System.out.print("i is " + i + " and " ); product = product*i; System.out.println("the new product is " + product); } System.out.println(originalNumber + "! is " + product);

16 What's Different?! for (int i=originalNumber;i>0;i--) {
System.out.print("i is " + i + " and " ); product = product*i; System.out.println("the new product is " + product); } The loop is a lot cleaner, but perhaps harder to understand. int i=originalNumber sets i equal to the original number value. i>0 is the condition for the loop to continue. Once i is less than or equal to 0, the loop stops. i-- is what is done to i in the loop. Here, we start at the original number and decrement it until it is 0.

17 Example Execution Please enter a number to be factorial-ed: 6
The original product is 1 i is 6 and the new product is 6 i is 5 and the new product is 30 i is 4 and the new product is 120 i is 3 and the new product is 360 i is 2 and the new product is 720 i is 1 and the new product is 720 6! is 720 This is almost exactly the same as the while loop!

18 Which One Do I Use?! For the most part, it’s a matter of preference.
*I* will be telling you which to use for this course. Choosing on your own is a skill to be developed in the AP Computer Science course. However, there are some key differences between the two which requires one or the other.

19 When to Use Which for is typically used for iterating through arrays (huh) or when working with numerical values. while is used when working with more complex data types, such as Vector or ArrayList (AP Comp Sci things) Do not worry about this!

20 What Do You Think This Does?
class TestingForLoops { public static void main(String[] args) { boolean blah=true; int i=0; for (;blah;) { i++; System.out.println("In loop and i is " + i); if (i==10) { blah = false; }


Download ppt "Sophomore Scholars Java"

Similar presentations


Ads by Google