Decision Statements II-83
if(z>y)
printf("z=%d ",z);
else
printf("y=%d ",y);
}
}
OUTPUT:
Enter three numbers x, y, z : 10 20 30
The largest out of three numbers is :z=30
Explanation: This is also an example of nested ifs. When the if statement satisfies the condition,
control passes to another if statement block. Three numbers are entered through keyboard. The first if
statement compares the first number with the second number. If the condition is true, the block following
the first if statement executes. Inside the block, ifstatement checks whether the first number is larger
than the third number. If yes, then, the largest number is the first one and the same is displayed. Else the
third number is the largest and the same is printed. In case, the first ifstatement fails for satisfying the
condition the else block with nested ifwould be executed. The third number is compared with the
second number. If it is true, the third number is the largest; otherwise, the second number is the largest.
16. Is it possible to use multiple else statements with if statement?
Ans: Any if statement can be paired with one and only one else statement; therefore, it is not
possible to link an if statement with more than one else statement. In such case, we can use nested
if-else statement or if-else-if ladder to solve the problem.
17. Is it possible to use multiple default statements in switch statement?
Ans: We cannot use multiple default statements in a single-switch statement, because
default statement is executed when the switch variable does not match with any of the cases; there-
fore, it is meaningless to include more than one default statement in a single-switch statement.
There can be more than one default statements in a single-switch statement if it is a nested
switch statement and all the default statements will be at different level of nesting.
18. Write the use of elseand defaultstatements in if-elseand switchstatements,
respectively.
Ans: The else statement is used with if statement. There are some statements that we want to execute
on the failure of some condition. All such statements are enclosed in the block followed by else statement.
The default statement is used with the switch statement. The default is used when the case
variable mismatches with any of the cases present in the switch statement. In such a case, all state-
ments are written under the default statement.
19. Why goto statement is avoided?
Ans: Using goto statement control of execution of the program is passed to the label that can be
placed anywhere in the program. As the labels can be spread anywhere in the program, it is very difficult
to monitor the flow of control in the program, which may lead to logical errors.
20. What is the use of the keyword break?
Ans: The keyword break allows the programmers to terminate the loop. The break skips from the
loop or block in which it is defined. The control then automatically goes to the first statement after the
loop or block. The break can be associated with all conditional statements.
M05_ITL-ESL4791_02_SE_C05.indd 83 12/22/2012 4:58:54 PM
II-84 Programming Concepts
We can also use break statements in the nested loops. If we use break statement in the innermost
loop, then the control of the program is terminated only from the innermost loop.
21. Why the break statement is essential in the switch statement?
Ans: In switch statement, we find a number of cases written one after another. When the case
variable matches with any of the cases, the control passes to that case and execution begins from the
statement following to that case. If we do not write the break statement after each case, the control
starts executing all the cases written after the case that has been matched; therefore, to avoid this situation,
we have to write break statement after each case.
22. Is it permissible to use break statement outside the loop?
Ans: One should not use break statement outside the loop. It is used only exiting from the loop.
Therefore, question does not arises to use outside the loop.
23. Which other functions or keywords can be used in place of the break statement?
Ans: The break statement is used to pass the control out of the loop. We can use the goto statement
to pass the control outside the loop by writing a label at the end of the loop statement.
24.  Is it possible to use the else statement in place of a default statement or vice versa?
Ans: The else statement can be used with only the if statement; therefore, we cannot use it at the
place of a default statement.
Moreover, the if statement cannot be paired with any statement other than the else statement;
therefore, we cannot use a default statement in place of the else statement.
25.  Can we put default statement anywhere in the switchcase structure?
Ans: Yes, we can put default statement anywhere in the switch statement, because in switch
statement the sequence of cases does not matter in the execution of the statement.
26. What are the limitations of the switchcase statement?
Ans: The switch case statement can check only for equality; we cannot have any logical expres-
sion in case statement, also we can use only integer or character value, the floating-point values are
avoided in case statements.
27. What is a sequential execution?
Ans: When the control is passed line by line, one statement after another in the sequence without
any jump, or looping without skipping any of the statements in the sequence, such an execution is called
sequential execution.
28. What is the transfer of control?
Ans: The transfer of control means changing the statement that is going to execute next in the sequence.
29. Write short note on goto statement?
Ans: This statement does not require any condition. This is unconditional control jump. This state-
ment passes control anywhere in the program, i.e., control is transferred to another part of the program
without testing any condition. User has to define goto statement as follows:
goto label;
where the label name must start with any character.
M05_ITL-ESL4791_02_SE_C05.indd 84 12/22/2012 4:58:54 PM
Decision Statements II-85
30. Write  a  program  to  detect  whether the  entered  number  is  even or  odd. Use  goto
statement.
Ans:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int x;
clrscr();
printf("Enter a Number :");
scanf("%d",&x);
if(x%2==0)
goto even;
else
goto odd;
even:
printf(" %d is Even Number.");
return;
odd:
printf(" %d is Odd Number.");
}
OUTPUT:
Enter a Number : 5
5 is Odd Number.
Explanation: In the above program, a number is entered. The number is checked for even or odd with
modules division operator. When the number is even, the goto statement transfers the control to the
label even. Similarly, when the number is odd, the goto statement transfers the control to the label odd
and respective message will be displayed.
31. Why continue statement is used in the program?
Ans: Loop interruption statement is done with continue. The continue statement is used
within the loop control statements such as while or do while. It skips all the statements following
the continue and transfers the control to the start of the loop.
32. What is the difference between break and continue?
break continue
It is used to exit from current block or loop. It is used to skip the current loop iteration and return
the control to the next loop statement /iteration.
Terminates the loop. Never terminates the loop/program.
Control passes at the beginning of loop. Control passes to the next statement.
M05_ITL-ESL4791_02_SE_C05.indd 85 12/22/2012 4:58:54 PM
II-86 Programming Concepts
33. What is the difference between break and exit()?
Ans:
S.No.
break exit()
1 It is a keyword. It is a function.
2 No header file is needed. Header file process.h must be included.
3 It stops the execution of the loop. It terminates the program.
4 It does not require the header file process.h. It requires header file process.h.
34. What is the difference between switch case and nestedifs?
Ans:
switch case nested ifs
The switch can only test for equality, i.e., only constant values
are applicable.
The if can evaluate relational or logical expressions.
No two case statements have identical constants in the same
switch.
Same conditions may be repeated for number of times.
Character constants are automatically converted to integers. Character constants are automatically converted to integers.
In switch case statement nested if can be used. In nested if statement switch case can be used.
35. When to use switch statement?
Ans: Switch statement can be used instead of multiple if-else/if conditions. The switch statement
is used when there are more than one conditional expression based on single variable of integer or character type.
36.  Explain in brief the syntax of switch case ?
Ans: The syntax of the switch case statement is as given below.
switch(variable or expression)
{
case constant A :
statement;
break;
case constant B :
statement;
break;
default :
statement ;
}
(a) The switch expression:
In the block, the variable or expression can be a character or an integer. The integer expression
following the keyword switch will yield an integer value only. The integer may be any value 1,
2, 3 etc. In case if a character is constant, the values may be with alphabetic such as x’, y and z’.
(b) The switch organization:
The switch expression should be terminated neither with (;) semicolon nor with any other symbol.
The entire case structure following switch should be enclosed with curly braces. The keyword
case is followed by a constant. Every constant terminates with a colon (:). Each case statement
must contain different constant values. Any number of case statements can be provided. If the case
M05_ITL-ESL4791_02_SE_C05.indd 86 12/22/2012 4:58:54 PM
Decision Statements II-87
structure contains multiple statements, they need not be enclosed with curly braces. Here, the
keyword case &breakperforms the job of opening and closing curly braces, respectively.
(c) The switch execution:
When one of the cases satisfies, the statements following it are executed. In case there is no
match, the default case is executed. The default can be put anywhere in the switch
expression. The switch statement can also be written without default statement. The
break statement used in switch passed control outside the switch block. By mistake if no
break statements are given all the cases following it are executed.
37.  Draw the flowchart for switchcase?
default:
Switch
expression
case
constant 0:
Statement1;
break
case
constant 1:
End of Switch
Statement1; break
Statement1; break
Figure 5.4 Flowchart for switch statement
38. Write a program using multiple if-elseconditions and convert the same program 
using switch statements.
Ans: Program using multiple if conditions.
#include <stdio.h>
#include <conio.h>
void main()
{
char x;
clrscr();
printf(" Enter any alphabet from (a,b,c)");
M05_ITL-ESL4791_02_SE_C05.indd 87 12/22/2012 4:58:55 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset