In programming, loops are used to replicate a block of code over and over until a specified condition is met. The loop begins with its parameters, inside the parameters is the condition of the loop, and inside the scope (the {}) is the block of code to be repeated.
#include <stdio.h>
int main () {
int a = 0;
/* while loop execution */
while( a < 100 ) {
printf("value of a: %d\n", a);
a++;
}
return 0;
}
This code will repeat to print out all of the digits from one to 100. A begins at 0, and each time the loop executes, 1 is added until a is eventually larger than 100, the condition in the parameters is met, and the code stops.
There are many times in which you may wish to preform a loop, mostly if you wish to preform a task over and over again. This is particularly useful for games, perhaps you wish the player to move forward while the direction button is held down, or to fall while not touching the ground. However you need to be sure to give some way of exiting the loop, here we did this by counting how many times the loop executed by adding 1 to a variable each time the loop is complete, until it no longer met its condition.
Top Tip!
Remember the ++ is a short hand of writing a = a +1. Another shorthand is += where a += 2 will add 2 every time the loop is ran.
Be careful however not to get stuck in an infinite loop. This can sometimes be useful, but more often than not. No doubt you have been playing a game before, and it has become stuck in an infinite loop before crashing, this is why.
#include <stdio.h>
int main () {
/* while loop execution */
while( 1 == 1 ) {
printf("Never Ending Story");
}
return 0;
}
Here 1 will always be equal to 1, and thus this loop will never return 0.
Write a while loop that lists the times table of any given number, up to 10.
#include <stdio.h>
int main()
{
//Declare Variables
int a = 2 ;
int b = 0 ;
int c = 0 ;
//Prompt user input and gain a number
printf("Input a number to find it's times table") ;
scanf("%d", &b) ;
// While a is les than or equal to 10, loop trough and print the times table
while (a <= 10)
{
c = b * a ;
printf("%d\n", c) ;
++a ;
}
return 0;
}
The simplest difference between a while loop and a do while loop, is that a while loop checks it’s condition first, where as a do while loop checks the condition after the loop has executed, it is guaranteed to execute at least one time.
#include <stdio.h>
int main () {
/* Declare Variables */
int a = 10;
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a <= 20 );
return 0;
}
Rewrite the code from exercise one, but this time, use a do while loop instead.
This tutorial was short and sweet, loops are a core concept, but a simple one to try to understand. As always, try experimenting with your code and see what you can challenge yourself to make.
This course is provided for free, as we at Devoke believe all education should be. However, if you enjoy the course, we are always grateful for support and donations to Patreon to ensure we can continue providing resources for free.