Tutorial references

c
c++
c#

Tutorial references

chapters

Loops

Loop

If you want to execute a block of code more than one time then you can use loop. Loops are used in c programming to repeat a specific block of code.

There are three types of loop:

(1.) for loop

(2.) while loop

(3.) do while loop

for() loop

Syntax

for(initialization; condition; iteration){

/* block of code here */

}

initialization : It is an initial variable of value which is used start for() loop. Initialization is a variable which is used to hold that initial value from where you want to start for() loop.

condition : If condition returns true then loop body will be execute. for() loop checks each and every iteration condition. A iteration which condition returns false value then for() loop will be automatically terminate. condition tells how many times you want to execute your for() loop.

iteration : It is use to increment or decrement initialization variable value.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int i;
 clrscr();
 printf("\n This is a for() loop statement program.");
 for(i=1;i<=10;i++){
 printf("\n %d HELLO FRIENDS ",i);
 }
 getch();
}

Run program

while() loop

Syntax:

initialization;
while(condition){


/* code here */

iteration;
}

initialization : It is an initial variable of value which is used start while() loop. Initialization is a variable which is used to hold that initial value from where you want to start while() loop.

condition : If condition returns true then loop body will be execute. while() loop checks each and every iteration condition. A iteration which condition returns false value then while() loop will be automatically terminate. condition tells how many times you want to execute while() loop.

iteration : It is use to increment or decrement initialization variable value.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int i;
 clrscr();
 printf("\n This is a while() loop statement program.");
 i=1;
 while(i<10){
 printf("\n %d HELLO WORLD",i);
 i++; /* you can write like this i= i + 1; */
 }
 getch();
}

Run program

do-while loop

"do-while" loop executes first do(){} body statements then check condition. Therefore its body statements are executed at least one time. It means "do-while" loop gives output at least one time.

If in initial iteration condition return false value then it return at least one time output otherwise it works same as "while()" loop.


initialization;
do{

/* code here */

iteration;
}
while( condition );

Initialization, iteration and condition is doing same work like while() loop.

Below program gives output same as while() loop because its condition does not return false value in initial iteration.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int i;
 clrscr();
 printf("\n This is a do-while loop statement program.");
 i=1;
 do{
 printf("\n %d HELLO WORLD",i);
 i++; /* you can write like this i= i + 1; */
 }
 while(i<10);
 getch();
}

Run program

Below program will give at least one time output because its condition return false value in initial iteration.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int i;
 clrscr();
 printf("\n This is a do-while loop statement program.");
 i=1;
 do{
 printf("\n %d HELLO WORLD",i);
 i++; /* you can write like this i= i + 1; */
 }
 while(i>10);
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018