## For Loop - syntax

``` 1  for (<initial statement(s)>; <Condition expression>; <Repeat step(s)>)
2  {
3    <Loop statement(s)>;
4  }```

## While Loop - syntax

``` 1  while (<Condition expression>)
2  {
3    <Loop statement(s)>;
4  }```

## Do-While Loop - syntax

``` 1  do
2  {
3    <Loop statement(s)>;
4
5  }while (<Condition expression>);```

## Important notes

Initial statement(s) - these are one or multiple statements (use comma in between). Often contains initialization of counter variables.

Repeat step(s) - these are one or multiple statements. Often contains counter operation on variables like increment, decrement etc.

Condition statement - is however only one condition statement.

Condition statement - for loop will be an infinite if it is blank.

Condition expression - while/do-while loop will be an infinite if it is a always TRUE.

Loop statement(s) - these statements are tasks in each steps. Counter variables are often used in the steps.

## Examples

``` 1  for (int i = 0; i < 10; i++)
2  {
3    printf("Iteration %d\n", i);
4  }
5
6  for (int i = 0, j = 2; i < 10; i++, j += 2)
7  {
8    printf("2 x %d = %d\n", i + 1, j);
9  }
10
11  for (; ;)
12  {
13    printf("I am infinite loop\n");
14    delay(1000);
15  }
16  ```
``` 1  int i = 0;
2  while(i < 10)
3  {
4    printf("Iteration %d\n", i);
5    i++;
6  }
7
8  while (TRUE)
9  {
10    printf("I am infinite loop\n");
11    delay(1000);
12  }```
``` 1  int i = 0;
2  do
3  {
4    printf("Iteration %d\n", i);
5    i++;
6  } while (i < 10);
7
8  do
9  {
10    printf("I am infinite loop\n");
11    delay(1000);
12  }while (TRUE)```

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

0

Similar topics related to this section