
Statements in language are the sections of a C program that are executed in a certain order and are used to construct the body of a function. Any function’s body is a compound statement, which is made up of a series of statements and declarations.
How many types of statements in C? We can divide the statements in C language in a variety of ways, but we’ve categorized them into the following 3 main categories.
- Conditional Statements
- Loop Statements
- Jump Statements
Table of Contents
Conditional Statements in C – if, if-else, nested if, else-if ladder and switch
Conditional statements are mostly used to make decisions. In decision-making, a statement performs a task based on whether or not a condition is true or false.
In the C programming language, the conditional statements can be used in a variety of ways.
- if statement
- if-else statement
- nested if statement
- else-if Ladder
- switch statement
if statement
A conditional statement is written using the if statement. If the specified condition is true, the statements will be executed; else, the optional statements will be executed.
Syntax of if statement
if(condition)
{
-----
-----
}
Write a program using if statement
#include <stdio.h>
void main()
{
int a = 12;
if(a % 2 == 0)
{
printf("Number is Even");
}
}
if-else statement
A conditional statement is written using an if-else statement. If the specified condition is true, the statements will be executed. Otherwise, the else block statement will be executed.
Syntax of if-else statement
if(condition)
{
-----
-----
}
else
{
-----
-----
}
Write a program using if-else statement
#include <stdio.h>
void main()
{
int a = 12;
if(a % 2 == 0)
{
printf("Number is Even");
}
else
{
printf("Number is Odd");
}
}
nested if statement
Nested if is an if statement within an if statement.
Syntax of nested if statement
if(outer-condition)
{
if(inner-condition)
{
-----
-----
}
}
Write a program using nested if statement
#include <stdio.h>
void main()
{
int a = 10;
if(a % 2 == 0)
{
if(a % 5 == 0)
{
printf("Number is Divisible by 5");
}
printf("Number is Divisible by 2");
}
}
else-if ladder
Syntax of nested else-if ladder statement
if(condition 1)
{
-----
-----
}
else if(condition 2)
{
-----
-----
}
else if(condition 3)
{
-----
-----
}
else
{
-----
-----
}
Write a program using if-else ladder statement
#include <stdio.h>
void main()
{
int a = 15;
if(a % 2 == 0)
{
printf("Number is Divisible by 2");
}
else if(a % 5 == 0)
{
printf("Number is Divisible by 5");
}
else if(a % 7 == 0)
{
printf("Number is Divisible by 7");
}
else
{
printf("Number is not Divisible by 2, 5 or 7");
}
}
switch statement in C language
The switch statement is a multiway decision maker that compares an expression to one of a set of constant values and braces them accordingly. Because several if-statements are time expensive, a switch case statement is a better solution.
Syntax of switch statement
switch (expression)
{
case constant-1:
statement;
break;
case constant-2:
statement;
break;
.
.
case constant-n:
statement;
break;
default:
statement;
}
Write a program using switch statement
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, choice;
while(choice != 3)
{
printf("\n 1. Press 1 for Addition");
printf("\n 2. Press 2 for Subtraction");
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
c = a + b;
printf("%d",c);
break;;
case 2:
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
c = a - b;
printf("%d",c);
break;;
default:
printf("You have passed a wrong key.");
printf("\n Press any key to continue");
}
}
}
Key points related to switch statement
- The break statement is optional in this case. However, if we do not write a break, many lines of code will be run.
- The inside-the-switch expression should provide a constant value.
- Case values that are similar are not permitted.
- Each of the case statements constants must be of the same type.
- The value after the keyword case can only be constants; expressions are not allowed. Integers and characters are allowed, but not floating point values or character strings.
Loop Statements in C – while Loop, do..while loop, for loop
Loop statements repeat the execution of a statement.
while loop in C
When we aren’t sure if the loop will be run, we use a while loop. The condition of a while loop is verified first, and then the statement is executed.
Syntax of While Loop
For a single statement
Variable initialization;
while (condition)
{
statement 1;
statement 2;
...
}
For a block of statements
#include <stdio.h>
int main()
{
int i;
i = 1;
while(i <= 10)
{
printf("%d\t", i);
i++;
}
return 0;
}
Write a program using while loop
#include <stdio.h>
int main()
{
int i;
i = 1;
while(i <= 10)
{
printf("%d\t", i);
i++;
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
do…while loop in C
When we are certain about the condition, we use a do-while loop. It enters the loop at least once before determining if the provided condition is true or false. It is run at least once to see if any of the conditions are true or false.
Syntax of do…while loop
Variable initialization;
do
{
statement 1;
statement 2;
...
} while (condition);
Write a program using do…while loop
#include <stdio.h>
int main()
{
int i;
i = 1;
do
{
printf("%d\t", i);
i++;
} while(i <= 10);
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
for loop in C
In a for loop, there are three expressions. The first expression sets the index value, the second checks if the loop should be iterated, and the third sets the index value for future iteration.
Syntax of do…while loop
for (Initialization; condition; Increment/Decrement)
{
statement 1;
statement 2;
...
}
Write a program using for loop
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <=10; i++)
{
printf("%d\t", i);
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
Jump Statements in C – break, continue, goto statement
- break statement
- continue statement
- goto statement
Break Statement in C
- Loop: The break statement is used to stop the loop statement from running.
- Switch: When using the break statement in a switch-case, it must be used in each instance. If this is not the case, control will be passed at a next case.
The break statement will end control of the innermost loop in a nested loop, and execution will begin on the line after the innermost loop block.
Example of break statement
#include <stdio.h>
int main()
{
int i;
i = 1;
while(i <= 10)
{
printf("%d\t", i);
i++;
if (i == 5)
{
break; // Terminates the loop using break statement.
}
}
return 0;
}
Output: 1 2 3 4
Continue Statement in C
The continue statement skips the current iteration but keeps the loop running. It is used just for a condition, allowing us to bypass code for that condition.
Example of continue Statement
#include <stdio.h>
int main()
{
int i;
i = 1;
while(i <= 10)
{
if (i == 5)
{
i++;
continue; // Skip the current iteration based on condition.
}
printf("%d\t", i);
i++;
}
return 0;
}
Output: 1 2 3 4 6 7 8 9 10
goto statement in C
By transferring control to another part of the program, the goto statement can modify the program’s execution sequence. The goto statement can be used in two ways.
- Unconditional goto
- Conditional goto
Syntax of goto statement
#include <stdio.h>
int main()
{
start: printf("Welcome to Geek World!");
goto start;
return 0;
}
Unconditional goto statement
The unconditional goto statement is used to transfer control from one program section to another without verifying any conditions.
Example of unconditional goto statement
#include <stdio.h>
int main()
{
start: printf("Welcome to Geek World!");
goto start;
return 0;
}
Output: 1 2 3 4 6 7 8 9 10
Conditional goto statement
By testing a condition, conditional goto is used to transfer control of execution from one area of the program to another.
Example of conditional goto statement
#include <stdio.h>
int main()
{
int i;
i = 10;
while(true)
{
if (i < 0)
{
goto error;
}
printf("%d\t", i);
i--;
}
error: printf("\nNegative data found.");
return 0;
}
Output: 10 9 8 7 6 5 4 3 2 1 0 Negative data found.
Also Read:
- C Programming File Handling
- Importance of storage classes and example
- How pointer is used in C language?
- DBMS Data Independence
- Notes of linked list in data structure
- HR Interview Questions and Answers
C Programming File Handling
How pointer is used in C language?
Importance of storage classes and example