C Loops

while, do-while and for.

✍️ 2 exercises in this lesson
  • while (condition) { }
  • do { } while (condition);: runs at least once
  • for (int i = 0; i < n; i++) { }

break leaves a loop and continue skips to the next iteration. Loops can be nested.

Key Takeaways: C Loops

Mastering C Loops helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C Loops Code Example

C
#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        printf("%d x 3 = %d\n", i, i * 3);
    }

    int n = 1;
    while (n < 100) n *= 2;
    printf("First power of 2 >= 100: %d\n", n);

    for (int row = 1; row <= 3; row++) {
        for (int col = 1; col <= row; col++) printf("*");
        printf("\n");
    }
    return 0;
}
Output
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
First power of 2 >= 100: 128
*
**
***
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C Loops Practice Exercises

Check what you learned in “C Loops”.

/ 2 solved
Fill in the blank

Write a for loop header that counts from 0 to 4.

for (int i = 0; i  5; i++)
✓ Correct! Not quite. Try again.
Multiple choice

Which loop checks its condition after running once?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services