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

Looping Statements in C - 6 Looping Statements and Usage (For, While, Do... While

C program lo

Uploaded by

rioj9944
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Looping Statements in C - 6 Looping Statements and Usage (For, While, Do... While

C program lo

Uploaded by

rioj9944
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What are Loops in C?

Loop is used to execute the block of code several


times according to the condition given in the loop.
It means it executes the same code multiple times.

Loop Types in C:
while loop
for loop
do-while loop

FOR LOOP

What is for loop?


A for loop is a repetition control structure that
allows you to efficiently write a loop that needs to
execute a specific number of times.

SYNTAX
The syntax of a for loop in C programming
language is
for (initialization; condition; increment/decrement ) {
statement(s);
}

INITIALIZATION:
The initialization step is executed first, and only
once. This step allows you to declare and initialize
any loop control variables. You are not required to
put a statement here, as long as a semicolon appears.
CONDITION:
Next, the condition is evaluated. If it is true, the
body of the
loop is executed. If it is false, the body of the loop
does not execute and the flow of control jumps to
the next statement just after the 'for' loop.

INCREMENT:
After the body of the 'for' loop executes, the flow of
control jumps back up to the increment statement.
This statement allows you to update any loop
control variables. This statement can be left blank,
as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true,


the loop executes and the process repeats itself
(body of loop, then increment step, and then again
condition). After the condition becomes false, the
'for' loop terminates.

FLOW DIAGRAM
#include <stdio.h>

int main () {
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}

return 0;
}
OUT PUT:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

INFINITY PROGRAM:

#include <stdio.h>
int main () {

for( ; ; ) {
printf("This loop will run forever.\n");
}

return 0;}
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++){
printf("%d \n",(number*i));
}
return 0;
}
OUTPUT:
Enter a number: 2
2
4
6
8
10
12
14
16
18
20
WHILE LOOP

WHAT IS WHILE LOOP?


A while loop in C programming repeatedly executes
a target statement as long as a given condition is true.

SYNTAX
The syntax of a while loop in C programming language is
while(condition) {
statement(s);
}
Here, statement(s) may be a single statement or a block of
statements. The condition may be any expression, and true is
any nonzero value. The loop iterates while the condition is true.

FLOW DIAGRAM:

#include <stdio.h>
int main () {

/* local variable definition */


int a = 10;

/* while loop execution */


while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

return 0;}
OUT PUT:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

DO-WHILE LOOP

What is do-while loop?


Unlike for and while loops, which test the loop
condition at the top of the loop, the do...while loop in C
programming checks its condition at the bottom of the
loop.
A do...while loop is similar to a while loop, except
the fact that it is guaranteed to execute at least one time.

SYNTAX
The syntax of a do...while loop in C programming
language is
do {
statement(s);
} while( condition );
FLOW DIAGRAM:

#include <stdio.h>
int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

return 0;}
OUTPUT:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

You might also like