Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Loops 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. 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; } } 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. 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; 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. 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. 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! 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! 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 counter=originalNumber; int product=1; //do not set to 0 System.out.println("The original product is " + product); while (counter > 0) { System.out.print("counter is " + counter + " and "); product = product * counter; System.out.println("the new product is " + product); counter--; } System.out.println(originalNumber + "! is " + product); } } int originalNumber = myScanner.nextInt(); int counter=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. 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 * counter 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. 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. Please enter the number to be factorial-ed: 6 The original product is 1 counter is 6 and the new product is 6 counter is 5 and the new product is 30 counter is 4 and the new product is 120 counter is 3 and the new product is 360 counter is 2 and the new product is 720 counter is 1 and the new product is 720 6! is 720 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); } } 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. 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! 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. 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! 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; } } } }