Looping Statement in C

Advertisements

Switch Case in C While Loop in C

Looping Statement in C

Looping statement are the statements execute one or more statement repeatedly several number of times. In C programming language there are three types of loops; while, for and do-while.

Why use loop ?

When you need to execute a block of code several number of times then you need to use looping concept in C language.

Advantage with looping statement

  • Reduce length of Code
  • Take less memory space.
  • Burden on the developer is reducing.
  • Time consuming process to execute the program is reduced.
loop in c

Types of Loops.

There are three type of Loops available in 'C' programming language.

  • while loop
  • for loop
  • do..while

Difference between conditional and looping statement

Conditional statement executes only once in the program where as looping statements executes repeatedly several number of time.

While loop

In While Loop in C First check the condition if condition is true then control goes inside the loop body other wise goes outside the body. while loop will be repeats in clock wise direction.

while loop in c

Syntax

Assignment;
while(condition)
{
Statements;
......
Increment/decrements (++ or --);
}

Note: If while loop condition never false then loop become infinite loop.

Example of while loop

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
while(i<5)
{
printf("\n%d",i);
i++;
}
getch();
}

Output

1
2
3
4

For loop

For Loop in C is a statement which allows code to be repeatedly executed. For loop contains 3 parts Initialization, Condition and Increment or Decrements.

for loop

Example of for loop

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
for(i=1;i<5;i++)
{
printf("\n%d",i);
}
getch();
}

Output

1
2
3
4

do-while

A do-while Loop in C is similar to a while loop, except that a do-while loop is execute at least one time.

A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block (in while).

Syntax

do
{
Statements;
........
Increment/decrement (++ or --)
} while();
do while loop in c

When use do..while Loop

When we need to repeat the statement block at least 1 time then we use do-while loop.

Example of do..while loop

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
do
{
printf("\n%d",i);
i++;
}
while(i<5);
getch();
}

Output

1
2
3
4

Nested loop

In Nested loop one loop is place within another loop body.

When we need to repeated loop body itself n number of times use nested loops. Nested loops can be design upto 255 blocks.


Switch Case in C While Loop in C

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements