For Loop - syntax

for (<initial statement(s)>; <Condition expression>; <Repeat step(s)>)
{
  <Loop statement(s)>;
}

While Loop - syntax

while (<Condition expression>)
{
  <Loop statement(s)>;
}

Do-While Loop - syntax

do
{
  <Loop statement(s)>;
}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

for (int i = 0; i < 10; i++)
{
  printf("Iteration %d\n", i);
}
for (int i = 0, j = 2; i < 10; i++, j += 2)
{
  printf("2 x %d = %d\n", i + 1, j);
}
10 
11  for (; ;)
12  {
13    printf("I am infinite loop\n");
14    delay(1000);
15  }
16 
int i = 0;
while(< 10)
{
  printf("Iteration %d\n", i);
  i++;
}
while (TRUE)
{
10    printf("I am infinite loop\n");
11    delay(1000);
12  }
int i = 0;
do
{
  printf("Iteration %d\n", i);
  i++;
} while (< 10);
do
{
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.

 Vote 0

Similar topics related to this section

switch case, compare with ==, execution sequence, loop statements, for, while, do-while loop syntax, for to while, do-while and while usage, infinite loops, break and continue,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan