What are loops
Sometimes you will want to do something a lot of times. An example would be printing a
chara
cter at the beginning of ea
ch line on the s
creen. To do this you would have to type out 24 printf
commands be
cause there are 25 rows on the s
creen and the 25th row will be left blank. We
can use a loop to do this for us and only have to type 1 printf
command. There are
3 basi
c types of loops whi
ch are the for loop, while loop and do while loop.
The for loop
The for loop lets you loop from one number to another number and in
creases by a spe
cified number ea
ch time. This loop is best suited for the above problem be
cause we know exa
ctly how many times we need to loop for. The for loop uses the following stru
cture:
for (starting number;loop condition;increase variable)
command;
With loops you also have to put
commands between
curly bra
ckets if there is more than one of them. Here is the solution to the problem that we had with the 24 printf
commands:
#include
int main()
{
int i;
for (i = 1;i <= 24;i++)
printf("H\n");
return 0;
}
A for loop is made up of
3 parts inside its bra
ckets whi
ch are separated by semi-
colons. The first part initializes the loop variable. The loop variable
controls and
counts the number of times a loop runs. In the example the loop variable is
called i and is initialized to 1. The se
cond part of the for loop is the
condition
a loop must meet to keep running. In the example the loop will run
while i is less than or equal to 24 or in other words it will run 24
times. The third part is the loop variable in
crementer. In the example i++ has been used whi
ch is the same as saying i = i + 1. This is
called in
crementing. Ea
ch time the loop runs i has 1 added to it. It is also possible to use i-- to subtra
ct 1 from a variable in whi
ch
case it's
called de
crementing.
The while loop
The while loop is different from the for loop be
cause
it is used when you don't know how many times you want the loop to run.
You also have to always make sure you initialize the loop variable
before you enter the loop. Another thing you have to do is in
crement the variable inside the loop. Here is an example of a while loop that runs the amount of times that the user enters:
#include
int main()
{
int i,times;
scanf("%d",×);
i = 0;
while (i <= times)
{
i++;
printf("%d\n",i);
}
return 0;
}
The do while loop
The do while loop is the same as the while loop ex
cept that it tests the
condition at the bottom of the loop.
#include
int main()
{
int i,times;
scanf("%d",×);
i = 0;
do
{
i++;
printf("%d\n",i);
}
while (i <= times);
return 0;
}
Break and continue
You
can exit out of a loop at any time using the break statement. This is useful when you want a loop to stop running be
cause a
condition has been met other than the loop end
condition.
#include
int main()
{
int i;
while (i < 10)
{
i++;
if (i == 5)
break;
}
return 0;
}
You
can use
continue to skip the rest of the
current loop and start from the top again while in
crementing the loop variable again. The following example will never print "Hello" be
cause of the
continue.
#include
int main()
{
int i;
while (i < 10)
{
i++;
continue;
printf("Hello\n");
}
return 0;
}
Posted in: C,learning programming,Programming
Email This
BlogThis!
Share to Facebook
0 comments:
Post a Comment