This article provides a comprehensive overview of control statements in C programming with their syntax and control flowcharts.
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:
- Selection statements or Branching statements
- 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:
- if statement
- switch statement
- Conditional operator statement
- goto
Contents
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:
There are four variations of the if statement. They are:
- Simple if or null else
- if…else
- Nested if
- 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:
Syntax for simple if is as shown below:
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:
Syntax of if…else statement is as shown below:
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:
Syntax of nested if statement is as shown below:
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:
Syntax of else if statement is as shown below:
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:
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:
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:
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:
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:
- Forward jump
- Backward jump.
Syntax of forward jump and backward jump is as shown below:
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:
- while
- do…while
- 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:
- Entry controlled loops
- 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:
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:
Based on the nature of the loops, the loops can be categorized into two types namely:
- Definite loops (Ex: for)
- 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:
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:
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:
The flowchart of the for loop is as shown below:
break and continue
C provides two unconditional branching statements which are extensively used inside the looping statements. They are:
- break
- 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:
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:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Nice and very helpful article for beginners like me…..
Very very nice
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.