Control statements in the C language are crucial elements that enable programmers to control the flow of execution in their code. With control statements, we can make decisions, repeat blocks of code, and alter the program’s behavior based on specific conditions. In this content, we will delve into the intricacies of control statements in C language and explore their functionalities and applications.
In C, control flows from one instruction to the next in all programs so far. This flow of control from one command to the next is called a sequential flow of control. However, in most C programs, programmers can skip instructions or iterate over a series of instructions when writing logic. This is called sequential flow of control. Statements in C allow programmers to make such decisions, called decisions or control statements.
Types of Control Statements in C Language:
C also supports many types of Statements are as follows:
1. If Statements:
The If statement allows the programmer to conditionally select a set of statements. If the condition evaluates to true, a series of statements is executed, and if the condition evaluates to false, another series of instructions is executed.
Types of if Statement:
a. If…else Statement:
There are two types of statements executed in this statement. First, if the condition is true, the first statement is executed, and if the condition is false, the second condition is executed.
Syntax of if…else Statement:
if(condition)
{
Statement(s);
}
else
{
Statement(s)
}
Statement
b. Nested if Statement:
If the condition evaluates to true in the first if statement, the condition is evaluated in the second if statement.
Syntax of Nested if Statement:
if(condition)
{
if(condition)
{
Statement(s);
}
else
{
Statement(s)
}
}
c. else if Ladder:
The corresponding array of statements is executed when the first condition is correct. If the conditions are incorrect, the following conditions are validated: If all specifications fail, the default block declaration is executed. The rest of the staircase can be seen as follows:
Syntax of else if ladder:
if(condition)
{
Statement(s);
}
else if(condition)
{
Statement(s);
}
else if(condition)
{
Statement(s)
}
…
else
{
Statement(s)
}
Statement(s);
d. Null else or Simple else:
Whether the programmer can execute or skip a set of statements based on the value of the condition. A simple one-way declaration is selected. If the condition is true, a series of statements will be executed. If the condition is false, control continues with the next statement after the if statement. Simple else statement :
Syntax of null else or simple else:
if(condition)
{
Statement(s);
}
Statement(s);
2. Switch Statement:
C provides select statements in various ways, as if the program became more difficult to read as the number of conditions increased. C has a multi-directional select statement called a shift statement that is easy to understand to solve this problem. The change statement is easy to understand when there are three or more options. The command changes blocks based on the value of the expression. Each block has a corresponding value.
Syntax of switch Statement:
switch(expression)
{
case label1:
Statement(S);
break;
case label2:
Statement(S);
break;
case label3;
Statement(s);
break;
….
case labelN:
Statement(s);
break;
default:
Statement(s);
break;
}
If you use the case keyword, all blocks are displayed and the block label follows the case keyword. The default lock and break instructions are optional in the change instruction.
3. Conditional Operator Statement:
The C language provides anomalous operators, represented as conditional operators.
Syntax of Conditional Operator Statement:
(condition)? expr1: expr2
expr1 is executed when the condition is valid. If the statement is false then expr2 will be executed.
4. Goto Statement:
The goto statement is known for skipping control statements. It is used to transfer control of a program from one block to another. The goto keyword is used to declare a goto statement.
Syntax of goto Statement:
goto labelname;
labelname;
In the above syntax, goto is the keyword used to pass control to the tag name. labelname is the name of the variable. In this case, goto transfers control of the program to labelname, and the statement followed by labelname is executed.
5. Loop Statements:
When writing a C program, a programmer may want to repeat some instructions until some requirements are met. To that end, C makes a loop statement for decision making.
Types of Loop Statements:
a. For Loop:
In a For loop, the initialization statement executes only once. Then the condition is checked, and if the result of the condition is true, the loop is executed. If false, the for loop ends. However, the result of the conditional evaluation is true, the statement is executed in the body of the for loop, and the expression is updated. Then check the status again. This process continues until the result of the condition is false. If the condition is false, the loop terminate(end).
Syntax of for loop:
for( initialization statement; condition)
{
//statements inside the loop
}
b. While Loop:
In C, the while loop is a guided input loop. The body of the while loop executes only if the condition is valid. If the condition scores are incorrect, the loop structure will not run.
Whereas loops are normally used when it is necessary to repeat several instructions indefinitely.
Syntax of while loop:
while(condition)
{
//statements inside the loop
}
c. Do While Loop:
Unlike while loops, the body of do is the difference between while and … while loops. The while loop is guaranteed to run one at a time.
Syntax of do while loop:
do
{
//statements inside the loop
}
while(condition);
Learn More About What is Array in C Language
Conclusion:
The C language programs presented so far follow a sequential form of instruction execution. In many cases, it is necessary to change the flow of the instruction sequence. The C language provides statements that can change the flow of a series of instructions. These statements are called control statements. We have reviewed the various C control statements and their syntax and examples.
References:
- https://www.sitepoint.com/asynchronous-programming-using-async-await-in-c/
- https://www.guru99.com/c-if-else-statement.html
- https://javatutoring.com/control-statements-in-c/
- https://www.circuitstoday.com/control-structures-in-c-and-cpp
- https://beginnersbook.com/2014/01/c-decision-control-statements/
- https://www.tutorialspoint.com/ansi_c/c_control_statements.htm
- https://www.programiz.com/c-programming/c-decision-making-loops-examples