OneBite.Dev - Coding blog in a bite size

use a basic loop in C

Code snippet on how to use a basic loop in C

  for (int i = 0; i < 10; i++){
    printf("This loop has run %d times\n", i);
  }

This code shows how to use a basic loop in C. The loop will run ten times. The line of code beginning with “for” is the loop header. It starts by declaring a variable named “i” and giving it an initial value of 0. The condition after the semi-colon “i < 10” specifies that the loop will continue to run until “i” is no longer less than 10. The third line in the header after the second semi-colon “i++” increases the value of “i” by one each time the loop repeats. The next line of code, “printf(“This loop has run %d times\n”, i);”, prints out a message with the value of “i”. This will tell us how many times the loop has run. Once the value of “i” reaches 10, the condition in the loop header will no longer be true and the loop will terminate.

c