|
Page 2 of 2
The ELSE If Ladder
When 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.
/* Example program using If else ladder to grade the student according to the
following rules.
|
Marks |
Grade |
|
70 to 100
60 to 69
50 to 59
40 to 49
0 to 39
|
DISTINCTION
IST CLASS
IIND CLASS
PASS CLASS
FAIL
|
Example program using If else ladder to grade the student #include <stdio.h> //include the standard stdio.h header file void main () //start the function main { int marks; //variable declaration printf ("Enter marks\n"); //message to the user scanf ("%d", &marks); //read and store the input marks. if (marks <= 100 && marks >= 70) //check whether marks is less than 100 or greater than 70 printf ("\n Distinction"); //print Distinction if condition is True else if (marks >= 60) //else if the previous condition fails Check printf("\n First class"); //whether marks is > 60 if true print Statement else if (marks >= 50) //else if marks is greater than 50 print printf ("\n second class"); //Second class else if (marks >= 35) //else if marks is greater than 35 print printf ("\n pass class"); //pass class else printf ("Fail"); //If all condition fail apply default condition print Fail }
%23include%20%3Cstdio.h%3E%20%2F%2Finclude%20the%20standard%20stdio.h%20header%20file%0D%0Avoid%20main%20%28%29%20%2F%2Fstart%20the%20function%20main%0D%0A%7B%0D%0A%20%20%20int%20marks%3B%20%2F%2Fvariable%20declaration%0D%0A%0D%0A%20%20%20printf%20%28%22Enter%20marks%5Cn%22%29%3B%20%2F%2Fmessage%20to%20the%20user%0D%0A%20%20%20scanf%20%28%22%25d%22%2C%20%26marks%29%3B%20%2F%2Fread%20and%20store%20the%20input%20marks.%20%0D%0A%0D%0A%20%20%20if%20%28marks%20%3C%3D%20100%20%26%26%20marks%20%3E%3D%2070%29%20%2F%2Fcheck%20whether%20marks%20is%20less%20than%20100%20or%20greater%20than%2070%20%0D%0A%20%20%20%20%20%20%20%20%20printf%20%28%22%5Cn%20Distinction%22%29%3B%20%20%2F%2Fprint%20Distinction%20if%20condition%20is%20True%0D%0A%20%20%20else%20if%20%28marks%20%3E%3D%2060%29%20%2F%2Felse%20if%20the%20previous%20condition%20fails%20Check%0D%0A%20%20%20%20%20%20%20%20%20printf%28%22%5Cn%20First%20class%22%29%3B%20%2F%2Fwhether%20marks%20is%20%3E%2060%20if%20true%20print%20Statement%0D%0A%20%20%20else%20if%20%28marks%20%3E%3D%2050%29%20%2F%2Felse%20if%20marks%20is%20greater%20than%2050%20print%0D%0A%20%20%20%20%20%20%20%20%20printf%20%28%22%5Cn%20second%20class%22%29%3B%20%2F%2FSecond%20class%0D%0A%20%20%20else%20if%20%28marks%20%3E%3D%2035%29%20%2F%2Felse%20if%20marks%20is%20greater%20than%2035%20print%0D%0A%20%20%20%20%20%20%20%20%20printf%20%28%22%5Cn%20pass%20class%22%29%3B%20%2F%2Fpass%20class%0D%0A%20%20%20else%0D%0A%20%20%20%20%20%20%20%20%20printf%20%28%22Fail%22%29%3B%20%2F%2FIf%20all%20condition%20fail%20apply%20default%20condition%20print%20Fail%0D%0A%7D
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.
The Switch Statement:
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.
A program to stimulate the four arithmetic operations using switch #include <stdio.h> void main () { int num1, num2, result; char operator; scanf ("%d %d", &num1, &num2); scanf ("%c", &operator); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) result = num1 / num2; else { printf ("warning : division by zero \n"); result = 0; } break; default: printf ("\n unknown operator"); result = 0; break; } }
%23include%20%3Cstdio.h%3E%20%0D%0Avoid%20main%20%28%29%20%0D%0A%7B%20%20%0D%0A%20%20%20int%20num1%2C%20num2%2C%20result%3B%20%0D%0A%20%20%20char%20operator%3B%0D%0A%0D%0A%20%20%20printf%20%28%22Enter%20two%20numbers%22%29%3B%20%0D%0A%20%20%20scanf%20%28%22%25d%20%25d%22%2C%20%26num1%2C%20%26num2%29%3B%20%0D%0A%20%20%20printf%20%28%22Enter%20an%20operator%22%29%3B%20%0D%0A%20%20%20scanf%20%28%22%25c%22%2C%20%26operator%29%3B%20%0D%0A%0D%0A%20%20%20switch%20%28operator%29%20%0D%0A%20%20%20%7B%0D%0A%20%20%20%20%20%20%20case%20%27%2B%27%3A%20%0D%0A%20%20%20%20%20%20%20%20%20%20result%20%3D%20num1%20%2B%20num2%3B%20%0D%0A%20%20%20%20%20%20%20%20%20%20break%3B%20%0D%0A%20%20%20%20%20%20%20case%20%27-%27%3A%20%0D%0A%20%20%20%20%20%20%20%20%20%20result%20%3D%20num1%20-%20num2%3B%20%0D%0A%20%20%20%20%20%20%20%20%20%20break%3B%20%0D%0A%20%20%20%20%20%20%20case%20%27%2A%27%3A%20%0D%0A%20%20%20%20%20%20%20%20%20%20result%20%3D%20num1%20%2A%20num2%3B%20%0D%0A%20%20%20%20%20%20%20%20%20%20break%3B%20%0D%0A%20%20%20%20%20%20%20case%20%27%2F%27%3A%20%0D%0A%20%20%20%20%20%20%20%20%20%20if%20%28num2%20%21%3D%200%29%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20num1%20%2F%20num2%3B%20%0D%0A%20%20%20%20%20%20%20%20%20%20else%0D%0A%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20printf%20%28%22warning%20%3A%20division%20by%20zero%20%5Cn%22%29%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%200%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20break%3B%0D%0A%20%20%20%20%20%20%20default%3A%20%0D%0A%20%20%20%20%20%20%20%20%20%20printf%20%28%22%5Cn%20unknown%20operator%22%29%3B%0D%0A%20%20%20%20%20%20%20%20%20%20result%20%3D%200%3B%0D%0A%20%20%20%20%20%20%20%20%20%20break%3B%20%0D%0A%20%20%20%7D%0D%0A%20%20%20printf%20%28%22%25d%22%2C%20result%29%3B%20%0D%0A%7D
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:
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
a>
goto label;
Label;
Statement; |
b>
label;
goto label;
|
|
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.
A program to find the sum of n natural numbers using goto statement #include <stdio.h> //include stdio.h header file to your program main () //start of main { int n, sum = 0, i = 0; // variable declaration printf ("Enter a number"); // message to the user scanf ("%d", &n); //Read and store the number loop: i++; //Label of goto statement sum += i; //the sum value in stored and I is added to sum if (i < n) goto loop; //If value of I is less than n pass control to loop printf ("\n sum of %d natural numbers = %d", n, sum ); //print the sum of the numbers & value of n }
%23include%20%3Cstdio.h%3E%20%2F%2Finclude%20stdio.h%20header%20file%20to%20your%20program%0D%0Amain%20%28%29%20%2F%2Fstart%20of%20main%0D%0A%7B%0D%0A%20%20%20%20%20int%20n%2C%20sum%20%3D%200%2C%20i%20%3D%200%3B%20%2F%2F%20variable%20declaration%0D%0A%20%20%20%20%20printf%20%28%22Enter%20a%20number%22%29%3B%20%2F%2F%20message%20to%20the%20user%0D%0A%20%20%20%20%20scanf%20%28%22%25d%22%2C%20%26n%29%3B%20%2F%2FRead%20and%20store%20the%20number%0D%0A%20%20%20%20%20loop%3A%20i%2B%2B%3B%20%2F%2FLabel%20of%20goto%20statement%0D%0A%20%20%20%20%20sum%20%2B%3D%20i%3B%20%2F%2Fthe%20sum%20value%20in%20stored%20and%20I%20is%20added%20to%20sum%20%0D%0A%20%20%20%20%20if%20%28i%20%3C%20n%29%20goto%20loop%3B%20%2F%2FIf%20value%20of%20I%20is%20less%20than%20n%20pass%20control%20to%20loop%0D%0A%20%20%20%20%20printf%20%28%22%5Cn%20sum%20of%20%25d%20natural%20numbers%20%3D%20%25d%22%2C%20n%2C%20sum%29%3B%0D%0A%20%20%20%20%20%2F%2Fprint%20the%20sum%20of%20the%20numbers%20%26%20value%20of%20n%0D%0A%7D%20
Trackback(0)

|