100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C

Loops in C (for, while, do-while)

Complete guide to C loops — for, while, and do-while — with syntax, execution order, examples, and exam-focused comparisons.

Operators & Control FlowBeginner12 min readJul 7, 2026
Analogies

1. Introduction

Loops let a C program repeat a block of statements without duplicating code, driven by a condition that determines when repetition should stop. C provides three looping constructs — for, while, and do-while — which differ mainly in when the controlling condition is checked and how the initialization/update steps are organized.

🏏

Cricket analogy: A loop is like a bowler repeating the same run-up and delivery action over after over without rewriting instructions each time; for, while, and do-while are like three different net-practice routines that differ in when the coach checks if you should bowl another ball.

Choosing the right loop improves readability: for is preferred when the number of iterations is known or counter-driven, while is preferred for condition-driven repetition where the iteration count isn't known in advance, and do-while is used when the loop body must run at least once regardless of the condition.

🏏

Cricket analogy: Use a for loop like bowling a fixed six-ball over (known count); use a while loop like batting until you get out (condition-driven, unknown balls); use a do-while loop like a toss that must happen once before deciding who bats first.

2. Syntax

2.1 for loop

c
for (initialization; condition; update) {
    statement(s);
}

2.2 while loop

c
while (condition) {
    statement(s);
}

2.3 do-while loop

c
do {
    statement(s);
} while (condition); // note the trailing semicolon

3. Explanation

3.1 for loop

The for loop bundles initialization, condition check, and update into one header, executed in this order: initialization runs once; the condition is checked; if true, the body runs, then the update executes, and the condition is checked again — repeating until the condition becomes false. Because the condition is checked BEFORE the first iteration, a for loop's body can execute zero times if the condition is initially false. Any of the three header components can be omitted (e.g. for (;;) creates an infinite loop), and the comma operator can initialize or update multiple variables.

🏏

Cricket analogy: The for loop is like a set over where you first set the field (init), check if there are balls left (condition), bowl the ball (body), then update the over count; if the umpire calls no balls left before bowling even starts, zero deliveries happen, and an unbounded practice session, like the empty for(;;), bowls forever.

3.2 while loop

The while loop checks its condition BEFORE each iteration, including the first. If the condition is false at the very start, the body never executes. while is an entry-controlled (pre-tested) loop, best suited when the number of repetitions depends on a condition evaluated at runtime rather than a fixed counter, such as reading input until a sentinel value is seen.

🏏

Cricket analogy: A while loop is like a batsman checking the scoreboard before every ball to see if the target is reached; since the check happens before facing the first ball, an already-met target means zero balls are needed — ideal for chasing a total where the number of balls isn't fixed in advance.

3.3 do-while loop

The do-while loop checks its condition AFTER executing the body, making it an exit-controlled (post-tested) loop. Consequently, the body of a do-while always executes at least once, even if the condition is false from the start. This makes do-while ideal for menu-driven programs where an action (like displaying a menu) must happen at least once before checking whether to repeat. Note the mandatory trailing semicolon after while (condition) in a do-while — forgetting it is a compile-time syntax error.

🏏

Cricket analogy: A do-while loop is like a coin toss that always happens once before the umpire checks whether extra overs are needed — the toss (body) runs at least once regardless of the game situation, just as a menu-driven scorer app always shows the menu once before checking if the user wants to continue; forgetting the closing semicolon is like forgetting to record the toss result.

break immediately terminates the nearest enclosing loop (or switch), transferring control to the statement right after it. continue skips the remaining statements in the current iteration and jumps to the next iteration's condition check (for while/do-while) or to the update step (for for). Both work identically across all three loop types.

A common exam trap is confusing when the condition is tested. For identical-looking bodies, while and for can execute the body zero times, but do-while always executes it at least once. Also watch for infinite loops caused by forgetting to update the loop control variable inside a while/do-while body — the for loop's structure makes this mistake slightly less likely because the update is visible in the header.

4. Example

c
#include <stdio.h>

int main(void) {
    // for loop: print 1 to 5
    printf("for: ");
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    printf("\n");

    // while loop: sum digits of a number
    int n = 1234, sum = 0;
    printf("while digit sum: ");
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    printf("%d\n", sum);

    // do-while loop: runs at least once even though condition is false
    int count = 10;
    printf("do-while: ");
    do {
        printf("%d ", count);
        count++;
    } while (count < 5);   // false immediately, but body already ran once
    printf("\n");

    // break and continue demo
    printf("break/continue: ");
    for (int i = 1; i <= 10; i++) {
        if (i == 7) break;       // stop the loop entirely at 7
        if (i % 2 == 0) continue; // skip even numbers
        printf("%d ", i);
    }
    printf("\n");

    return 0;
}

5. Output

text
for: 1 2 3 4 5 
while digit sum: 10
do-while: 10 
break/continue: 1 3 5 

6. Key Takeaways

  • for combines initialization, condition, and update in one header; ideal for counter-driven, known-iteration-count loops.
  • while checks its condition before every iteration (entry-controlled) and may run zero times.
  • do-while checks its condition after the body runs (exit-controlled) and always runs at least once.
  • do-while requires a trailing semicolon after the while(condition) clause — a frequent syntax-error trap.
  • break exits the nearest enclosing loop entirely; continue skips to the next iteration.
  • Forgetting to update the loop-control variable inside while/do-while bodies is a common cause of infinite loops.

Practice what you learned

Was this page helpful?

Topics covered

#CProgrammingStudyNotes#Programming#LoopsInCForWhileDoWhile#Loops#While#Syntax#Loop#StudyNotes#SkillVeris#ExamPrep

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse