Control Statement and Looping in C


Control Statement

 

Generally, the execution of the program is in the sequence of the written statements. That is, in the order in which the statements are written, they are executed in the same order and they are executed only once.

The program written in this way is called a sequential control structure program. It is easy to understand and improve them, that is, to make some changes in them.

But to write big programs, many times a decision has to be made on the basis of condition or the same statement has to be executed multiple times, then other types of statements are used in the program.

Programming language 'C' provides many statements to do this type of work. These statements are called decision statements and looping statements.

 

Statement

Statements are a type of instructions that interrupt the computer to perform various types of tasks. In the programming language 'C', a semicolon (;) sign is placed at the end of each statement.

The curly braces { } are used to write more than one statement in one unit. The statements written in this way are called compound statements. like -

 

{
    statement 1;
    statement 2;
           :
}   statement n;

 

The above statement is an example of a compound statement.

 

Flow Control Statements

 

Statements in the program can be executed in the following three ways.

 

  1. Sequential Statements
  2. Selection Statements
  3. Looping Statements

 

All three types of instructions are included in every computer language.

 

Sequential Statements

 

These are general statements of the program which are executed one by one in the top to bottom order.

 

Selection Statements

 

A Selection Statement is a part of a program in which instructions are executed on the basis of one condition or more than one condition.

In the programming language 'C', there are four types of decision statements –

 

  1. If statement
  2. Switch statement
  3. Conditional operator statement (? :)
  4. Goto statement

 

Looping Statements

 

Looping is the repeated execution of a part of the program until a particular condition is met in the program. It can be understood that when we execute more than one statement more than once, then we use looping in the program. By this, the repeated execution of any part of the program continues as long as the given condition is true.

 

Generally, there are two types of loops -

 

  1. Entry Controlled Loop
  2. Exit Controlled Loop

 

These can be understood through the diagram as follows –

 

Control Statement

 

It is clear from the above diagram that, In an entry-controlled loop, the condition is checked first or before the loop starts.

Whereas in an exit-controlled loop the condition is checked once the loop is executed.

Programming language 'C' provides three different types of loops -

 

  1. For loop
  2. While loop
  3. Do—while loop