Conditions and Loops
Conditions with if, else if, and else
The if statement makes it possible for a program to decide whether or not to run a chunk of code. The decision depends upon a condition, which will evaluate to…
The switch Statement
The switch statement selects one path to execute from multiple options, based on matching the value of an expression to a set of constant cases. It's really a s…
while Loops
A while loop executes a code block as long as some condition remains true. The program evaluates the condition before every execution of the block. If the condi…
do-while Loops
A do while loop executes its body first and only then checks its controlling condition. Because the test occurs after the body, the statements within the body a…
for Loops
Learn the initialization, condition, update, and body of a for loop, including scope, multiple expressions, and common off-by-one errors.
break and continue
The keywords break and continue can be used within loops to change their normal flow of execution. Each is typed as a single word with a semicolon immediately a…
Nested Loops
Nested loops are loops within the bodies of other loops. For each iteration of the outer loop, the inner loop runs from the start to the end of its own statemen…
Range-Based for Loops
A range-based for loop iterates over every element of a sequence without an explicit index and no explicit step of iteration. The loop variable is automatically…
Common Loop Errors
A loop tends to fail for one of a few reasons. Usually, these problems stem from a condition that doesn't eventually go to false, from a boundary that is off by…