A Branch lets program choose between two alternatives.
if-else statement:
- Is used in C++ to perform a branch.
- For example:
- if (hours > 40)
- gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
- else
- gross_pay = rate * hours;
- if (boolean expression)
- When the boolean expression is true only the true statement is executed.
- When the boolean expression is false only the false statement is executed.
While-loop:
- When an action must be repeated, a while-loop is used.
- First, the boolean expression is evaluated:
- If false, the program skips to the line following the while loop.
- If true, the body of the loop is executed.
- Syntax:
- while (boolean expression is true);
- {
- statements to repeat
- }
do-while loop:
The body of the loop is first executed.
The boolean expression is checked after the body has been executed.
do
{
statements to repeat
}
while (boolean_expression);
Infinite Loop:
- Loops that never stop are infinite loops.
- The loop body should contain a line that will eventually cause the boolean expression to become false.
No hay comentarios.:
Publicar un comentario