Decision making statements and Switch Statements
Decision Making Statements
In decision control statements( if else and nested if ), group of statements are executed when condition is true. If condition is false, then else part statements are executed.
There are 3 types of decision making control statements in C language.
They are,
1. if statements
2. if else statements
3. nested if statements
Decision Making Statements - Syntax
S.No if statement if…else statement nested if
1.Syntax if(condition)
{
Statements;
}
if(condition) {
Statement1;
Statement2; }
else {
Statement3;
Statement4; }
Decision Making Statements - if
Example program for if:
In “if” control statement, respective block of code is executed when condition is true.
main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
}
Output:
m and n are equal
Decision Making Statements - if else
Example program for C – if else:
In C if else control statement, group of statements are executed when condition is true. If condition is false, then else part statements are executed.
void main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output
m and n are equal
Decision Making Statements - nested if
Example program for C – nested if:
In “nested if” control statement, if condition 1 is false, then condition 2 is checked and statements are
executed if it is true. If condition 2 also gets failure, then else part is executed.
void main()
{
int m=40,n=40;
if (m>n)
{
printf("m is greater than n");
}
else if (m<n) {
printf("m is less than n");
}
else
{
printf("m is equal to n");
}
}
Output
m is greater than n
C – Case control statements
The statements which are used to execute only specific block of statements in a given series of block are called case control statements.
There are 4 types of case statements in C language. They are,
1.switch case
2.break
3.continue
4.goto
1. switch case statement:
This is used to execute only specific case statements based on the switch expression.
Syntax : switch (expression)
{
case label1:
statements;
break;
case label2:
statements;
break;
default:
statements;
}
Example
void main()
{
int value = 3;
switch (value)
{
case 1:
printf("Value is 1 \n" );
break;
case 2:
printf("Value is 2 \n" );
break;
case 3:
printf("Value is 3 \n" );
break;
case 4:
printf("Value is 4 \n" );
break;
default :
printf("Value is other than 1,2,3,4 \n" );
}
}
Output
Value is 3
2. break statement
Break statement is used to terminate the while loops, switch case loops and for loops from the subsequent execution.
Syntax: break;
void main()
{
int i;
for (i=0;i<10;i++)
{
if (i==5) {
printf("Coming out from for loop when i = 5");
break; }
printf("%d\n",i);
}
}
Output
0
1
2
3
4
Coming out from for loop when i = 5
3. continue statement
Continue statement is used to continue the next iteration of for loop, while loop and do-while loops. So, the remaining statements are skipped within the loop for that particular iteration.
Syntax: continue;
void main()
{
int i;
for (i=0;i<10;i++)
{
if (i==5) {
printf("continue for loop and skip 5\n");
continue; }
printf("%d\n",i);
}
}
Output
0
1
2
3
4
continue for loop and skip 5
6
7
8
9
4. goto statements
goto statements is used to transfer the normal flow of a program to the specified label in the program.
Syntax:
{
…….
go to label ;
…….
…….
label:
Statements;
}
Example
void main()
{
int i;
for(i=0;i<10;i++) {
if (i==5) {
printf(" We are using goto statement when i = 5 \n");
goto HAI ; }
printf("%d\n",i);
}
HAI : printf("Now, we are inside label name hai \n");
}
Output
0
1
2
3
4
We are using goto statement when i = 5
Now, we are inside label name hai
Comments
Post a Comment