Learn C programming tutorial lesson 4 - Loops
What are loops
Sometimes you will want to do something a lot of times. An example would be printing a character at the beginning of each line on the screen. To do this you would have to type out 24 printf commands because there are 25 rows on the screen 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 basic types of loops which 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 increases by a specified number each time. This loop is best suited for the above problem because we know exactly how many times we need to loop for. The for loop uses the following structure:
for (starting number;ending condition;increase variable)
command;
With loops you also have to put commands between curly brackets if there is more than one of them. Here is the solution to the problem that we had with the 24 printf commands:
#include<stdio.h>
int main()
{
int i;
for (i = 1;i == 24;i++)
printf("H\n");
return 0;
}
In the above example I used i++ which is the same as using i = i + 1. This is called incrementing. i++ adds 1 to i and i-- subtracts 1 from i. You can also say ++i or --i and the difference is that with ++i the 1 is added before the for loop tests if i == 24 and with i++ 1 is added to i after the loop has tested if i == 24.
The while loop
The while loop is different from the for loop because 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 increment the variable inside the loop. Here is an example of a while loop that runs the amount of times that the user enters:
#include<stdio.h>
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 except that it tests the condition at the bottom of the loop.
#include<stdio.h>
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 because a condition has been met other than the loop end condition.
#include<stdio.h>
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 incrementing the loop variable again. The following example will never print "Hello" because of the continue.
#include<stdio.h>
int main()
{
int i;
while (i < 10)
{
i++;
continue;
printf("Hello\n");
}
return 0;
}
- Learn C programming tutorial lesson 1 - Hello World
- Learn C programming tutorial lesson 2 - Variables and constants
- Learn C programming tutorial lesson 3 - Decisions
- Learn C programming tutorial lesson 4 - Loops
- Learn C programming tutorial lesson 5 - Pointers
- Learn C programming tutorial lesson 6 - Arrays
- Learn C programming tutorial lesson 7 - Strings
- Learn C programming tutorial lesson 8 - Functions
- Learn C programming tutorial lesson 9 - Structures
- Learn C programming tutorial lesson 10 - Text and data files