Loops in C
If you want to repeat the same blocks of code over and over you have two choices. Copy and paste each block or you can use a loop. In C there are three different types of loops: for, while, and do…while. Each of them has their own specific uses and syntax, and below I’ll explain all three.
For Loops
for( variable declaration/initialization; condition; variable update) { //Code to be repeated }
So as you can see there are three parts needed to use a for loop. The first part is the variable declaration/initialization. Here you can either declare a new variable and assign it a value or use an existing variable. In C you can use any integer based data type (int, char, etc…), in other languages there are other types of for loops (for each…) where you can use other data types. Second, the condition tells the program to exit the loop when the conditional expression evaluates to false. Finally the variable update does just that, updates the variables. Generally you would put something like x++, x = x + 10 but if you really wanted to you could call another function to update the variable.
For loops are generally used when you know the exact number of iterations. Here’s an example of a simple for loop.
#include <stdio.h> int main() { int x; for (x = 0; x < 10; x++) { printf("x = %d", x); } return (1); }
The results of this program would be: “x = 0x = 1x = 2x = 3x = 4x = 5x = 6x = 7x = 8x = 9″ Because the program loops from 0 and exits when x = 10 (meaning it only prints up to 9). As you can see for loops are fairly easy.
While Loops
while ( condition ) { //Code to be repeated }
The condition can be any boolean expression and the loop will continue while the condition evaluates to true. Here are a couple of legal conditions: (x == 1), (x != 7), or even (x ==5 || v == 7) which says execute the loop while x is equal to 5 or v is equal to 7. A while loop is basically a simple for loop with no initialization or update section. Heres an example of a while loop.
#include <stdio.h> int main() { int x = 0; while (x > 10) { printf("x = %d", x); x++; } return (1); }
The program above would output the same as the for loop program but with an extra line of code. Now theres one thing you need to be careful about here. Since the while loop doesn’t initialize the condition variable it must be done outside of the loop. I’ve forgotten to do this before and the results can be varied. Sometimes it’ll be fine but sometimes you’ll get results that are not as expected.
Do..While Loops
do { //Code to be repeated } while (condition);
Do..While loops are essentially the same as While loops except that you know they’ll be executed at least once. The condition again can be any boolean expression and will loop while the condition is true. A while loop says “Loop while the condition is true, and execute this block of code”, a do..while loop says “Execute this block of code, and then continue to loop while the condition is true”. Here’s an example of a do..while loop.
#include <stdio.h> int main() { int x = 0; do { printf("x = %d", x); x++; } while (x << 10); return (1); }
The results are the same as before. One thing that people often forget about do..while loops is the semi-colon after the while (condition);
Break and Continue
These two keywords have great importance to looping. The Break command will exit the most immediately surrounding loop regardless of what the conditions of the loop are. If you are executing a loop and hit a continue statement, the loop will stop its current iteration, update itself (in the case of for loops) and begin to execute again from the top. Here’s an example of both statements.
#include <stdio.h> int main() { int x; for (x = 0; x < 10; x++) { if(x == 1 || x == 4 ) { continue; } else if(x == 8) { break; } else { printf("x = %d", x); } } return (1); }
The results of this are x = 0x = 2x = 3x = 5x = 6x = 7. As you can see both 1 and 4 are exclude and the list ends at 7. Break and Continue statements are very useful when used properly.












Get Updates via RSS
Get Updates via Email
What is RSS?
12 comments
10:57 am
[...] have now learned how to write your first c program as well as the different loops in the c language so its now time to learn about functions. In general, functions are blocks of code that perform [...]
9:57 pm
..nice one..
1:48 am
hi friends i had a doubt …..
can we declare a variable are an character in an if loop (or) while loop what ever it may be….
for ex:
main()
{
int i=0;
if ( i <=5)
{
int P =0; //can i declare P here are not
P = P+i;
printf(”%d”,&P);
}
if it is wrong than what is the reason ……..
thx in advance.
6:46 am
@Praveen
You can do this. Just note that you aren’t actually using a for loop here.
The output of this program will always be 0.
1:53 am
can there be if((res1&& res2) == 0)
can there be a condition like the above mentioned.
12:43 pm
why it gives always 0’s pls tell me the reason.
for ex:
main()
{
int i=0;
if ( i <=5)
{
int P =0; //can i declare P here are not
P = P+i;
printf(”%d”,&P);
}
if it is wrong than what is the reason ……..
11:06 pm
Hey Malli
1) Yes you can declare P there
2) Basically what your doing is your setting i to 0. Then your checking if i is less than 5 (which it will always be).
Then you’re setting P to 0 and adding i (0 + 0 = 0).
If you wanted to loop you’d do: for(int i = 0; i <= 5; i++)
8:04 am
i want the loop for
1
23
456
78910
if it is possible pls send me the code.
11:33 am
hey bhavin…soln fr ur ques is…..
#include
#include
void main()
{int m=1;
clrscr();
for(int i=0;i<=3;i++)
{ for(int j=0;j<=i;j++)
{ printf(”%d”,m);
m++;
}
printf(”\n”);
}
getch();
}
3:35 am
please give definition for while loop and do while loops
how to use
why use this
1:25 am
main()
{
int i=0;
if ( i <=5)
{
int P =0; //can i declare P here are not
P = P+i;
printf(”%d”,&P);
}
i think the answer for this pgm wil be a garbage value. bcoz u r printing &p (means, the address of P) , . if u want to know the value of P, give, printf(”%d”, p);
11:39 am
Ravindaer Here is ur answer,
for detail, contact
http://www.computermukhiya.com
LOOPING: THE while STATEMENT
The while statement is used to carry out looping operations, in which a group of statements is executed repeatedly, unit some condition has been satisfied.
The general from of the while statement is
while (expression) statement
The statement will be executed repeatedly, as long as the expression is true (i.e., as long expression has nonzero value). As with if, the expression inside the parenthesis can be any valid C expression. The statement can be simple or compound, through it is usually a compound statement. It must include some feature that eventually alters the value of the expression, thus providing a stopping condition for the loop.
THE do – while STATEMENT
When a loop is constructed using the statement described in the previous section, the test for continuation of the loop is carried out at the beginning of each pass. Sometimes, however, it is desirable to have a loop with the test for continuation at the end of each pass. This can be accomplished by means of the do – while statement.
The general from of the do –while statement is
do statement while (expression);
The statement will be executed repeatedly, as ling as the value of expression is true (i.e., is nonzero). Notice that statement will always be executed at least once, since the test for repetition does not occur until the end of the first pass through the loop. The statement can be either simple or compound; through most applications will require it to be a compound statement. It must include some feature that eventually alters the value of expression so the looping action can terminate. Note that a semicolon is present after the closing parenthesis of the expression.
For many applications it is more natural to test for continuation of a loop at the beginning rather than at the end of the loop. For this reason, the do – while statement is used less frequently than the while statement described in the previous section. For illustrative purpose, however, the programming examples shown in the previous section are repeated below using the do – while statement for the conditional loops.
Leave a Comment