looping

4

Click here to load reader

Upload: steven-li

Post on 23-May-2017

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Looping

LoopingA quick reminder from the “if” statement unit… Squiggly brackets control code.

{ // Code in here is controlled by the squiggly brackets // And the squiggly brackets are controlled by some statement. Last unit // Possible statements were if(condition), else if(alternate condition),else}In this unit, we will learn two more control statements to control squiggly brackets and they are

while for

While LoopsWhile loops are similar to if statements… They both take a condition check, both “activate” when the condition is true. The main difference between the if and while is that if statements “activate” only once. While statements activate as long as the condition remains true.

while(strBluePill.equalsIgnoreCase("Matrix")){ con.println("Matrix Matrix Matrix Matrix"); con.println(" Matrix Matrix Matrix Matrix");In this example, the condition to activate the while loop is if the variable has the word “matrix” in it. If the word has “matrix” the loop will run. And because there are no other inputs to possibly change the variable, this loop runs forever.

while(strBluePill.equalsIgnoreCase("Matrix")){ con.println("Matrix Matrix Matrix Matrix"); con.println(" Matrix Matrix Matrix Matrix"); con.println("Where does the blue pill take you now?"); strBluePill=con.readLine();In this example, we have input inside of the while loop. The user might decide to continue to type “matrix” and stay on the loop. The user might decide to type something else, making the loop condition false, and exit the loop.

While loops are also known as “conditional loops” because they run based on a condition.

Note- All of the comparisons that you can do with if statement conditions can be done with while loops.

< > <= >= == .equals

.equalsIgnoreCase != Not equals. Check

two numbers to see if they are NOT equal to one another

!strWord.equals(strWord2) -Not equals. Check two words to see if they are NOT equal to each other

Page 2: Looping

Random Numbers intRandom = (int)(Math.random()*10+1);In the example above, we are generating a random integer from 1 to 10. The code above is actually a formula. Let’s break it down:

1. Math.random() Generates a double number from 0.000 to 0.9992. *10 Multiplies that double number by 10 so the number is now 00.000 to 09.0003. +1 Adds 1 to that double number so the number is now 01.000 to 10.9994. (int) Then converts the double to an integer by chopping all decimals till it is 1-10

If you don’t care about the math and just want to generate a range of integers… remember this:

The first multiplied number represents the range The second added number represents the start number of that rangeEx: Range:51-70

intRandom = (int)(Math.random()*20+51);

Random numbers make our programs “fun” and “different”. Random numbers decide your next spawn point in a FPS. Random numbers shuffle decks of cards so solitaire plays different every time. Random numbers determine the next number that will show up in 2048 so that the puzzle. Even flappy birds use random numbers to randomize the lengths of pipes.

for(intCount=0;intCount<20;intCount++){ con.println("Something to repeat over and over again");

}In this example, the squiggly brackets are controlled by a “for” loop. This for loop has three statements within it. Here is the breakdown: intCount=0the intCount variable controls the loop and it is initialized to 0 intCount<20the loop will run as long as the intCount variable is less than 20. Condition check intCount++ The intCount variable will count up one time every loop. – means counting downFor loops can count up or count down. They can start at any number you want and end at any number you want… It all depends on the way you set up the three parts of the loop

For loops are also known as “counted loops” because they count.

intLength=strName.length();The length command can be done to any string variable to count the number of letters in the string. The resulting integer value can then be used to run a “for” loop, or even do a calculation.

strNameBack=strNameBack+strLetter;In this example, strings are added to strings but they aren’t mathematically added like numbers. The two strings are just attached to one another.

System.out.println("This gets printed to the interactions tab (command line): "+strNameBack);

Page 3: Looping

This is a special print command. It does not print to the console window, it prints to the “command line”. System.out.println is useful for testing messages to yourself (Ex: If you want to see the value of a variable but you don’t want it appear on the main window screen).

strNameBack= "";