Loops in C Language

Controlling Program Execution

The default order of execution in a C program is top-down. Execution starts at the beginning of the main() function and progresses, statement by statement, until the end of main() is reached. However, this sequential order is rarely encountered in real C programs. The C language includes a variety of program control statements that let you control the order of program execution. You have already learned how to use C’s fundamental decision operator, the if statement, so now it’s time to explore three additional control statements you will find useful:

1) The for statement.

2) The while statement.

3) The do…while statement.

for loop syntex in C

  //syntex for loop
  for ( initial; condition; increment )

while loop syntex in C

//syntex while loop
while (condition)

do while loop syntex in C

  //syntex do while loop
  while (condition);

Why do we use loops in C?

C – for loop in C programming with example. A loop is used for executing a block of statements repeatedly until a given condition returns false.

loops work in C language?

  1. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. …
  2. Next, the condition is evaluated. …
  3. After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement. …
  4. The condition is now evaluated again.

Why use a while loop instead of a for loop?

A for loop runs for a pre-determined number of times. So, it can iterate through an array from start to finish, say, but it will only ever loop for that specific number. A while loop will carry on until a pre-determined scenario takes place. That may happen immediately, or it may require a hundred iterations.