CSCI 1111 Chapter Notes - Chapter 4: Infinite Loop, For Loop, While Loop
Document Summary
Some behaviors should be repeated over and over, like a racecar driving around a track. A loop is a construct that repeatedly executes specific code as long as some condition is true. A while loop is a program construct that executes a list of sub-statements repeatedly as long as the loop"s expression evaluates true. If true, execution proceeds into the sub-statements inside the braces, known as the loop body. Each execution of the loop body is called an iteration while (expression) { // loop expression. // loop body: sub-statements that execute if the. // statements that execute after the expression evaluates to false. An infinite loop is a loop that will always execute. Commonly, a loop should iterate a specific number of times, such as 10 times. A loop variable counts the number of iterations of a loop. // iterating n times using loop variable i i = 1; while (i <= n) {