Tutorials
C LanguageWhen a series of many conditions have to be checked we may use the ladder else if statement which takes the following general form.
| if
(condition1) statement – 1; else if (condition2) statement2; else if (condition3) statement3; else if (condition) statement n; else default statement; statement-x; |
This construct is known as if else construct or ladder. The conditions are evaluated from the top of the ladder to downwards. As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement – x (skipping the rest of the ladder. When all the condition becomes false, the final else containing the default statement will be executed.
|
Marks |
Grade |
|
70 to 100 |
DISTINCTION |
The above program checks a series of conditions. The program begins from the first if statement and then checks the series of conditions it stops the execution of remaining if statements whenever a condition becomes true.
In the first If condition statement it checks whether the input value is lesser than 100 and greater than 70. If both conditions are true it prints distinction. Instead if the condition fails then the program control is transferred to the next if statement through the else statement and now it checks whether the next condition given is whether the marks value is greater than 60 If the condition is true it prints first class and comes out of the If else chain to the end of the program on the other hand if this condition also fails the control is transferred to next if statements program execution continues till the end of the loop and executes the default else statement fails and stops the program.
Unlike the If statement which allows a selection of two alternatives the
switch statement allows a program to select one statement for execution out of a
set of alternatives. During the execution of the switch statement only one of
the possible statements will be executed the remaining statements will be
skipped. The usage of multiple If else statement increases the complexity of the
program since when the number of If else statements increase it affects the
readability of the program and makes it difficult to follow the program. The
switch statement removes these disadvantages by using a simple and straight
forward approach.
The general format of the Switch Statement is :
| Switch
(expression) { Case case-label-1; Case case-label-2; Case case-label-n; ……………… Case default } |
When the switch statement is executed the control expression is evaluated first and the value is compared with the case label values in the given order. If the label matches with the value of the expression then the control is transferred directly to the group of statements which follow the label. If none of the statements matches then the statement against the default is executed. The default statement is optional in switch statement in case if any default statement is not given and if none of the condition matches then no action takes place in this case the control transfers to the next statement of the if else statement.
In the above program the break statement is need after the case statement to break out of the loop and prevent the program from executing other cases.
The goto statement is simple statement used to transfer the program control
unconditionally from one statement to another statement. Although it might not
be essential to use the goto statement in a highly structured language like C,
there may be occasions when the use of goto is desirable.
Syntax
|
The goto requires a label in order to identify the place where the branch is to be made. A label is a valid variable name followed by a colon.
The label is placed immediately before the statement where the control is to be transformed. A program may contain several goto statements that transferred to the same place when a program. The label must be unique. Control can be transferred out of or within a compound statement, and control can be transferred to the beginning of a compound statement. However the control cannot be transferred into a compound statement. The goto statement is discouraged in C, because it alters the sequential flow of logic that is the characteristic of C language.
First Page: C Programming - Decision Making - Branching
| a real good explanation about all the topics covered(exellenet) |
| all the topics are better explained in this book |
| this is the best |
| its a very clear explanation. thank you! its great! keep up. |
| Thank's for detail |
| very gud xplanation.... |