MODULE5
INTRODUCTION TO 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. Below we will discuss the types of Control Statements in
C.
Types of Control Statements in C
C also supports an unconditional set of branching statements that transfer the
control to another location in the program. Selection declarations in C.
1. If statements
2. Switch Statement
3. Conditional Operator Statement
4. Goto Statement
5. Loop Statements
1. If Statements
If statement enables the programmer to choose a set of instructions, based on a
condition. When the condition is evaluated to true, a set of instructions will be
executed and a different set of instructions will be executed when the condition
is evaluated to false. We have 4 types of if Statement which are:
1. If..else
2. Nested if
3. Else if ladder
4. Simple if or null else
5. Null else or Simple else
If…else Statement
In this statement, there are two types of statements execute. First, if the
condition is true first statement will execute if the condition is false second
condition will be executed.
Syntax:
If(condition)
Statement(s);
else
{
Statement(s)
Statement
Nested if
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.
Syntax:
If(condition)
If(condition)
Statement(s);
Else
Statement(s)
}
else if Ladder
The corresponding array of instructions is executed when the first condition is
correct. If the condition is incorrect, the next condition will be verified. If all the
specifications fail, the default block statements will be executed. The remainder
of the ladder can be shown as shown below.
Syntax:
If(condition)
Statement(s);
Else if(condition)
Statement(s);
}
else if(condition)
Statement(s)
Else
Statement(s)
Statement(s);
Null else or Simple else
If the programmer can execute or skip a set of instructions based on the
condition value. The simple one-way statement is selected. A set of statements
is carried out if the condition is true. If the condition is false, the control will
proceed with the following declaration after the if declaration. Simple else
statement:
Syntax:
If(condition)
Statement(s);
Statement(s);
Switch Statement
C offers a selection statement in several ways as if the program becomes less
readable when the number of conditions increases. C has a multi-way selection
statement called the switch statement that is easy to understand to resolve this
problem. The switch declaration is easy to understand if more than 3
alternatives exist. The command switches between the blocks based on the
expression value. Each block will have a corresponding value.
Syntax:
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;
Using the case keyword every block is shown and the block label follows the
case keyword. The default block and the break statement are optional in a
switch statement.
https://www.educba.com/control-statements-in-c/
WORKING WITH CONDITIONAL STATEMENT
A loop statement allows us to execute a statement or group of statements
multiple times. Given below is the general form of a loop statement in most of
the programming languages −
C programming language provides the following types of loops to handle
looping requirements.
Sr.No Loop Type & Description
.
1 while loop
Repeats a statement or group of statements while a given condition
is true. It tests the condition before executing the loop body.
2 for loop
Executes a sequence of statements multiple times and abbreviates
the code that manages the loop variable.
3 do...while loop
It is more like a while statement, except that it tests the condition at
the end of the loop body.
4 nested loops
You can use one or more loops inside any other while, for, or
do..while loop.
Loop Control Statements
Loop control statements change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope
are destroyed.
C supports the following control statements.
Sr.No Control Statement & Description
.
1 break statement
Terminates the loop or switch statement and transfers execution to
the statement immediately following the loop or switch.
2 continue statement
Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.
3 goto statement
Transfers control to the labeled statement.
The Infinite Loop
A loop becomes an infinite loop if a condition never becomes false.
The for loop is traditionally used for this purpose. Since none of the three
expressions that form the 'for' loop are required, you can make an endless loop
by leaving the conditional expression empty.
#include<stdio.h>
int main (){
for(;;){
printf("This loop will run forever.\n");
}
return0;
}
When the conditional expression is absent, it is assumed to be true. You may
have an initialization and increment expression, but C programmers more
commonly use the for(;;) construct to signify an infinite loop.
https://www.tutorialspoint.com/cprogramming/c_loops.htm
C BREAK AND CONTINUE
We learned about loops in previous tutorials. In this tutorial, we will learn to use
break and continue statements with the help of examples.
C break
The break statement ends the loop immediately when it is encountered. Its
syntax is:
break;
The break statement is almost always used with if...else statement inside the
loop.
How break statement works?
Example 1: break statement
// Program to calculate the sum of a maximum of 10 numbers
// If a negative number is entered, the loop terminates
# include<stdio.h>
intmain()
{
inti;
double number, sum = 0.0;
for(i=1; i<= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
// If the user enters a negative number, the loop ends
if(number <0.0)
{
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf",sum);
return0;
}
Output
Enter a n1: 2.4
Enter a n2: 4.5
Enter a n3: 3.4
Enter a n4: -3
Sum = 10.30
This program calculates the sum of a maximum of 10 numbers. Why a
maximum of 10 numbers? It's because if the user enters a negative number,
the break statement is executed. This will end the for loop, and the sum is
displayed.
In C, break is also used with the switch statement. This will be discussed in the
next tutorial.
C continue
The continue statement skips the current iteration of the loop and continues
with the next iteration. Its syntax is:
continue;
The continue statement is almost always used with the if...else statement.
How continue statement works?
Example 2: continue statement
// Program to calculate the sum of a maximum of 10 numbers
// Negative numbers are skipped from the calculation
# include<stdio.h>
intmain()
{
inti;
double number, sum = 0.0;
for(i=1; i<= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
if(number <0.0)
{
continue;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf",sum);
return0;
}
Output
Enter a n1: 1.1
Enter a n2: 2.2
Enter a n3: 5.5
Enter a n4: 4.4
Enter a n5: -3.4
Enter a n6: -45.5
Enter a n7: 34.5
Enter a n8: -4.2
Enter a n9: -1000
Enter a n10: 12
Sum = 59.70
In this program, when the user enters a positive number, the sum is calculated
using sum += number; statement.
When the user enters a negative number, the continue statement is executed and
it skips the negative number from the calculation.
https://www.programiz.com/c-programming/c-break-continue-statement
WORKING WITH JUMP STATEMENTS
A goto statement in C programming provides an unconditional jump from the
'goto' to a labeled statement in the same function.
NOTE − Use of goto statement is highly discouraged in any programming
language because it makes difficult to trace the control flow of a program,
making the program hard to understand and hard to modify. Any program that
uses a goto can be rewritten to avoid them.
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
Live Demo
#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);
return0;
}
When the above code is compiled and executed, it produces the following
result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm#:~:text=A
%20goto%20statement%20in%20C,statement%20in%20the%20same
%20function.&text=Any%20program%20that%20uses%20a%20goto%20can
%20be%20rewritten%20to%20avoid%20them.