top of page

Learning Loops in C

Introduction


Say I wanted to print a message to the terminal 100 times. I could copy-paste this statement again

printf("hi\n");

again...

printf("hi\n");

and again...

printf("hi\n");

But as you can imagine, this gets tedious quickly.


Luckily, C and most other programming languages come equipped with loops, which simply repeat lines of code a number of times, thereby reducing redundancy and improving design.


Let's take a closer look at the various types of loops in C, and how they might differ from other programming languages you may have encountered.


while


while (boolean-expression)
{
	...
}

Code within the loop is repeated an indefinite number of times until the boolean expression evaluates to false.


For instance, if I wanted to count up from 1 to 100, its while loop form could look something like this:


int x = 1;
while (x <= 100)
{
	printf("%i\n", x);
	x++;
}

In this case, the boolean expression is x <= 100, which comes out to false once the variable x reaches 101.


Similarly, I could make a loop run forever by using true as a boolean expression, which is always... true.


do-while


do
{
	...
}
while (boolean-expression);

This strain of a while loop only exists in languages like C and C++, and can come in useful when you need to execute some code (usually to get the value of a variable) to then check that variable just defined in the while statement.


For instance:


do
{
	int x;
	scanf("%d", &x)
}
while (x < 0);

Here, we check for user input, and using the value stored in x, we determine whether it is negative. If so, the loop continues; if not, the loop breaks.


If we had used a traditional while, an error would be thrown as the variable x would not be initialized.


An alternative to the do-while would be simply getting user input above the while, though this would result in redundant code:


int x;
scanf("%d", &x)
while (x < 0)
{
	// scanf() called for the second time
	scanf("%d", &x)
}

for


for (initialize-variables; condition-repeatedly-checked; increment)
{
	...
}

For loops are definitely syntactically weirder than the other types, but can come in handy when repeating lines of code a definite number of times.


For instance:


for (int i = 0; i < 12; i++)
{
	printf("hi\n");
}

  • first, the variable i was initialized to 0

  • second, the condition checked again and again was if i < 12

  • third, each time the loop runs, i is incremented by 1 (i++)


You can even define multiple variables, conditions, and increments:


for (int i = 0, int j = 11; i < 12 && j >= 0; i++, j--)
{
	printf("%i %i\n", i, j);
}

Final Thoughts


As a final review:


while

  • used to repeat a block of code an indefinite number of times, but possibly zero times

do-while

  • used to repeat a block of code an indefinite number of times, but at least once

for

  • used to repeat a block of code a definite number of times


Hopefully now you won't need to resort to copy pasting...



Thanks for reading!

Comments


bottom of page