Types of Control Statements in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Control Statements in C

In C, the control flows from one instruction to the next instruction until now in all
programs. This control flow from one command to the next is called sequential
control flow. Nonetheless, in most C programs the programmer may want to skip
instructions or repeat a set of instructions repeatedly when writing logic. This can be
referred to as sequential control flow. The declarations in C let programmers make
such decisions which are called decision-making or control declarations.

Types of Control Statements in C

Decision statement

Decision statement is condition based statement. It define single or set of conditions that
must be satisfied before statement/s can execute. For example, allow amount
withdrawal from ATM, only if pin validation succeeds. These situations are handled
using decision statements.

Decision statements are also known as conditional or branching statement. In


decision statement we will learn about -

1. Simple if statement
2. if...else
3. if...else...if statement
4. Nested if...else statement
5. Switch...case
Looping statement

Looping statement defines a set of repetitive statements . These statements are


repeated, with same or different parameters for a number of times.

In programming, there exists situations when you need to repeat single or a group of
statements till some condition is met. Such as - read all files of a directory.
Looping statements are also known as iterative or repetitive statement.

There are three types of looping statement in C.

1. for loop
2. while loop
3. do...while loop

Jump statements

Unlike conditional and looping statement, jump statement provides unconditional


way to transfer control from one part of program to other.

C supports three jump statements.

1. break
2. continue
3. goto

The goto statement is known as jump statement in C. As the name suggests, goto is
used to transfer the program control to a predefined label. The goto statment can be
used to repeat some part of the code for a particular condition. It can also be used to
break the multiple loops which can't be done by using a single break statement.
However, using goto is avoided these days since it makes the program less readable
and complecated.

Syntax

The syntax for a goto statement in C is as follows −

goto label;

..

label: statement;

Here label can be any plain text except C keyword and it can be set anywhere in the
C program above or below to goto statement.

Flow Diagram
Example

#include <stdio.h>

int main () {

/* local variable definition */

int a = 10;

/* do loop execution */

LOOP:do {

if( a == 15) {

/* skip the iteration */

a = a + 1;

goto LOOP;

printf("value of a: %d\n", a);

a++;

}while( a < 20 );

return 0;

You might also like