C Language for & while & do..while Loop
Loops are vital in programming, automating repetitive tasks to save time and resources. They enhance code efficiency, readability, and resource utilization by reusing instructions and variables. Loops empower developers to create concise, adaptable solutions for various challenges.
Types of Loops
There are several types of loops commonly used in C programming:
- For Loop : These loop are ideal when you know how many times you want the code to repeat. They have a defined starting point, an end point, and a step size.
- While Loop :
While loop
keep running as long as a specified condition evaluates to true. They're useful when the number of iterations is uncertain. - Do-While Loop : Similar to
while loop
, but they execute the code block at least once before checking the condition.
for Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
OUTPUT:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
explanation:
#include <stdio.h>
: This line includes the standard input-output library, allowing the use of functions likeprintf
.int main()
: This is the main function where the program's execution begins.for (int i = 1; i <= 5; i++)
: This is thefor
loop construct.int i = 1
: This initializes a loop control variablei
with the value1
before the loop starts.i <= 5
: This is the condition. As long asi
is less than or equal to5
, the loop will continue to execute.i++
: This is the increment statement. After each iteration, the value ofi
is incremented by1
.printf("Iteration %d\n", i);
: This line prints the current value ofi
in the format "Iteration X" whereX
is the value ofi
.return 0;
: Themain
function returns an integer value (usually0
for successful execution) to the operating system.
while Loop
#include <stdio.h>
int main() {
int i = 0; // Initialization
while (i < 5) { // Condition
printf("Iteration %d\n", i);
i++; // Iteration (incrementing i)
}
return 0;
}
OUTPUT:
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
explanation:
#include <stdio.h>
: This line includes the standard input-output library, allowing you to use functions likeprintf
.int main()
: This is the main function where the execution of the program starts.int i = 0;
: Here, we initialize an integer variablei
with the value0
. This sets the starting point for the loop.while (i < 5) {
: This is the start of thewhile
loop. The loop will continue executing the code block as long as the conditioni < 5
is true.printf("Iteration %d\n", i);
: Inside the loop, this line uses theprintf
function to print the current value ofi
, which represents the iteration number.i++;
: After printing the iteration number, we increment the value ofi
by 1 using the++
operator. This is the iteration step, and it's crucial to ensure that the loop eventually terminates.- The loop continues to execute until the condition
i < 5
becomes false. return 0;
: This line ends themain
function and indicates that the program executed successfully. The value0
is returned to the operating system.
Do-while Loop
#include <stdio.h>
int main() {
int count = 1;
do {
printf("Iteration %d\n", count);
count++;
} while (count <= 5);
return 0;
}
OUTPUT:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
explanation:
The
#include <stdio.h>
line includes the standard input-output library, which allows you to use functions likeprintf()
for output.The
main()
function is the entry point of the program.We declare an integer variable
count
and initialize it with a value of1
.The
do..while
loop starts with thedo
keyword, followed by the code block to be executed.Inside the loop's code block, we use the
printf()
function to print the current iteration number using the value of thecount
variable.After printing, we increment the
count
variable using thecount++
statement.The
while
keyword is followed by the condition(count <= 5)
. This condition is checked after each iteration. If the condition istrue
, the loop continues; otherwise, the loop terminates.The loop's code block will be executed at least once, even if the condition is initially
false
.The loop continues to execute as long as the condition
(count <= 5)
remainstrue
.Once the condition becomes
false
(whencount
becomes6
), the loop terminates.The
return 0;
statement ends themain()
function and indicates successful program execution to the operating system.
Conclusion
In this example, all three loop types achieve the same goal of iterating and printing a sequence of numbers. However, they differ in terms of their initialization, condition checking, and termination behavior.