Conditonal Statements in C
A conditional statement in C language allows a program to execute different actions based on whether a specified condition is true or false. The most commonly used conditional statement in C is the "if" statement, which tests a condition and executes a block of code if the condition is true, and optionally executes a different block of code if the condition is false. There are also other conditional statements in C such as the "else if" statement and the "switch" statement which allow for more complex conditional logic.
If() Statement
if()
statement is used to check condition, Inside if()
statement every expression is evaluate as true or false. when condition is become true if block code will be execute. In C language, every non-zero value treated as true.
In C, the most commonly used conditional statement is the if
statement, which has the following basic syntax:
if (condition) {
// code to execute if the condition is true
}
Here, condition
is a Boolean expression that evaluates to either true or false. If the condition is true, the block of code inside the curly braces will be executed. If the condition is false, the code inside the curly braces will be skipped.
Example:
#include <stdio.h> // header file
#include <stdlib.h> // header file
int main(int argc, char const *argv[])
{
int number1 = 45;
int number2 = -34;
if (number1 > 0) // true
{
printf("Number is positive");
}
if (number1 < 0) // false
{
printf("Number is Negative");
}
if (number2 > 0) // false
{
printf("Number is positive");
}
if (number2 < 0) // true
{
printf("Number is Negative");
}
return 0;
}
If - Else
You can also use an else
clause to specify a block of code to execute if the condition is false:
if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}
Example for if else:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int number1 = 45;
int number2 = -34;
if (number1 > 0) // true
{
printf("Number is positive");
}
else //execute when condition become false
{
printf("Number is Negative");
}
if (number2 < 0) // true
{
printf("Number is Negative");
}
else //execute when condition become false
{
printf("Number is positive");
}
return 0;
}
In addition, you can use an else if
clause to specify additional conditions to test:
Syntax :
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition1 is false and condition2 is true
} else {
// code to execute if both condition1 and condition2 are false
}
if() ... else()
statement is used for checking condition when condition becomes true if()
block code will be execute, when condition becomes false then else()
block code will be execute.
if()... else if()... else()
statement is used to check multiple conditions . here is the Examples
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int number1 = -45;
if (number1 > 0) // when true
{
printf("Number is positive ");
}
else if (number1 < 0) // when true
{
printf("Number is Negative ");
}
else //execute when all condition become false
{
printf("Number is Zero ");
}
return 0;
}
Switch Case Statement
You can also use a switch
statement to test a variable against multiple possible values: Here is switch case
syntax
switch (variable) {
case value1:
// code to execute if variable is equal to value1
break;
case value2:
// code to execute if variable is equal to value2
break;
default:
// code to execute if variable does not match any of the previous cases
break;
}
Here, variable
is the variable you want to test, and each case
statement specifies a possible value for the variable. If the variable matches a case
value, the code inside that case
block will be executed. The default
case specifies code to execute if the variable does not match any of the previous cases.
Example for switch case:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int i = 5;
switch (i)
{
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(" To day is Holiday");
break;
default:
printf("Invalid week day");
break;
}
return 0;
}