Loops
Loops
Loops in programming are used to repeat a block of code until the specified
condition is met. A loop statement allows programmers to execute a statement
or group of statements multiple times without repetition of code.
#include <stdio.h>
int main()
{
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
return 0;
}
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops the test condition is
checked before entering the main body of the loop. For Loop and While
Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is
evaluated at the end of the loop body. The loop body will execute at least
once, irrespective of whether the condition is true or false. do-while
Loop is Exit Controlled loop.
While Loop
A while loop executes a block of code as long as a specified condition is true. It's
like telling the computer, "Keep doing this while something is true."
#include <stdio.h>
int main() {
int i = 1;
return 0;
}
In this example:
The variable i is initialized to 1.
The while loop checks if i <= 5.
As long as i is less than or equal to 5, it prints the value of i and
increments it by 1.
Important Points
Condition first: The condition is checked before the loop body is
executed. This means if the condition is false initially, the loop will not run
at all.
Infinite loop: If the condition is always true, the loop will run indefinitely.
You should make sure there is a condition inside the loop body that
eventually becomes false.
Example of an infinite loop:
while(1) {
printf("This will print forever!\n");
}
Loop control with break: The break statement can be used to exit the loop
prematurely.
int i = 1;
while (i <= 10) {
if (i == 6) {
break; // Exit the loop when i is 6
}
printf("%d\n", i);
i++;
}
Loop control with continue: The continue statement can be used to skip the
current iteration and move to the next one.
int i = 0;
while (i < 10) {
i++;
if (i == 5) {
continue; // Skip the iteration when i is 5
}
printf("%d\n", i);
}
Common Mistakes
Forgetting to update the loop variable: If the loop variable is not updated
correctly, it can cause an infinite loop.
Example of an infinite loop due to a mistake:
int i = 1;
while (i <= 5) {
printf("%d\n", i);
// Missing the increment step here will cause an infinite loop
}
Using wrong condition: Ensure the condition is meaningful and will eventually
become false. For example:
int i = 10;
while (i < 5) {
printf("This won't run because the condition is false initially.\n");
}
Continue Statement in C
The continue statement in C is a jump statement that is used to bring the
program control to the start of the loop. We can use the continue statement in
the while loop, for loop, or do..while loop to alter the normal flow of the program
execution. Unlike break, it cannot be used with a C switch case.
What is continue in C?
The C continue statement resets program control to the beginning of the
loop when encountered. As a result, the current iteration of the loop gets skipped
and the control moves on to the next iteration. Statements after the continue
statement in the loop are not executed.
Syntax of continue in C
The syntax of continue is just the continue keyword placed wherever we want in
the loop body.
continue;
Use of continue in C
The continue statement in C can be used in any kind of loop to skip the current
iteration. In C, we can use it in the following types of loops:
Single Loops
Nested Loops
Using continue in infinite loops is not useful as skipping the current iteration
won’t make a difference when the number of iterations is infinite.
do…while Loop in C
Loops in C language are the control flow statements that are used to repeat
some part of the code till the given condition is satisfied. The do-while loop is
one of the three loop statements in C, the others being while loop and for loop. It
is mainly used to traverse arrays, vectors, and other data structures.
What is do…while Loop in C?
The do…while in C is a loop statement used to repeat some part of the code till
the given condition is fulfilled. It is a form of an exit-controlled or post-tested
loop where the test condition is checked after executing the body of the loop.
Due to this, the statements in the do…while loop will always be executed at least
once no matter what the condition is.
Syntax of do…while Loop in C
do {
} while (condition);
Difference between while and do…while Loop in C
for Loop in C
The for loop in C Language provides a functionality/feature to repeat a set of
statements a defined number of times. The for loop is in itself a form of
an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization,
condition, and updating statements as part of its syntax. It is mainly used to
traverse arrays, vectors, and other data structures.
Syntax of for Loop
for(initialization; check/test expression; updation)
{
// body consisting of multiple statements
}
Structure of for Loop
The for loop follows a very structured approach where it begins with initializing a
condition then checks the condition and in the end executes conditional
statements followed by an updation of values.
1. Initialization: This step initializes a loop control variable with an initial
value that helps to progress the loop or helps in checking the condition. It
acts as the index value when iterating an array or string.
2. Check/Test Condition: This step of the for loop defines the condition
that determines whether the loop should continue executing or not. The
condition is checked before each iteration and if it is true then the iteration
of the loop continues otherwise the loop is terminated.
4. Updation: This specifies how the loop control variable should be updated
after each iteration of the loop. Generally, it is the incrementation
(variable++) or decrementation (variable–) of the loop control variable.
How for Loop Works?
The working of for loop is mentioned below:
Step 1: Initialization is the basic step of for loop this step occurs only
once during the start of the loop. During Initialization, variables are
declared, or already existing variables are assigned some value.
Step 2: During the Second Step condition statements are checked and
only if the condition is the satisfied loop we can further process otherwise
loop is broken.
Step 3: All the statements inside the loop are executed.
Step 4: Updating the values of variables has been done as defined in the
loop.
Continue to Step 2 till the loop breaks.