C Tutorial – for loop, while loop, break and continue
In every programming language, thus also in the C programming language, there are circumstances were you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf function, but it is easier to use a loop. The only thing you have to do is to setup a loop that execute the same printf function ten times.
There are three basic types of loops which are:
- “for loop”
- “while loop”
- “do while loop”
The for loop
The “for loop” loops from one number to another number and increases by a specified value each time.
The “for loop” uses the following structure:
for (Start value; end condition; increase value)
statement;
Look at the example below:
#include<stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf ("Hello\n");
printf ("World\n");
}
return 0;
}
Note: A single instruction can be placed behind the “for loop” without the curly brackets.
Let’s look at the “for loop” from the example: We first start by setting the variable i to 0. This is where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).
In the example we used i++ which is the same as using i = i +1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i–. It is also possible to use ++i or –i. The difference is is that with ++i the one is added before the “for loop” tests if i < 10. With i++ the one is added after the test i < 10.
The while loop
The while loop can be used if you don’t know how many times a loop must run. Here is an example:
#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
while ( counter < howmuch)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
Let’s take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.
The do while loop
The “do while loop” is almost the same as the while loop. The “do while loop” has the following form:
do
{
do something;
}
while (expression);
Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example:
#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
do
{
counter++;
printf("%d\n", counter);
}
while ( counter < howmuch);
return 0;
}
Note: There is a semi-colon behind the while line.
Break and continue
To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following example:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
if ( i == 10)
break;
}
return 0;
}
In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break).
With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
continue;
printf("Nothing to see\n");
}
return 0;
}
In the example above, the printf function is never called because of the “continue;”.
That was all for now. Don’t forget to make some example programs of your own, just for practice!
Update: You can also take a at the C tutorial: a star pyramid and string triangle using for loops
the examples given is good but I have to know first the program that will output , “Do you want to try again?”.
please help me.
Write a program to print all the ASCII value using While Loop.
Looks like a home-work question, so we won’t give you a while-loop answer, instead we give a for loop example:
To find ASCII value of character in C, you just have to remember the following: a variable of type char is just an 8-bit int.
#include<stdio.h>
void main(void)
{
int i;
for (i = 0; i < 128; i++)
{
printf("%d = %c\n", i, i);
}
}
For an ASCII-table look at http://www.asciitable.com/
You can figure out the rest yourself!
@shelven,
You can try something like
do{
/* blah blah */
printf(“\n Do you want to continue? \n”);
} while(getchar()!=’n’ && getchar()!=’N'); /* Continue while the response does *not* equal ‘n’ or ‘N’
don’t forget parenthesis while using “&&” operator
while( (getchar()!=’n’) && (getchar()!=’N’) );
hello sir
i want to know how we can use two FOR loop.
which out put must be-
A B C D E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
please help me
hello
i need this out put some one help
here a and b are variables
this output should be through loops.
a=1, b=0 a=5 , b=2 ;
a=3, b=1 a=7 , b=3 ;
a=1, b=0 a=5 , b=2 ;
a=3, b=1 a=7 , b=3 ;
Perfect
********** code for Akhter*********
enjoy but try to understand programming
void main()
{
clrscr();
int a=1,b=0,c=0;
while (c<2)
{
cout<<endl;
cout<<"a="<<a<<" ,b ="<<b<<", a="<<a+4<<", b= "<<b+4;
a+=2;
b+=1;
cout<<endl;
cout<<"a="<<a<<" ,b ="<<b<<", a="<<a+4<<", b= "<<b+4;
c++;
a=1;
b=0;
}
getch();
}
hi i am nooruddin i am IT teacher you are going good in programming, i like your lessons
it’s helpful
you are doing well to reply our qns and to teche programming thanks
This helps me to understand d concept of while,do while and for loop …
thanx…
the above examples are very good
Thank u so much sir hv is vr use ful 2 me. Iam a b.com student…
thank u so much sir this is vry heplful for me am a btech student
any body tell me — when i am run a for loop like for(i=0;i<=10;i++)thn result is 012345678910
i wan to know how's the 0 come;
(i=0;i<=10;i++) —-
i=0; is the start make it (i=1;
i<=10 is the end process
reply for Ankit:
Hi Ankit
the loop is (i=0;i<=10;i++)
i is the variable which stands for starting value,ending value and inc or dec.
here u hav started with 0 only.Thts y it came 0.If u want to atrt with 1 means u should write ur loop like ths (i=1;i<=10;i++)
so good!
All,
Can any one help me to code a program using while loop, that will count the charecters,numbers, alphanumerical.
for ankit
hello ankit,in the 1st step of for loop the value are intialise ok,then second test the condition, then in third incre/decrement ok
now if initialise i=0
then 1st it will check condition then if condition is true then it will execute the block or a single line statement.
after that the value of i will going for incr/decrement ok
that’s why ur output will showing like that-
012345678910 ok
Hello Sir,
i want to know that how to use for loop this statement
*
* A *
* A * A *
* A * A * A *
@tarun: take a look at C Tutorial a star pyramid an string triangle using for loops. In that tutorial we do almost what you’re asking. You should be able to make the additional changes yourself.
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
i want output like this
conditions
1)using single loop(for or while)
2)can use maximum of 2 variables
i read break statement in book. But didnt got it. i read here and instantly got it because of your simple language. thnx
[...] C Tutorial – for loop, while loop, break and continue | CodingUnit Programming Tutorials C Programming – Arrays Basicly : Code: [...]
**decimal to binary using while loop **
#include
#include
#include
void main()
{
int n,s=0,r,x=0;
clrscr();
printf(“these programe is develope by manas maity.BCA \n”)
printf(“\n enter number:”);
scanf(“%d”&n);
while(n!=0)
{
n=n%2;
s=s+r*pow(10,x);
n=n/2;
x++;
printf(“the number is : %d”,s);
}
getch();
}
**write a programe to print a pascal trangle using for loop. **
#include
#include
void main()
{
int
clrscr();
printf(“these programe is develope by manas maity.BCA \n”);
pintf(“enter number which u want: “);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
c=1;
for(j=0;j<=n-i;j++)
printf("%3c",32);
for(j=0;j<=i;j++)
{
printf("%6d",c);
c=c*(i-j)/(j*1)
}
printf("\n");
}
getch();
}
It is very helpfull