Read: http://www.learnjavaonline.org/en/Loops
Alright now we are moving into some more dangerous, but very helpful stuff. First lets start with the easy while loop.
while loop is when a requirement is met and makes a section of the code run over and over again nonstop, until the condition is false.
Copy the code in the if statement lesson that has an if and else statement. Then in your if statement, write a while loop like this:
public class Main {
public static void main(String[] args) {
boolean stupid = true;
if(stupid == false){
System.out.println("SMART!@#");
while(stupid == false){
System.out.println("HA");
} //Think about what would happen here
}
else{
System.out.println("DUNCE");
}
}
}
Essentially, if stupid is false then the code will spit out SMART!@# and at the same time run an infinite stream of HA. This instance is called an infinite loop and is a terrible, HORRIBLE, annoyance that is the equivalent of spamming in a video game.
In order to prevent this from happening we add in a line of code called break, which stops any loop from carrying out its function. So lets adjust our code where break can be used.
public class Main {
public static void main(String[] args) {
boolean stupid = false;
if(stupid == false){
System.out.println("SMART!@#");
int insult = 0;
while(stupid == false){
System.out.println("HA");
insult = insult + 1;
if(insult == 10){
break;
}
}
}
else{
System.out.println("DUNCE");
}
}
}
Now what the code will do is that when the if statement is initialized, it sets up an integer variable called insult which is set to 0. Then the while loop will spit out HA and at the same time, adding 1 to insult. When insult equals 10, then the while loop will break and the program will stop.
Lets move on to for loops.
For loop is basically like a while statement except that it condenses a common use of while loop into a simpler version. It usually starts by creating a new variable. Then, it checks the condition. If true, it will run the code and perform statement #3. Then, the condition will be checked again, and the process repeats. If the condition is false, the loop ends.
for(#1; #2; #3) {}
#1=initiation, #2=condition, #3=increment/decrement
Copy the following code below:
public class Main {
public static void main(String[] args) {
boolean stupid = true;
if(stupid == false){
System.out.println("SMART!@#");
for(int insult = 0; insult<10; insult++){
System.out.println("HA");
}
}
else{
System.out.println("DUNCE");
}
}
}
In the for loop, we make an integer called insult and then we say that if it is less than 10 add 1 to insult. After it adds 1 to insult, it then prints HA. This code should make the program spit out SMART!@# and HA 10 times. If you want to do it for more, change the value of where 10 is in the for loop's parenthesis.
For each loop is also very helpful if you have an array. See Learn Java Online (link at the top) for detail and practice.
Now you know how to use for and while loops!
coming soon
Created: Luke 4/3/18
Updated: Dexin 12/9/18