OneBite.Dev - Coding blog in a bite size

do a for loop in C

Code snippet on how to do a for loop in C

for (int i = 0; i < 10; i++) 
  {
    printf("Hello world! %d\n", i);
  }

This code will print “Hello world!” ten times, each with a number that increases by one each time. The code uses a for loop, which is a type of loop that repeats a set of instructions until an end condition is reached. This particular loop has three parts. The first part, int i = 0, sets up a counter that starts at 0. The second part, i < 10, creates a condition that the counter must stay less than 10 while the loop is running. The last part, i++, increases the counter by 1 each time the loop runs. The body of the loop, between the curly brackets {}, will run a set of instructions every time the loop runs. In this case, it uses the printf function to print the message “Hello world!” plus the value of the counter, which is i. This loop will run ten times and each time it runs the counter will increase, until it finally reaches 10, which is the condition that was set up in the second part of the loop.

c