There are basically 3 types of decision control statements.
The ‘if’ statement
The format for if statement is if (expression) Statement. This is the simplest form of ‘if’ statement. The expression is to be placed in parenthesis. It can be any logical expression. The statement can be either a simple statement or a compound statement. The statement following the “if clause” constitutes the if-block.
If the expression given in brackets evaluates to true (1), the statement is executed. If it evaluates to false the statement is skipped and execution continues after the “if block”. Consider a simple example showing use of the if statement,
/* program to demonstrate use of if-statement
Body of if-statement is executed only if expression results into true */
Program accepts a number from the user and output the number entered. (j>100) is a logical expression which uses a variable operand j (whose value can be any integer entered by user) and a constant 100 as its second operand. The greater than (>) operator (relational operator) connects the two operands to form a logical expression. It evaluates to true only if number j entered by user is greater than 100. If the number entered is greater than 100, the message is printed; if not then the statement is skipped.
In the “if” statement seen earlier, we can take some action if expression is true. But if expression is false there is no action. We can include an action for both conditions (i.e. true or false) by using if-else statement. The general form which is
If expression evaluates to true, statement 1 is executed, while if expression evaluates to false, statement 2 is executed. Statement 1 and statement 2 can again be single statement or compound statements.
The following program illustrates the use of if-else statement.
Program accepts a character from user and informs whether it is a vowel or a consonant. The logical expression compares the entered character (ch) with each vowel to see if it matches with any one of them. It uses the equality operator (==) for comparison and OR (||) operator for checking with all vowels. If ch matches with any vowel, expression is true and ‘if’ block is executed. If it does not match with any vowel, expression is false and ‘else’ block is executed.
It is possible to nest if-else statement within one another. An if-else statement can occur within another if-else statement. The inner if-else is said to be nested in the outer if-else. Nesting can go upto any level. There are several forms that nested if-else statement can take.
Here e1, e2, e3 are expressions and s1, s2, s3, s4 represent statements. One complete if-else will be executed if e1 is true and another complete if-else will be executed if e1 is false.
In the 5th form, to which ‘if’ does the ‘else’ belong to? It belongs to e2. The general rule is, ‘else’ clause is always associated with the closest, preceding, unmatched (else-less) ‘if’. If we want to associate ‘else’ with ‘if’ of e1, we can write.
The brace brackets change the pair of else. If we stretch Example (2) to include digits as well as space character, newline character, the program can be written as
/* program to show nesting of if-else statement */
It is of form (3) format. There is no limit on how deeply the ‘if’s and the ‘else’s can be nested.
switch – case
Another form of statement available for selective execution is the switch statement. It causes particular group of statements to be selected from several available group. The general format is as follows.
The expression should result in an integer value or character value. First the expression following switch is solved.The result is then matched with each case expression (i.e. expression1, expression2….expression n). The statement following the matching case expressions are executed. If no values of case expression match with value of switch expression then statement following default label are executed. Else control is transferred directly to the statement that follows switch statement. Consider the following program. Default is used to handle errors; cases which may pop up unexpectedly (like user entering value which may not be listed in our case expressions).
The expression should result in an integer value or character value. First the expression following switch is solved. The result is then matched with each case expression (i.e. expression1, expression2….expression m).
The statement following the matching case expressions are executed. If no value of case expression matches with value of switch expression then statement following default label are executed. Else control is transferred directly to the statement that follows switch statement. Consider the following program. Default is used to handle errors; cases which may pop up unexpectedly (like user entering value which may not be listed in our case expressions)
The output will be
I am in case 2.
I am in case 3.
I am in default.
Since j = 2 and it matches with case 2. We would expected the output to be
I am in case 2.
But it is not the thing. Thus switch executes the case where a match is found and all the subsequent cases and default as well. This is called the fall through. Fall through is helpful if we want the same action for number of values. But if we want switchto exist when action for the match is carried out, we have to use a break statement as follows.
/* program to demonstrate use of switch- case and need of break in switch statements */
Break causes exit from the switch statement. Hence above program gives output as “I am in case 2”.
/* program demonstrates use of switch-case statement. */
/* Using switch- case instead of nested if-else*/
Switch can be replacement for if-else statement. But it does not provide facility for logical expressions. i.e. Expression like (count>1) or (count<10) are not allowed as case expressions. The expression should result in a specific value not range of values. Hence we have to replace the expression ((ch –‘0’) > 0) && ((ch –‘0’) <9)) byCase 1: case2: …case 9:
C Related Interview Questions
|DBMS Interview Questions||C++ Interview Questions|
|C & Data Structures Interview Questions||Java Interview Questions|
|Go (programming language) Interview Questions||F Sharp (programming language) Interview Questions|
|C preprocessor Interview Questions||R Programming language Interview Questions|
|D Programming Language Interview Questions||Lua (programming language) Interview Questions|
|Embedded C Interview Questions|
Introduction To C-programming
Data Types And Storage Classes
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.