Loops
Loops
By: Paul
What is a Loop
A loop is a fundamental control structure that allows you to repeatedly
execute a block of code as long as a certain condition is true. It provides a
way to perform tasks iteratively, making it more efficient and concise than
writing repetitive code manually.
Loops are powerful constructs for automating repetitive tasks, and they can
be combined with conditional statements to create complex algorithms. Be
cautious to avoid infinite loops where the loop condition never becomes
false, as it can lead to program freezing or unexpected behavior.
While Loop Structure
while( condition )
{
//put code here that will run continuously while the condition is true
}
//the program will continue here
while( condition 1 )
{
//put code here that will run continuously while the condition is true
}
while( condition 2 )
{
//put code here that will run continuously while the condition is true
}
while( condition 3 )
{
//put code here that will run continuously while the condition is true
}
do while structure
do
{
//his loop will execute the code block once, before checking if the condition
is true, then it will repeat the loop as long as the condition is true
}
while( condition );
For Loop Structure
for( statement 1; statement 2; statement 3 )
{
//write your code here that will run several times
}
Statement 1 = is executed one time before executing the block (initialize)
Statement 2 = defines the condition for running the block
Statement 3 = is executed every time after the block has been executed.
For Loop Structure
for( int i = 0 ; i < 10 ; i++ )
{
// ! remember that the variable i is only accessible withing this scope !
//write your code here that will run several times
}
int i = 0; = initialise a variable defining the loop will start counting from 0
i < 10; = the loop will run while this condition is true in this case while i < 10
i++ = the variable i will increment by 1 after every iteration
for( int i = 0 ; i < 10 ; i++ )
{
//write your code here that will run several times
}
int i = 0 ;
Here you declare a variable and give it an initial value
i < 10 ;
put a condition of when the loop will run < , > , <= , >= , = , !=
i++
How would you like the loop to count? E.g.
i++ count up and increment by 1
i-- count down decrementing by 1
i += 3 count up incrementing by 3
i -= 4 count down decrementing by 4
break
break;
while( condition )
{
//write your code here that will run several times
break; //when this line is executed, the loop will be exited immediately
}
//program will continue here
for( statement 1; statement 2; statement 3 )
{
//write your code here that will run several times
break; //when this line is executed, the loop will be exited immediately
}
//program will continue here
Which Loop should I use and when
To choose the right Loop:
Select the loop that best fits the situation.
➢ Use a for loop when you know the number of iterations beforehand.
➢ Use a while loop when the number of iterations is not predetermined but relies on a
condition.
➢ Use a do-while loop when you want the loop to execute at least once.
Guidelines for loops
Initialize the loop control variable before the loop starts.
Update (increment or decrement) the loop control variable within the loop
body.
Be cautious about infinite loops; make sure the loop control variable reaches
a condition that terminates the loop.
Guidelines
1 Loop Condition Remember, the loop continues as long as the condition is true.
2 Exit Condition Make sure there's a clear exit condition for the loop to prevent infinite loops also ensure the exit condition
becomes true at some point within the loop.
3 Loop Body Place the code to be executed within the loop's curly braces make sure to keep the loop body concise and
focused.
4 Loop Control Statements Use break to exit a loop prematurely and continue to skip the current iteration and move to the next one.
5 Avoid Nested Loops Nested loops can quickly become complex and hard to maintain. If possible, consider using functions to
break down the logic.
6 Optimization Minimize calculations within loops that don't change during iterations.
If loop conditions can be evaluated before the loop starts, do so to potentially avoid entering the loop.
7 Avoid Unnecessary If possible, perform calculations or checks outside the loop that don't need to be repeated on every iteration.
Work
8 Loop Variable Scope The loop control variable declared in the for loop header has scope limited to the loop. If you need its value
after the loop, declare it outside the loop.
9 Consider Efficiency If you're working with a large number of iterations, try to optimize your loop code to minimize processing time
and memory usage.
10 Use Meaningful Variable Choose variable names that reflect the purpose of the loop and its control variables.
Names
END