Loops in C Programming Language


A loop in a program consists of two parts, one is called the body of the loop and the other is called the control statement. The control statement performs a logical test whose result is either true or false.
Loops in c
If the result of the logical test is true, then the statements contained in the body of the loop are executed. Otherwise, the loop is terminated. The loop is used in c programming to repeat the statements or numbers repeatedly to the given limis.
  1. Array
  2. Strings
  3. Structures
C provides the following types of loop control statements:
  • While Statement
  • Do-While Statement
  • For Statement
  • Break Statement
  • Continue Statement

1. While statement:- 

While statement is used to execute a set of statements repeatedly, as long as the specified condition is true.
The syntax of a while loop is as follows:

Example Of While Loop:-

2. Do-While Statement:-  

This is used to execute a set of statements repeatedly, until the logical test results in false. This is called the post-test loop, because, the test for repetition is made at the end of each pass.
The flowchart of Do-while statement is Showing blow:-
The syntax of do-while is as follows:

Exaample Of Do-While:-

3. For Statement:- 

For statement is used when the programmer knows how many times a set of statements are to be executed.
The syntax of a for statement is as follows:

Example Of For Statement:-

4. Break Statement:- 

The break statement is used to terminate loops or to exit from a switch statement. It can be used within for, while, do-while or switch statement.
The general syntex of the break statement is:
A break used in a switch statement will affect only that switch.
Read Also:
In a loop statement when break is encountered, the loop is immediately terminated, and program control resumes at the next statement following the loop.

For Example:-

The above program prints the number 0 to 10 on the sereen. Then the loop terminates because break causes immediate exit from the loop, overriding the conditional test a<100.

5. Continue Statement:- 

The continue statement is used for the next iteration of the loop to take place, skipping any code in between. Hence, it is used to terminate the current iteration and continue with the next iteration of the loop.
The general syntex of continue statement is:
For the loop, continue causes the updation and then the conditional test portions of the loop to execute. For the while and do-while loop, program control passes to the conditional tests. 
For example:

Post a comment