control flow in c

control flow in c language is similar to control flow in other languages.

The program is a set of instructions. Each instruction of the program is executed by the processor. In addition, the processor executes instructions one by one. Whatever you will write in your program will be executed in the same order as they appear in the program.

So, its clear that processor executes an instruction in a sequential fashion. At a particular instant processor is executing some line of code, means control of the processor is on that line. Processor’s control moves from one line to another. This movement of the processor is called a control flow in c language.

Types of control flow in c

  • Decision control flow or Decision making
  • Iterative control flow or looping
  • Switch case-control flow
  • control statements (break, continue, goto)

Decision control instruction

Decision control instruction or decision making is also called as decision control flow in c language. It is also known as selection control instruction. The main function of this control instruction is to select a set of code for execution on the basis of some condition.

Types of decision control flow in c:

  • if
  • if-else
  • conditional operator
  • nested if-else (You may consider)

Iterative Control Instruction

Iterative control instruction, mostly known as the loop is also called as iterative control flow in c language. It is also called repetitive control instruction. Sometimes, it is desirable to execute the same statement again and again.

There are three ways to implement loops in c

  • while
  • do-while
  • for

Switch Case Control Instruction

Switch case control is used when you are making a program where multiple choices are involved. Switch transfers the control to a case written in its body depending upon the value evaluated by expression in switch parenthesis.

In switch body, each case is postfixed by a constant. This constant could be an integer or a character but not real. It is important to note that the constant in each case must be distinct.

When the value of expression does not match with any case constant then control flow moves on the default segment. In addition, once control moves from a switch to an appropriate case, it will not only execute statement written in that case but also execute all the statements in the following cases including default. However, it will not execute the cases written above that particular case.

If you want to execute only one case execution, you must use the break keyword at the last statement of each case.

Have You enjoyed the article on control flow in c!

Goto Control Instruction

The goto statement is used for the unconditional jump from one part of the program to another part of the program. However, it is always suggested not to use goto statement because this reduces the readability of the program. I would recommend you to hardly or not use goto statement because the use of goto statement is considered as a poor programming approach.

The goto is a keyword that moves control to a location in the program mentioned by location name ‘label’. In addition, goto statement can move the control in forward as well as backward in the program. It’s important to note that goto works only within the same function body.

<<Prev                                                                                                                Next>>


Your friends are waiting for this content. Please Share To Aware Them!