Â
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 statement
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 else statement
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 niether 'if else' nor 'else' statement will be executed but when it is False then if else will get checked and if its not True either then atlast else statement will be executed
SYNTAX:-
else statement
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:-
Nested if statement
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 case
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 in Different Programming Languages.
If Statement in C Language :
If statement
#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++ :
Switch case
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter number for greetings";
cin>>a;
switch (a)
{
case 1:
cout<<"Hello Users";
break;
case 2:
cout<<"Thank you Users";
break;
default:
cout<<"Re-enter your input";
break;
}
return 0;
}
Else statement in Java:
Else statement
public class Main()
{
public static void main(String[] args)
{
int time = 20;
if (time < 18)
{
System.out.println("Good day.");
}
else
{
System.out.println("Good evening.");
}
}
}
If else statement in Python:
if else statement
a=input("Enter the first number")
b=input("Enter the second number")
if a>b:
print("The greater number is ",a)
elif b>a:
print("The greater number is ",b)
# In python we write 'else if' as 'elif'
Note: In python Switch case is not supported so in cases where switch case is required we use 'if' and 'elif' statements in ladder form (continuously)
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP