Java Levels 3-4 Flashcards
loops
allow a chunk of code to be repeated over & over, depending on a condition
while
int x;
x = 0;
while(x < 10) { condition
System.out.println("Hello World!"); x++;
}
will repeat everything inside the curly braces of the loop as long as the condition inside the parentheses is true
x++
short for x = x + 1
incrementing
adding 1 to the value stored in the variable
increment operator
++ in x++
for
almost identical to while, but syntax diff
public class TheCount { public static void main() { for(int count = 1; count \<= 100; count++) { System.out.println(count + "! A-HA-HA!"); } } }
for(initialization; condition; statement)
initiliazition
The initialization section of a for loop is done only one time before the loop starts. Most programmers use this section to declare their counting variable and set up its starting value.
counting variable/count
integer
set to a starting #
sentinel loop
waits for a condition to be true before stopping but doesn’t involve a looping variable
waiting for user to type something before it exits