Back to GT CS-1331

while Loops:

  • Runs a snippit of code until a condition evaluates to false

    • If the condition stays true, it creates an infinite loop

      • Ctrl-C can force quit the program
  • Can use a counter to track iterations

while (booleanExpression){
    statement1; //brackets not needed if there is only one statement
    statement2;
}

do-while Loops:

  • While loop but ensures that one interation is done
do {
 statement1;
 statement2;
} while(booleanExpression);

for Loop:

  • While loop but better in some ways
for(initStatement; condition; updateStatement) {
    statement1;
    statement2;
}

Nested Loops:

  • Can put loops into one another to solve more complex problems

  • Similar to nested if statements

The break Statement:

  • Can be used in both for and while loops

  • Can be used with if-else

  • Exits a loop

The continue Statement:

  • Can be used in both for and while loops

  • Can be used with if-else

  • Skips iteration and starts a new loop