0% found this document useful (0 votes)
7 views

Loops_in_C_Notes

The document explains three types of loops in C: for, while, and do-while. The for loop is used when the number of iterations is known, the while loop is for unknown iterations with a pre-check condition, and the do-while loop executes at least once with a post-check condition. Each loop type is illustrated with syntax and example code that outputs the numbers 1 to 5.

Uploaded by

saransh8998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Loops_in_C_Notes

The document explains three types of loops in C: for, while, and do-while. The for loop is used when the number of iterations is known, the while loop is for unknown iterations with a pre-check condition, and the do-while loop executes at least once with a post-check condition. Each loop type is illustrated with syntax and example code that outputs the numbers 1 to 5.

Uploaded by

saransh8998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Loops in C: for, while, do-while

1. FOR Loop

- Best when you know how many times to loop.

- Syntax:

for(initialization; condition; increment) {

// code

Example:

#include <stdio.h>

int main() {

for(int i = 1; i <= 5; i++) {

printf("%d ", i);

return 0;

Output: 1 2 3 4 5

---------------------------------------------

2. WHILE Loop

- Used when number of iterations is unknown.

- Condition is checked before loop body.

Syntax:
while(condition) {

// code

Example:

#include <stdio.h>

int main() {

int i = 1;

while(i <= 5) {

printf("%d ", i);

i++;

return 0;

Output: 1 2 3 4 5

---------------------------------------------

3. DO-WHILE Loop

- Always executes at least once.

- Condition checked after loop body.

Syntax:

do {

// code

} while(condition);
Example:

#include <stdio.h>

int main() {

int i = 1;

do {

printf("%d ", i);

i++;

} while(i <= 5);

return 0;

Output: 1 2 3 4 5

You might also like