Fork me on GitHub

Project Notes

Loops in C

All about loops in C



While Loops

The while construct has a guard condition tested on entry to loop:

while (condition) {

Do-While Loops

The do .. while construct has the guard condition tested at the end of the loop:

do {
} while (condition);

For loop

The for construct is a super-charged while loop, with embedded initialization and stepping of the loop control variable:

for (initial value; condition; incrementation or decrementation )  {

Break Statement

A break statement in a loop will immediately exit the loop.

Continue Statement

A continue statement in a loop will immediately end the current loop and skip to the next iteration.

Running the Examples

See example.c for details. It runs simple tests of all combinations of syntax. A makefile compiles and runs:

$ make
gcc -Wall -O0    example.c   -o example

===== test_while

===== test_break_while

===== test_continue_while

===== test_do_while

===== test_break_do_while

===== test_continue_do_while

===== test_for

===== test_break_for

===== test_continue_for

Credits and References

About LCK#165 c
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.