C Tutorial

C For Loop

for packs init, condition, and step in one line. Use it when you know how many times to repeat.

Three parts in one line

A for loop has three slots separated by semicolons: initialize, condition, step. The initialize runs once. Then C checks the condition, runs the body, runs the step, and checks again, until the condition is false.

That is the same three jobs a while loop has, written where you can see them together. Usefor when the number of passes is known: 0 to n minus one, 1 to 10, every index in an array.

A complete for program

Declare the index in the init slot: int i = 0. That C99 form is the style this tutorial uses.i < 5 is the test. i = i + 1 is the step. The values printed are 0, 1, 2, 3, 4 — five times, not six.

Example

#include <stdio.h>

int main(void) {
  for (int i = 0; i < 5; i = i + 1) {
    printf("%d\n", i);
  }
  return 0;
}

Run this in /c/try. Counting from 0 is the usual C habit because array indexes start at 0.

Read the header left to right

SlotThis exampleWhen it runs
Initint i = 0Once, before the first check
Conditioni < 5Before every pass, including the first
Stepi = i + 1After each pass, before the next check

i++ means the same as i = i + 1 here. You will see ++ in other people's code. Either form is fine for a counter.

The variable declared in init exists only inside the loop. After the closing brace, i is gone.

Count the other way

Start high and subtract if you need a countdown. The condition still has to become false. Herei goes 5, 4, 3, 2, 1, then i >= 1 fails.

Example

#include <stdio.h>

int main(void) {
  for (int i = 5; i >= 1; i = i - 1) {
    printf("%d\n", i);
  }
  printf("done\n");
  return 0;
}

Walk a list with an index

C has no range-for. To visit every slot of a fixed array, write a classic for withint i = 0 and stop at i < n. Arrays are the next chapters; this is enough to print a short list.

Example

#include <stdio.h>

int main(void) {
  int nums[3] = {2, 4, 6};
  for (int i = 0; i < 3; i = i + 1) {
    printf("%d ", nums[i]);
  }
  printf("\n");
  return 0;
}

Off-by-one

i < n runs n times when i starts at 0. i <= n runs n + 1 times. Both are valid; they mean different things. For an array of length n, the last valid index is n minus one, so the test is i < n.

Next: break and continue — leaving a loop early, or skipping one pass.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

A drop, second by second

Ticker-tape and strobe photos show position at equal time steps. From rest, s = ½ g t². At t = 1, 2, 3, 4 s the distances are about 4.9, 19.6, 44.1, 78.5 m. The gaps grow because the object is speeding up.

A for loop is the natural fit when you know the times in advance. Change g and the table is a different planet.

s(t) = ½ g t²

Example

#include <stdio.h>

int main(void) {
  const double g = 9.81;
  int t;
  for (t = 1; t <= 4; t++) {
    double s = 0.5 * g * t * t;
    printf("t = %d s, s = %.2f m\n", t, s);
  }
  return 0;
}

Maths

Sum of squares

1² + 2² + … + 10² is 385. The closed form n(n+1)(2n+1)/6 gives the same 385 for n = 10. The loop is how you check the formula before you trust it on n = 1000.

Adding n * n each time is the definition. Off-by-one errors show up here: i <= 10 versus i < 10 is a 100 missing at the end.

Σ k² = n(n+1)(2n+1)/6

Example

#include <stdio.h>

int main(void) {
  int n;
  int sum = 0;
  for (n = 1; n <= 10; n++) {
    sum += n * n;
  }
  printf("sum of squares = %d\n", sum);
  return 0;
}

FAQ: C For Loop

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c for loop examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c for loop in this C C lesson (C For Loop).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.