My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. 1 Comment on Control Statements in C

In C, until so far, in all the programs, the control is flowing from one instruction to next instruction. Such flow of control from one instruction to next instruction is known as sequential flow of control. But, in most of the C programs, while writing the logic, the programmer might want to skip some instructions or repeat a set of instructions again and again. This is can be called as non-sequential flow of control. The statements in C, which allows the programmers to make such decisions, are known as decision making statements or control statements.

In C, there are two types of decision making statements. One type is used to branch the control into different ways and the other type is used to repeat a set of instructions again and again. The two types of decision making statements are:

  1. Selection statements or Branching statements
  2. Looping statements

 

Selection Statements or Branching Statements

The selection statements in C enable the programmer to select a set of instructions to be executed by the CPU. This selection is based on a condition. C also supports a set of unconditional branching statements which transfers the control to some other place in the program. The selection statements in C are:

  1. if statement
  2. switch statement
  3. Conditional operator statement
  4. goto

 

if Statement

The if statement allows the programmer to select a set of instructions to be executed, based on a condition. If the condition is evaluated to true, then one set of instructions will be executed or if the condition is evaluated to false, another set of instructions will be executed. The general from of if statement is as shown below:

if-control-flow

There are four variations of the if statement. They are:

  1. Simple if or null else
  2. if…else
  3. Nested if
  4. else if Ladder

Simple if or null else

The simple if allows the programmer to execute or skip a set of instructions based on the value of a condition. The simple if is a one way selection statement. If the condition is true, a set of statements will be executed. If the condition is false, the control will continue with the next statement after the if statement. The simple if can be represented diagrammatically as shown below:

simple-if

Syntax for simple if is as shown below:

if-syntax

if…else Statement

The if…else is a two way decision making selection statement. If the condition evaluates to true, one set of instructions will be executed. If the condition evaluates to false, another set of instructions will be executed. The if…else statement can be represented diagrammatically as shown below:

if-else

Syntax of if…else statement is as shown below:

if-else-syntax

Nested if

The nested if can also be called as cascaded if. In nested if statement, one if statement is nested within another if statement. A nested if statement can be used as an alternative to logical AND operator. If the condition is evaluated to true in the first if statement, then the condition in the second if statement is evaluated and so on. The nested if statement can be represented diagrammatically as shown below:

nested-if-cf

Syntax of nested if statement is as shown below:

nested-if-syntax

else if Ladder

The else if ladder in C is a multi way selection operator. It allows the programmer to select one set of instructions among various other sets of instructions. The else if ladder is similar to the logical OR operator. If first condition is true, then corresponding set of instructions will be executed. If the condition is false, then the next condition is checked and so on. If all the conditions fail, the statements in the default block will be executed. The else if ladder can be represented diagrammatically as shown below:

else-if-ladder

Syntax of else if statement is as shown below:

else-if-ladder-syntax

 

switch Statement

Although, C provides a multi way selection statement like else if, when the number of conditions increases, the program will become less readable. To solve this problem, C provides an easy to understand multi way selection statement called switch statement. The switch statement to easy to understand when there are more than 3 alternatives. The switch statement can be represented diagrammatically as shown below:

switch-cf

As seen in the above diagram, the switch statement switches between the blocks based on the value of the expression. Each block will have a value associated with it. The expression in the switch statement must always reduce to an integer value. So the expression in the switch statement can be either an integer value or a character constant or an expression which reduces to an integer value. The label for each block can be either an integer value or a character constant.

Syntax for the switch statement is as shown below:

switch-syntax

As seen in the above syntax, each block is represented using the case keyword and the case keyword follows with the label of the block. In a switch statement, both the default block and the break statement are optional. If none of the blocks are matched, then the statements in the default block are executed. Every block is ended with a break statement. If we remove the break statement from a particular block, all the subsequent blocks are also executed until the next break statement is encountered.

 

Conditional Operator Statement

C language provides an unusual operator called conditional operator which is represented as ? : . The conditional operator is a two way selection operator. The syntax for conditional operator statement is as shown below:

conditional-syntax

As shown in the above syntax, when the condition evaluates to true, expr1 is executed. Otherwise, if the condition is false, expr2 is executed. The conditional operator statement can be represented diagrammatically as shown below:

conditional-cf

Every conditional operator statement can be converted into an if…else statement. But the vice versa is not true.

 

goto Statement

