Loops
keep looping some codes to run it many times
Iteration Statements
Iteration statements are used to repeat a section of code.
While loop
This statement will execute repeatedly as long as the condition is met. The format is as follows:
while (<expression>) <statement>
The following is an example:
The program checks if i < 10
. If true, then the statements inside {}
the block are executed. After the block is executed, the program loops back to i < 10
and executes the block again, until the condition is false.
Do-while loop
This statement will execute, then repeatedly execute as long as the condition is met. The format is as follows:
do <statement> while (<expression>);
If is_repeated
is ture
, the statement will keep looping until is_repeated
is false
.
For Loop
Often, developers use while loops to execute code while counting. For loops were introduced to simplify this situation while encasing the iterator (the counting number) within the scope. The following example illustrates this.
The for-loop format is as follows:
for (<init_clause>; <expression (optional)>; <expression (optional)>) <statement>
The 3 clauses in the parenthesis are usually called:
Initialization Expression
This expression is run before the statement. Usually, an iterator variable is declared and initialized here.
Condition
This expression is run before every iteration of the loop to determine whether the body of the loop would be run, or whether the loop ends here. Same as before, the body is run when this expression is a non-zero value.
Final Expression
This expression is run after every iteration of the loop. Usually, the iterator is incremented or decremented here.
This is the for-loop version of the number printing program above:
Of course, because the body is only a single line, the curly braces are not required. So it can become as so:
In fact, you can omit one of the clauses or even all of them:
Jump Statements
Jump statements alter the flow of the program by literally jumping to a different section of code to execute.
break;
This statement is only valid in loops and switch statements. It is used to exit the statement, breaking the execution.
Example:
continue;
This statement is only valid in loops. It breaks from the current iteration and executes the final expression if the loop is a for loop, then it will move to the condition for comparison immediately, continuing the loop.
Example:
return <expression (optional)>;
This statement is only valid in functions. It terminates the function, and then returns a resulting value to the caller. It is seen in the
main()
function, returning a program termination code to the operating system.return;
is treated as returningvoid
.goto <identifier>;
This statement is used to jump to a certain label to continue program execution. This statement is highly unrecommended, since it may create an erratic control flow as it can jump from scope to scope, and is harmful to debugging as it is difficult to follow the execution path of the program.
Last updated