CS 111A Lecture Notes - Loops - Chapter 4
Loops
- loop - a way of repeating a block of code while a specified condition is true. Use a loop when
you want to repeat a task.
- "while" loops have two main formats:
- Notice the placement of semi-colons above.
- Beware of infinite loops!
- Notice that "do" cannot stand alone. "do" is only used at the beginning of a do...while loop.
- Rules regarding use of curly braces "{" and "}" - If you want more than one line of code in the
result/body of an if-statement, else-statement, or loop, then that result/body code must be enclosed by
curly braces. They define a block of code with its own scope (We'll learn more about this later.)
Examples of while loops
Advanced Loop Control: break and continue
For-loops
Nested Loops
Return to the main CS 111A page