Unlike other selection or branching statements that we have seen so far which branches based on a condition, the goto statement branches unconditionally. That is why the goto statement is also referred to as unconditional jump statement. There are two more unconditional branch statements in C. They are: break and continue. We have already seen the break statement in switch statement. But both break and continue are extensively used inside loops. So, we will discuss about these two unconditional branch statements later. By using the goto branch statement, we can either skip some instructions and jump forward in the program or jump back and again repeat a set of instructions. So there are two ways in which we can use the goto statement. They are:

  1. Forward jump
  2. Backward jump.

Syntax of forward jump and backward jump is as shown below:

goto-syntax

As shown in the above syntax, if the label is after the goto statement, then it is known as forward jump and if the label is before the goto statement, it is known as backward jump.

 

Decision Making Looping Statements

While writing C programs, the programmer might want a set of instructions to be repeated again and again until some condition is satisfied. For this purpose, C provides decision making looping statements. The looping statements provided by C are:

  1. while
  2. do…while
  3. for

All the looping statements in C essentially consist of two parts namely: control statement and body of the loop. The control statement decides when the loop will be stopped and the body of the loop contains the instructions that are to be repeated. Based on where the control statement is placed in the loop, the looping statements are categorized to two categories. They are:

  1. Entry controlled loops
  2. Exit controlled loops

 

Entry controlled loops

The looping statements in which the control statement is placed before the body of the loop are known as entry controlled loops. Ex: while and for loops. In entry controlled loops, the condition is checked first and if the value is true, then the body of the loop is executed. Otherwise the body is never executed. An entry controlled loop can be represented diagrammatically as shown below:

entry-controlled-loop

 

Exit controlled loops

The looping statements in which the control statement is placed after the body of the loop are known as exit controlled loops. Ex: do…while loop. In exit controlled loops, the body of the loop is executed once and then the condition is checked. If the value is true, the body of the loop is executed again. Otherwise, the execution of the loop stops. An exit controlled loop is represented diagrammatically as shown below:

exit-controlled-loop

Based on the nature of the loops, the loops can be categorized into two types namely:

  1. Definite loops (Ex: for)
  2. Indefinite loops (Ex: while and do…while)

Definite loops: If the programmer exactly knows how many times he/she is going to repeat the set of instructions (loop), such loops are known as definite loops.

Indefinite loops: If the programmer does not know exactly how many times he/she is going to repeat the set of instructions (loop), such loops are known as indefinite loops.

The variable used in the condition inside of a definite loop is known as a counter and such loops are also known as counter controlled loops. The variable used in the condition inside of a indefinite loop is known as sentinel and such loops are also known as sentinel loops.

 

while Loop

In C, the while loop is an entry controlled loop. The body of the while loops is only executed when the condition evaluates to true. If the condition evaluates to false, the body of the loop is not executed. The while loops are generally used when there is a need for repeating a set of instructions for indefinite amount of times. The syntax of a while loop is as shown below:

while-loop-syntax

 

do…while Loop

In C, the do…while loop is an exit controlled loop. The body of the do…while loop is executed first and then the condition is evaluated. If the value is true, the body of the loop is executed again and if the value is false, the execution of the body of the loop stops. The difference between while and do…while is, unlike the while loop, the body of the do…while loop is guaranteed to be executed atleast once (>=1). The syntax of the do…while loop is as shown below:

dowhile-syntax

 

for Loop

In C, the for loop is an entry controlled loop. The for is generally used while implementing definite loops in C programs. The for loop’s syntax is a little bit different from the other loops. In the syntax of the for loop, first the counter is initialized, and then the condition is evaluated. If the value of the condition is true, the body of the for loop is executed. Otherwise, the body of the loop is not executed. After the execution of the for loop’s body, the counter is either incremented or decremented. Then the condition is evaluated again and so on. The syntax of the for loop is as shown below:

for-loop-syntax

The flowchart of the for loop is as shown below:

for-loop-cf

 

break and continue

C provides two unconditional branching statements which are extensively used inside the looping statements. They are:

  1. break
  2. continue

break Statement

The break statement is used inside the looping statements to break the execution of the loop. When the break statement is encountered inside the loop, the execution of the body of the loop stops and the control is given to the next instruction after the body of the loop. The syntax of the break statement is as shown below:

break-syntax

continue Statement

The continue statement is used inside the looping statements to skip the execution of a set of instructions and return the control back to the loop. When a continue statement is encountered within the body of the loop, the statements after the continue statement are skipped and the control is passed back to the loop. The syntax of the continue statement is as shown below:

continue-syntax

 

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

Related Links:

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

Very well written material. Could it be possible to provide additional notes on recursion, linked lists and structures. Whether you do send what I have requested or not my impression remains that the notes so far provided are excellent.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up