icp/oer/courses/c-basics/sections/02-basic-language-features/04-loops/program.c

40 lines
1.0 KiB
C

#include <stdio.h>
int main(int argc, const char *argv[])
{
// There are 3 basic types of loops
int i = 0;
// In the while loop the condition is checked before each execution
// on true the block is executed
// As a result the block is executed as long as the condition is met
while (i < 10)
{ // condition i < 10
i++; // increment i
}
// The do while loop works similarly to the while loop.
// But the block is executed at least once.
// Since the condition is only checked after the first execution.
do {
i++;
} while (i < 20);
// for loops provide an easy shorthand way to define conditions
for (int j = 0; j < 20; j += 5)
{
// continue skips the current iteration
// subsequent iterations may work as normal.
continue;
printf("This is never reached.");
}
for (;;)
{
// Break exits any loop for good
break;
}
// TODO: print the numbers 1 to 10 using a for loop.
}