Selection statement:
- selection statement executes one or more statements based on condition or expression.
- selection statements are also called conditional statements or branching statements.
- if statement
- if else statements
- nested if ...else statement
- if....else if statement
- switch
if statement:
- if is a conditional statement based on condition it execute one or more statements.
- it is also called one way selection.
syntax: if(expression)
{
statement1;
statement2;
statement n;
}
if ...else statement:
- it is also called two way selection statement because it select one alternative among two alternatives.
syntax: if(expression)
{
statement1;
}
else
{
statement2;
}
statement x;
- if expression is true it execute statement1 and skip statement 2.then execute statement x.
- if expression is false it skip statement1 and execute statement2 followed by statementx.
Nested if...else:
- when one if..else statement is included another if..else statement is called nested if...else statement.
- it is a multyway selection statement.
syntax:
if(exp1)
{
if(exp2)
statement1;
else
statement 2;
}else
statement3;
statement x;
Switch statement:
- switch statement is a multi way selection statement .
- it is similar to if ....else if statement.
syntax: Switch(expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
case valuen:
statement n;
break;
default:
statement d;
break;
}
statementx;
- here expression is a integer value or character.
/* c program to display week day by using switch statement*/
#include<stdio.h>
#include<conio.h>
Main()
{
Int d;
Clrscr();
Printf(“Enter week day in int”);
Scanf(“%d”,&d);
Switch(d)
{
Case 1:
Printf(“Sunday”);
break;
case 2:
Printf(“monday”);
break;
case 3:
Printf(“tuesday”);
break;
case 4:
printf(“wednessday”);
break;
case 5:
Printf(“thusday”);
break;
case 6:
Printf(“friday”);
break;
case 7:
Printf(“Satday”);
break;
default:
Printf(“invalid week day”);
break;
}
getch();
}
Output:
Enter week day in int 1
sunday
You can also refer
----------------------------------------------------------------------------------------------------------
No comments:
Post a Comment