Previous post " Decision Control Statement". This post continuation of control statements. If-else means you are selecting one from given two choices if your condition is true or false .
About the Author
Prasadh Teppala
Embedded Engineer,
India
prasadel06 [at] gmail.com
Prasadh Teppala
Embedded Engineer,
India
prasadel06 [at] gmail.com
if-else:
In ‘c’ language general format is:
if ( condition )
{
else
{
{
Simple statements-1;
}else
{
Simple statements-2;
}Here initially control will check condition is true or not. If condition is true simple statements-1 will execute, else if condition is false simple statements-2 will execute.
This is equal to the statement
condition ? Simple statements-1 : Simple statements-2;
Which is better??
In coding readability is the main characteristic. According to the readability ‘if-else’ statements are good one and also if we have more statements in ‘if- else’ blocks ‘if-else’ is better one. Conditional statements with ‘? :’ is complex type of coding.
Eg:
int noOfEggs; /* no of eggs */
If ( noOfEggs > 6 )
{
printf(“ More no of Eggs”);
}
else
{
printf(“ less no of Eggs ”);
}
Is equal to the statement
noOfEggs > 6 ? printf(“ More no of Eggs”) : printf(“ less no of Eggs ”);
if noOfEggs is 7, output: More no of Eggs
if noOfEggs is 5, output: less no of Eggs
Nested if-else:
1. Used for giving priority to do work.
2. If we want to select to do from so many works.
if ( condition-1 )
{
else if ( condition-2 )
{
……………………..
………………………
else
{
{
Simple statements-1;
}else if ( condition-2 )
{
Simple statements-2;
}……………………..
………………………
else
{
Simple statements-n;
}Here if any condition true the statements corresponding to that ‘if’ or ‘else-if’ block will be executed and remaining will not. If first condition is true i.e highest priority then statements in the first ‘if’ block will be executed. In the same way for second, third, etc…
switch-case:
Instead of using longer chain of if-else it is better use switch-case if the condition is number. And for menu driven programs we can use switch-case.
General structure of the switch-case is
switch (variable or expression )
{
case value1:
case valuen:
simple statement;
.......
.......
.......
{
case value1:
simple statement;
simple statement;
......break;
case valuen:
simple statement;
.......
break;
default:.......
.......
break;
}Here expression evaluates and give some value. If that value matches with any case then the statements under that case will be executed. Here break is used for execution of statements corresponding to the matched case and after that control come out of the entire switch block. And default is used as the default case. If expression gives value which not matched with any case then it is best practice to give some warning statements.
Eg :
int a;
switch(i)
{
Case 1:a=10;
break;
Case 2:a=20;
break;
default:a=0;
break;
}
If initially a = 1 then after switch block a = 10;
If initially a = 2 then after switch block a = 20;
If initially a = other than 1 and 2 then after switch block a = 0;
Eg :
int a;
switch(i)
{
Case 1:a=10;
Case 2:a=20;
break;
default:a=0;
break;
}
If initially a = 1 then after switch block a = ?? ;
Here if initially ‘a’ value is 1, in switch block it will match with case 1 then ‘a’ will assigned to 10. But due to no break after case 1, the next statement also execute in switch block because of the sequential execution of statements in ‘C’ language. Here control will not check again for case 2 because of control already in the switch block. Therefore ‘a’ again assigned to 20.i.e a =20.
Eg:
char a;
switch(a)
{
Case ‘A’:
printf(“A selected”);
Break;
Case ‘B’:
printf(“B selected”);
break;
default:printf(“Not A and B”);
break;
} What will be the output if ‘a’ is assigned to ‘A’ output is ???
case statements with expressions and float and range of values will not work.
Eg:
char a;
switch(a)
{
Case ‘A’;
printf(“A selected”);
Break;
Case ‘B’;
printf(“B selected”);
break;
default;
printf(“Not A and B”);
break;
} What will be the output if ‘a’ is assigned to ‘B’ output is ???
Here compiler gives error:
Reason: Case ‘A’; instead of Case ‘A’:
0 comments: