Â
WHAT ARE CONDITIONAL STATEMENTS ?
Conditional Statements are those statements which checks the condition whether it is True or False and then executes certain code as per the instruction.
If yes then we will replace bulb and if not then we will repair the lamp. As we saw in the example the same way a conditional statement will check for the conditions and perform action as per the result.
Conditional Statements are one of the important aspects in Programming Language, it decides the flow of program according to the conditions and the response of the user on that condition.
There are different types of Conditional statements in Programming Language :
- If statement
- If else statementÂ
- Else statement
- Nested if statement
- Switch Case
if Statement:
if statement is a conditional statement which will execute the code inside this only when the conditions given are true or satisfied.
Syntax:-
if (condition)
{
//code
//code
}
if-else Statement:
if-else statement is a conditional statement which will execute the code only when the condition of if statement is not satisfied or True.
Syntax:-
if (condition)
{
//code 1
//code 1
}
else if (condition)
{
//code 2
//code 2
}
else Statement:
else statement is a conditional statement which will execute the code only when the condition of if statement and also of if else statement is not satisfied or True, which means when if condition is true then neither 'if else' nor 'else' statement will be executed but when it is False then if else will get checked and if it's not True either then at-last else statement will be executed
Syntax:-
//else
//code
//For else statements its our choice to use brackets or not
else
{
//code
//code
}
Nested if Statement:
Nested if statement is a conditional statement which check for the condition of outer 'if statement' first,if its true then it will check for the inner 'if condition' and then executes the code further. If the outer 'if condition' is not true then it will not process further.
Syntax:-
Syntax:-
if (condition)
{
if(condition)
{
//code
//code
}
}
switch case
Switch Case is a special type of conditional statement. In this we will switch a variable to perform variety or different different tasks in the form of cases, the user will select which case he/she wants to execute and in every case it is important to write a break statement. Break statement will ensure that when the condition is satisfied it stops the further execution. In switch the use of default case is optional, it will get executed only when none of the above cases are chosen by the user.
Syntax:-
Syntax:-
switch(variable);
{
case 1:
//code
//code
break;
case 2:
//code
//code
break;
case 3:
//code
//code
break;
default:
//code
//code
}
Now let us see some examples of conditional statements.
if Statement in C Language :
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
if (a>b)
{
printf(" a is greater number");
}
if (b>a)
{
printf("b is greater number");
}
return 0;
}
Switch case in C :
#include <stdio.h>
int main()
{
int a;
printf("Enter a number for Greetings: ");
scanf("%d", &a);
switch(a)
{
case 1:
printf("Hello");
break;
case 2:
printf("Hello Users");
break;
case 3:
printf("Thankyou Users");
break;
case 4:
printf("Good Byee !!");
break;
default:
printf("Invalid Input");
}
return 0;
}
else statement in C:
#include <stdio.h>
int main()
{
int time = 24;
if(time < 13)
{
printf("Good Morning");
}
else
{
printf("Good Night");
}
return 0;
}
else-if statement in C:
#include <stdio.h>
int main()
{
int time = 24;
if(time < 13)
{
printf("Good Morning");
}
else if(time < 16)
{
printf("Good Afternoon");
}
else if(time < 20)
{
printf("Good Evening");
}
else
{
printf("Good Night");
}
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP