Control statements in C
They allow you to control the flow of your program by making decisions, repeating
tasks, or jumping to specific parts of the code.
Types of Control Statements in C
[Link] statements
• Decision-Making Statements: These statements allow the program to make
decisions and execute specific blocks of code based on conditions.
• Loop Control Statements: These statements repeatedly execute a block of
code as long as a specified condition is true.
[Link] statements
• Jump Statements: These statements transfer the program's control from one
part of the code to another, either conditionally or unconditionally.
Conditional statements:- In conditional control , the execution of statements depends
upon the condition-test. If the condition evaluates to true, then a set of statements is
executed otherwise another set of statements is followed.
Decision control structure in C can be implemented by using:-
1. If statement
2. If-else statement
3. Nested if else statement
4. else-if ladder
5. switch case
if statement:- The if statement is the simplest decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed or not i.e if a
certain condition is true then a block of statements is executed otherwise not.
A condition is any expression that evaluates to either a true or false
Its general syntax and flow chart is as under:-
if(condition)
{
Statements ;
}
The expression inside () parenthesis is the condition and set of statements
inside {} braces is its body. If the condition is true, only then the body will be
executed.
If there is only a single statement in the body, {} braces can be omitted.
/* program to check whether the given number is negative*/
void main()
{
int num; clrscr();
printf(“enter the number”);
scanf(“%d”,&num);
if(num<0)
{
printf(“The entered number is negative”);
}
getch();
}
if- else statement:- This is a bi-directional control statement. The if-else statement
in C executes one block of code if the condition is true and another block if it is
false.
The general form and flow chart of if-else statement is as under:-
if (condition)
{
// Code if condition is true
}
else
{
// Code if condition is false
}
/*Program to find the biggest of two
numbers*/ #include<stdio.h>
#include<conio.h
> void main()
{
int num1,num2 ;
clrscr();
printf(“enter the two numbers”);
scanf(“%d
%d”,&num1,&num2);
if(num1>num2)
{
printf(“the number %d is big”,num1);
}
else
{
printf(“the number %d is big”,num2);
}
getch();
}
Nested if-else:- If we have if-else statement within either the body of an if statement
or the body of else statement or in the body of both if and else, then this is known as
nesting of if else statement. The general form of nested if-else statement is as follows:-
if (condition1)
{
// Code block if condition1 is true
if (condition2) {
// Code block if both condition1 and condition2 are true
}
else {
// Code block if condition1 is true, but condition2 is false
}
}
else
{
// Code block if condition1 is false
// You can also nest another if-else here if needed
if (condition3) {
// Code block if condition1 is false and condition3 is true
}
else {
// Code block if condition1 is false and condition3 is false
}
}
We give the following program to explain nesting of if else:-
/*program to find the largest of three
numbers*/ #include<stdio.h>
#include<conio.h>
void main()
{
int a, b,c,large;
printf(“enter the three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
large=a;
else
large=c;
}
else
{
if(b>c)
large=b;
else
large=c;
}
printf(“largest number is %d”,large);
getch();
}
Else if ladder:- This conditional statement
in C checks multiple conditions one by one
until a true condition is found.
The general syntax and flow chart of else if
ladder is as follows:-
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if condition1 is false AND condition2 is true
}
else if (condition3) {
// block of code to be executed if condition1 is false AND condition2 is false AND
condition3 is true
}
// ... (additional else if statements can be added)
else {
// block of code to be executed if all preceding conditions are false
}
A program to illustrate the else if ladder:-
/*program to find the grade of the student*/
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 90) {
printf("Grade: A");
} else if (marks >= 75) {
printf("Grade: B");
} else if (marks >= 50) {
printf("Grade: C");
} else {
printf("Grade: F");
}
return 0;
}
Switch case statement :- The switch case statement is an alternative to the if else if
ladder that can be used to execute the conditional code based on the value of the
variable specified in the switch statement. The switch block consists of cases to be
executed based on the value of the switch [Link] general syntax of switch case
statement is as follows:-
switch (expression)
{
case value1:
// Code block to be executed if expression == value1
break; // Optional: Exits the switch statement
case value2:
// Code block to be executed if expression == value2
break; // Optional: Exits the switch statement
// ... more case statements
default:
// Code block to be executed if no case matches (optional)
break; // Optional: Exits the switch statement
}
Here switch, case and default are keywords.
The expression following the switch keyword must give an integer value. This
expression can be any integer or character variable, or a function call that returns an
integer. It can also be arithmetic, relational, logical or bitwise expression yielding
an integer. It can be integer or character constant also. Data types long int and short
int are also allowed.
The constant following the case keyword should be of integer or character type. We
cannot use floating point or string constant.
A program to explain switch case statement
Print Day Name of the Week
#include <stdio.h>
int main() {
// Day number
int day = 2;
// Switch statement to print the name of the day
// on the basis of day number
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid Input");
break;
}
return 0;
}
NOTE:- In switch case statement, if we do not associate a break statement with
every case then from the case for which the expression matches with the constant,
all the statements are executed until the switch case block ends. This situation is
referred to as Fall Through.
Conditional operator:- It is a ternary operator and is used to perform simple
conditional operations. It is used to do operations similar to if-else statement.
The general syntax of conditional operator
is as under:- Test expression?
expression1:expression2
Here firstly the test expression is evaluated.
If the test expression evaluates to true then the expression1 is evaluated and it
becomes the value of the whole expression.
If the test expression evaluates to false then the expression2 is evaluated and it
becomes the value of the whole expression.
For eg., a>b ? a : b
Here firstly the expression a>b is evaluated. If it evaluates to true then the value of
a becomes the value of the whole expression otherwise value of b becomes the
value of whole expression.
Loops in C
Loops in C programming are used to repeat a block of code until the specified condition
is met. It allows programmers to execute a statement or group of statements multiple
times without writing the code again and again.
Types of Loops in C
Entry Controlled Loop Entry Controlled Loop
Condition is evaluated before body Condition is evaluated after body of
of loop is executed. loop is executed.
Loop body may not execute if Loop body executes at least once
condition is false initially. before condition evaluation.
do { // Loop body } while
while (condition) { // Loop body }
(condition);
Used when the loop may not need Used when the loop body must
to execute at all if the condition is execute at least once before
initially false. evaluating the condition.
for Loop
It is an entry-controlled loop, which means that the condition is checked before the
loop's body executes.
Syntax
for (initialization; condition; updation) {
// body of for loop
}
The various parts of the for loop are:
• Initialization: Initialize the variable to some initial value.
The initialization statement is executed first and only once.
• Test Condition: This specifies the test condition. If the condition evaluates to
true, then body of the loop is executed. If evaluated false, loop is terminated.
• Update Expression: After the execution loop’s body, this expression
increments/decrements the loop variable by some value.
• Body of Loop: Statements to repeat. Generally enclosed inside {} braces.
Example:
#include <stdio.h>
int main()
{
// Loop to print numbers from 1 to 5
for (int i = 0; i < 5; i++)
{
printf( "%d ", i + 1);
}
return 0;
}
Flowchart of for Loop
while Loop
It is also an entry-controlled loop in which the condition is checked before entering the
body. Syntax
while (condition) {
// Body of the loop
}
Only the condition is the part of while loop syntax, we have to initialize and update
loop variable manually. If the test condition inside the () becomes true, the body of the
loop executes else loop terminates without execution. The process repeats until
the test condition becomes false.
Example:
#include <stdio.h>
int main() {
// Initialization expression
int i = 0;
// Test expression
while(i <= 5) {
printf("%d ", i + 1);
// update expression
i++;
}
return 0;
}
Flowchart of while Loop
do-while Loop
The do-while loop is an exit-controlled loop, which means that the condition is
checked after executing the loop body. Due to this, the loop body will execute at least
once irrespective of the test condition.
Syntax
do {
// Body of the loop
} while (condition);
Like while loop, only the condition is the part of do while loop syntax, we have to do
the initialization and updating of loop variable manually.
Example:
#include <stdio.h>
int main() {
// Initialization expression
int i = 0;
do
{
// loop body
printf( "%d ", i);
// Update expression
i++;
// Condition to check
} while (i <= 10);
return 0;
}
Flowchart of do-while Loop
While do-while
Condition is checked first then Statement(s) is executed atleast once,
statement(s) is executed. thereafter condition is checked.
It might occur statement(s) is
At least once the statement(s) is
executed zero times, If condition is
executed.
false.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
Variable in condition is initialized variable may be initialized before or
before the execution of loop. within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) { statement(s); } do { statement(s); } while(condition);
Infinite Loop
An infinite loop is executed when the test expression never becomes false, and the
body of the loop is executed repeatedly. A program is stuck in an Infinite loop when
the condition is always true.
Using for loop:
#include <stdio.h>
int main () {
// This is an infinite for loop
// as the condition expression
// is blank
for ( ; ; ) {
printf("This loop will run forever.");
}
return 0;
}
Using While loop:
#include <stdio.h>
int main() {
while (1)
printf("This loop will run forever.\n");
return 0;
}
Using the do-while loop:
#include <stdio.h>
int main() {
do {
printf("This loop will run forever.");
} while (1);
return 0;
}
Nested Loops
Nesting loops means placing one loop inside another. The inner loop runs fully for
each iteration of the outer loop. This technique is helpful when you need to perform
multiple iterations within each cycle of a larger loop, like when working with a two-
dimensional array or performing tasks that require multiple levels of iteration.
Example:
#include <stdio.h>
int main() {
// Outer loop runs 3 times
for (int i = 0; i < 3; i++) {
// Inner loop runs 2 times for each
// outer loop iteration
for (int j = 0; j < 2; j++) {
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}
Jump statements
Jump statements are used to jump from one part of the code to another altering the
normal flow of the program. They are used to transfer the program control to
somewhere else in the program.
Types of Jump Statements in C
There are 4 types of jump statements in C:
• break
• continue
• goto
• return
Break Statement in C
The break statement exits or terminates the loop or switch statement based on a certain
condition, without executing the remaining iteration of the loop or checking remaining
cases in switch statement.
Syntax of break in C
break;
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Terminates the loop when i is equal to 5
}
printf("%d\n", i);
}
return 0;
}
Output
1
2
3
4
Continue Statement in C
The continue statement in C is used to skip the remaining code after the continue
statement within a loop and jump to the next iteration of the loop. When the continue
statement is encountered, the loop control immediately jumps to the next iteration, by
skipping the lines of code written after it within the loop body.
Syntax of continue in C
continue;
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skips the rest of the loop body for i equal to 5
}
printf("%d\n", i);
}
return 0;
}
Output
1
2
3
4
6
7
8
9
10
Goto Statement in C
The goto statement is used to jump to a specific point from anywhere in a function. It is
used to transfer the program control to a labeled statement within the same function.
Syntax of goto Statement
goto label;
.
.
label:
//code
Flowchart of goto Statement
#include <stdio.h>
int main() {
int num = 5;
// Check if the number is even or odd
if (num % 2 == 0) {
goto even_label; // Jump to 'even_label' if num is even
} else {
goto odd_label; // Jump to 'odd_label' if num is odd
}
even_label:
printf("The number %d is even.\n", num);
goto end_program; // Jump to 'end_program' after printing
odd_label:
printf("The number %d is odd.\n", num);
goto end_program; // Jump to 'end_program' after printing
end_program:
printf("Program finished.\n");
return 0;
}
Return Statement in C
The return statement in C is used to terminate the execution of a function and return a
value to the caller. It is commonly used to provide a result back to the calling code.
Syntax:
return expression;
#include <stdio.h>
int multiply(int a, int b) {
return a * b;
}
int main() {
int result = multiply(5, 7);
printf("The product is: %d\n", result);
return 0;}
Difference Between the break and continue statement:
Break Statement Continue Statement
The Break statement is used to exit from The continue statement is not used to exit
the loop constructs. from the loop constructs.
The break statement is usually used with The continue statement is not used with
the switch statement, and it can also use it the switch statement, but it can be used
within the while loop, do-while loop, or the within the while loop, do-while loop, or
for-loop. for-loop.
When the continue statement is
When a break statement is encountered
encountered then the control
then the control is exited from the loop
automatically passed from the beginning
construct immediately.
of the loop statement.
Break Statement Continue Statement
Syntax: Syntax:
break; continue;
Break statements uses switch and label It does not use switch and label
statements. statements.
Leftover iterations are not executed after Leftover iterations can be executed even
the break statement. if the continue keyword appears in a loop.