Conditional Statement in C
A conditional statement is a programming technique used in the C language that enables the program to take actions depending on the outcome of a particular condition.
In C, there are two types of conditional statements are:
- If statement
- Switch statement
The following C programming language if statement variations are listed below.
Statement | Description |
if statement | A piece of Code is only executed using the "if" statement if a particular condition is true. |
if…else statement | If the condition is false, the program can run another section of the Code by adding an "else" clause to an "if" statement. |
if else, if the ladder | Numerous cases will be conducted for various conditions, Within an if-else-if ladder statement. |
Nested if statement | if or if-else otherwise statements can be used inside other if or if-else statements. |
If statement:
The most common conditional statement in C is the "if" statement. if the condition is true, this statement evaluates a condition and executes an entire section of the Code.
Flowchart:
Syntax:
if(expression)
{
Statement; // If the condition is true, run the Code.
}
Example:
#include<stdio.h>
main()
{
int a=20;
if(a>10) //check the if condition
{
printf(“ a is greater than 10”); // if condition is true its print
}
}
Output:
We declare the variable “a” and initialize it to 20 in this program. Then, we utilize an if statement to determine whether an is greater than 10. If so, we use the printf function to print the message variable "a is greater than 10" to the console.
If else statement
A conditional statement "if-else" clause executes one chunk of Code if it is true and another bit if it is false.
Flowchart:
Syntax:
if (condition)
{
// true statement
}
else
{
// false statement
}
Example:
#include <stdio.h>
main()
{
int n;
printf("Enter n value:");
scanf("%d", &n);
if (n>0)
{
printf("%d is a positive ",n);
}
else
{
printf("%d is a negative ",n);
}
}
Output:
The "if-else" phrase in the program determines whether the number is positive or negative.
Then, we utilize an if else statement to determine whether an number is positive or negative. If so, we use the printf function to print the message variable "n is a positive" else “ n is a negative” to the console.
If else-if ladder
When a certain condition is true, a conditional statement is known as an "if-else if" executes one piece of Code; when the condition is false, it executes a different block of Code. We can use an "if-else if" ladder to describe several conditions and related actions for each condition if multiple conditions need to be verified.
Flow chart:
Syntax:
if (condition1)
{
//if statement 1 is true
}
else if (condition2)
{
// if statement2 is true
}
else if (condition3) {
// if statement3 is true
}
else
{
//Code to be executed if all conditions are false
}
Example:
#include <stdio.h>
main()
{
int n;
printf("Enter n value:");
scanf("%d", &n);
if (n>0)
{
printf("%d is a positive ",n);
}
else if(n<0)
{
printf("%d is a negative ",n);
}
else
{
printf("%d is a zero",n);
}
}
Output:
To determine whether the number is positive, negative, or zero, use the "if-else if" ladder the program prints "n is a positive " if the n is greater than 0. The program prints "n is a negative" if the value is less than 0 the program prints "number is zero" if it is zero.
Switch Statement
The "switch" statement is a control statement that enables a program to assess an expression and run several code blocks based on the expression's value. When numerous conditions need to be verified, the "switch" statement is frequently used as an alternative to an "if-else if" ladder.
Flowchart:
Syntax:
switch (expression)
{
case value1:
// if expression equals value1
break;
case value2:
// if expression equals value2
break;
default:
if expression doesn't match any of the cases
}
Example:
#include <stdio.h>
main()
{
int day;
printf("Enter a number between 1 and 7: ");
scanf("%d", &day);
switch (day)
{
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input\n");
}
}
This program asks the user to enter a number between 1 and 7, and then outputs the matching day of the week using a switch statement. The program outputs "Invalid input!" if the user inputs a number that is outside of the range.
Output:
The user entered the number 4 in this instance, which caused the program to output "Wednesday".