If else,else if ladder, Switch Decision Making Statement's In C Language - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Sunday, 14 April 2019

If else,else if ladder, Switch Decision Making Statement's In C Language

You can decide which statements you want to execute in your program and which statements you want to skip. This is called decision making. Most decisions are done on a condition basis.
When a particular condition arrives, you can execute the statements you want. For this, you use some built in statements. Because these statements work with the conditions, they are also called conditional statements.
And because they control execution in the statement program, they are also called control statements.
Suppose you want to print the name of any of the two students whose age is high. How can you do this? See the program below.
#include <stdio.h>
#include <conio.h>
void main()
{
raviAge = 40;
ankitAge = 32;
}
In the above example, 2 students have been stored in age 2 variables. But you only have to print the age of the student whose age is the highest. In this situation you need to take a decision. You can do this by any decision making statement (If, If-else, Switch).

If Statement

If statement define a block by curly braces {}. When the condition is true then the statement given in this block is executed.
if(condition)
{
/* Statements to be executed if condition is true */
}
If the condition is false then skip the entire block to the compiler. If the if statement is used in the above example, then the program can be written as
#include <stdio.h>
int main()
{
int RamAge = 40;
int MohanAge = 32;
/* Taking decision which variable holds bigger value using if statement */
if(RamAge > mohanAge)
{
printf(“Ram is elder.”);
}
return 0;
}
In the above example, a condition is used while using conditional operator. If the age of Ravi is greater than Ankit then the printf () statement given in the if statement will execute. But if not, then this statement will not be executed.
Output: Ravi is elder
Also read
[su_posts template=”templates/list-loop.php” posts_per_page=”5″ tax_term=”1834″ order=”desc”]

If-Else Statement

If else statement is considered as part of an if statement. But else block and add to it. The statement given in Else block is executed when the condition of if is false.
if(condition)
{
/* Statements to be executed when condition is true */
}
else
{
/* Statements to be executed when condition is false */
}
As you know if the statements in if block are executed on if the condition is true. But you can also decide what to do if the condition is false. For this you use else block. This block always comes after the if block.
These statements are written in this block, which will be executed if the condition is false. If if else statement is used in the above example then you can write it like this.
#include <stdio.h>
void main()
{
int ramAge = 20;
int mohanAge = 32;
/* Taking decision using if else statements */
if(ramAge > mohanAge)
{
printf(“Ram is elder”);
}
else
{
printf(“mohan is elder”);
}
}
Output: Ankit is elder

else if

If you want to put another condition between if and else, then you can do it by defining another if block.

if(ramAge > mohanAge)

{
printf(“Ram is elder”);
}
/* one more condition checked using else if */
else if(mohanAge > ramAge)
{
printf(“mohan is elder”);
}
else
{
printf(“Both are equal”);
}

Switch Statement

The switch statement is similar to if statement but case check is done instead of checking the condition. When you come to a particular case, you write the statements that you want to execute inside the case.
Case is matched to an integer variable. The same case goes to the execute case which matches the integer variable

int caseNumber = n;

switch(caseNumber)
{
case 1:
/* Statements to be executed if caseNumber is 1 */
break;
case 2:
/* Statements to be executed if caseNumber is 2 */
break;
default:
/* Statements to be executed if caseNumber is not 1 & 2 */
break;
}
When you set the caseNumber variable with an integer value and pass it in the switch statement, the same number that match this number will be executed.
For example, if you have passed 2 in case number then the case of second number will execute and all statements before the break will be executed.
If a case does not match then the default case is execute. If after each case the break statement is used then all the cases will be executed. You can also define the case with alphabets.

#include <stdio.h>

void main()
{
int caseNumber;
printf(“Enter a number “);
scanf(“%d”,&caseNumber);
/* Passing the case number to switch to execute desired case */
switch(caseNumber)
{
case 1:
printf(“nFirst Case executed….”);
break;
case 2:
printf(“nSecond Case executed….”);
break;
default:
printf(“nNone of the case matched. You can only enter 1 or 2”);
break;
}
}
In the above example, a number is being entered from the user and matching case on the base of that number is being executed.

Enter a number :

2
Second case executed….

No comments:

Post a Comment