This document explains the usage of the three main loop structures in C: do-while, while, and for. These loops are essential in microcontroller programming to execute repetitive tasks. Each loop structure serves specific purposes depending on the conditions and requirements of the task at hand.
The while loop repeatedly executes a block of code as long as the specified condition evaluates to true.
while (condition)
{
// code block to be executed
}- The condition is checked before entering the loop.
- If the condition is true, the code block is executed.
- After executing the block, the condition is checked again.
- The loop continues as long as the condition remains true.
- If the condition is false initially, the code block will not execute.
#include <stdio.h>
int main()
{
int count = 0;
/* Start the while loop */
while (count < 5)
{
printf("Count is: %d\n", count);
count++; /**< Increment the count */
}
return 0;
}- The loop starts with
count = 0. - The loop condition (
count < 5) is checked before each iteration. - The block inside the loop prints the current value of
countand increments it. - The loop stops when
countreaches 5.
The do-while loop is similar to the while loop, but the condition is checked after the execution of the code block. This ensures that the code block is always executed at least once.
do
{
// code block to be executed
} while (condition);- The code block is executed first, before checking the condition.
- After executing the block, the condition is checked.
- If the condition is true, the block is executed again.
- The loop continues until the condition becomes false.
- This loop guarantees that the block of code will always run at least once, even if the condition is false at the first check.
#include <stdio.h>
int main()
{
int count = 0;
/* Start the do-while loop */
do
{
printf("Count is: %d\n", count);
count++; /**< Increment the count */
} while (count < 5);
return 0;
}- The
do-whileloop executes the code block at least once, regardless of the initial value ofcount. - In this case, it will print
Count is: 0,Count is: 1, and so on, untilcountreaches 5.
The for loop is a versatile loop that allows initialization, condition checking, and increment/decrement in a single line. It is commonly used when the number of iterations is known beforehand.
for (initialization; condition; increment)
{
// code block to be executed
}- Initialization: This step is executed once, at the start of the loop.
- Condition: The condition is checked before each iteration. If it evaluates to true, the loop body executes.
- Increment/Decrement: This step executes after each iteration, typically used to update the loop variable.
#include <stdio.h>
int i = 0
int main()
{
/* Start the for loop */
for (i = 0; i < 5; i++)
{
printf("i is: %d\n", i);
}
return 0;
}- The loop starts with
i = 0. - The condition
i < 5is checked before each iteration. - After each iteration,
iis incremented by 1. - The loop continues until
ireaches 5.
whileloop: The condition is checked before the first iteration. If the condition is false, the loop may not run.do-whileloop: The code block runs at least once, and the condition is checked after the first iteration.forloop: Best suited when the number of iterations is known ahead of time. It combines initialization, condition checking, and increment/decrement into a single line.
This section discusses the usage of the control flow statements goto, break, and continue in C programming. These statements are used to alter the normal flow of control in the program, especially useful in embedded systems where specific control is needed for efficiency or error handling.
The goto statement is used to transfer control to another part of the program. While it's often discouraged due to its potential to make code less readable, it can still be useful in specific situations, especially in embedded systems for handling errors or skipping over complex conditions.
goto label;
...
label:
// code block to jump to- The
gotostatement transfers control to the label specified. - The label must be defined somewhere in the function.
- It is important to use
gotosparingly, as overuse can make the code harder to maintain and understand.
#include <stdio.h>
int main()
{
int x = 0;
/* Check if x is zero */
if (x == 0)
{
goto label; /**< Jump to label if x is zero */
}
printf("This will not be printed.\n");
label:
printf("Control jumped to label!\n"); /**< Code after the jump */
return 0;
}- If
xis 0, the program will jump to thelabeland print"Control jumped to label!". - The statement
"This will not be printed."is skipped due to thegotostatement.
The break statement is used to exit from a loop or a switch statement prematurely. It immediately terminates the nearest enclosing loop or switch, transferring control to the next statement after the loop or switch.
break;- The
breakstatement is commonly used inside loops to exit when a specific condition is met. - It can also be used to exit from a
switchstatement to avoid fall-through behavior.
#include <stdio.h>
int main()
{
int i;
/* For loop that will break when i equals 3 */
for (i = 0; i < 5; i++)
{
if (i == 3)
{
break; /**< Exit the loop when i equals 3 */
}
printf("i is: %d\n", i);
}
printf("Loop terminated early.\n");
return 0;
}- The loop will terminate when
iequals 3 due to thebreakstatement. - The output will print values of
iup to 2, and then it will print"Loop terminated early.".
The continue statement is used to skip the current iteration of a loop and proceed with the next iteration. It does not exit the loop entirely, but simply skips over the remaining code in the current iteration.
continue;- When the
continuestatement is encountered inside a loop, it skips the remaining code in the current iteration and proceeds to the next iteration. - It is commonly used to skip over certain iterations based on a condition.
#include <stdio.h>
int main() {
int i;
/* For loop that skips printing even numbers */
for (i = 0; i < 5; i++)
{
if (i % 2 == 0)
{
continue; /**< Skip even numbers */
}
printf("i is: %d\n", i);
}
return 0;
}- The loop will skip printing even values of
ibecause of thecontinuestatement. - The output will print only odd numbers:
1and3.
goto: Use sparingly for jumping to specific points in the program, mainly in error handling or situations where other control structures are not suitable.break: Ideal for prematurely exiting from loops orswitchstatements.continue: Best used when you want to skip over specific iterations in a loop without breaking out of it entirely.
If you found this repository useful:
- Subscribe to my YouTube Channel.
- Share this repository with others.
- Give this repository and my other repositories a star.
- Follow my GitHub account.
Feel free to reach out to me through any of the following platforms: