Looping
Looping
Types of Loops
Common types of loops include "for" loops, "while" loops, and "do-while" loops, each
with its own way of controlling the number of iterations.
Iteration-the process of executing a loop is also known as iteration, where the code block is
repeated multiple times.
For loop is a control flow statement used to execute a block of code repeatedly, a certain
number of times. It's commonly used when the number of iterations is known beforehand. For
loops often include a loop counter or variable, allowing the body of the loop to track the current
iteration.
Functionality
Repeated Execution- the core purpose of for loop is to run a specific set of instructions
multiple times.
Known Number of Iterations- for loops are particularly well-suited when you know in
advance how many times the code block needs to be executed.
Loop Counter/Variable - many for loops use a counter or variable that changes with each
iteration, enabling the loop's body to understand which iteration it's currently executing.
Key Parts of a For Loop
1. Initialization: This part typically declares and initializes a counter variable (e.g., int i = 0;). It's
executed only once, at the beginning of the loop.
2. Condition: This part checks a condition that determines whether the loop should continue
executing. The loop runs as long as this condition is true.
3. Update: After each iteration of the loop's body, the update expression is executed. This often
involves incrementing or decrementing the counter variable (e.g., i++).
4. Loop Body: This is the block of code that gets executed repeatedly with each iteration of the
loop.
1. Repeating a task a specific number of times: If you need to do something a fixed number of
times, a for loop is often the best choice.
2. Iterating through sequences: For loops can be used to go through the elements of a list, array,
or other data structures.
3. Numerical calculations: When you need to perform calculations that involve a sequence of
numbers, for loops can be very useful.
Sytanx of for loop:
for (int i = 0; i < 5; i++) {
body of the loop
}
Do-while loop is a control flow statement in programming that executes a block of code at least
once, and then repeatedly executes it as long as a specified condition is true. Unlike a while loop,
which checks the condition before the first execution, the do-while loop executes the code block
first and then checks the condition.
Key Characteristics
Execution at least once: The code inside the do-while loop will always be executed at least one
time, regardless of the initial condition.
Post-test loop: The condition is checked after the code block is executed, hence it's also called a
post-test loop.
Exit-controlled loop: The loop continues as long as the condition remains true, and it will
terminate when the condition becomes false.
When to use a do-while loop
When you need to execute a block of code at least once before checking a condition.
When you want to ensure a user input is valid before continuing.
When you need to perform some initial setup or processing before deciding whether to continue
looping.
While loop is a control flow statement that executes a block of code repeatedly as long as a
specified condition remains true. It allows you to repeat a set of instructions based on a Boolean
condition, similar to a repeating if statement.
Here's a breakdown:
Condition: The while loop's condition is a Boolean expression that is evaluated before each
iteration of the loop.
Execution: If the condition is true, the code block within the loop is executed.
Iteration: After executing the code block, the condition is evaluated again.
Termination: The loop continues to execute as long as the condition remains true. Once the
condition becomes false, the loop terminates, and the program continues with the next
instruction.
Key points about while loops
Unknown Iteration: While loops are often used when the number of iterations is not known in
advance.
Conditional Execution: They execute a block of code repeatedly based on a condition.
Potential for Infinite Loops: If the condition never becomes false, the loop will continue
indefinitely, leading to an infinite loop.
State Machines: While loops are commonly used in state machine implementations, where the
program transitions between different states based on conditions.
while (condition) {
// Code to be executed repeatedly
}