Most statements in a typical C program are simple statements of this form. Other examples of simple statements are the jump statements return, break, continue, and goto.A return statement specifies the return value for a function (if there is one), and when executed it causes the function to exit immediately. The break and continue statements jump immediately to the end of a loop (or switch. The if else ladder statement in C programming language is used to test set of conditions in sequence. An if condition is tested only when all previous if conditions in if-else ladder is false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder. The if-else statement in C language is used to execute the code if condition is true or false. It is also called two-way selection statement. Decision making is about deciding the order of execution of statements based on certain conditions. C language handles decision-making by supporting if-else, switch, goto and conditional operator.
C control statements test paper 3 contains questions from decision statement: if-else and switch, loop statement: for loop, while loop & do-while loop and jump statement: break and continue.
11) Which statements are correct about an if-else statement in a C-program?
- Nested if-else statements are allowed
- Every if-else statement can be replaced by an equivalent statement using ?: operators
- Multiple statement in else block are allowed
- Multiple statement in if block are allowed
- 1, 3 and 4
- 1, 2, 3 and 4
- 2 , 3and 4
- 1 and 4
The correct option is (a).
Explanation:
Nested if-else statement is allowed in C-program we can use if-else statement within if or else statement.
Multiple statements in if or else block are allowed because we can execute multiple statements against true value of if or else condition by placing the statements within { ?.. }.
Mostly if-else statement can be replaced by ternary operator but there are some exceptions also in which if-else statement cannot be replaced by ternary operator.
Therefore 1, 3 and 4 statements are correct about if-else statement.
12) Find out the error, if any in the below program?
- Error: Constant expression required at line case P:
- Error: There is no break statement in each case
- Error: No default value is specified
- No error
The correct option is (a).
Explanation:
On compiling the program compiler will report an error 'constant expression required' in the line case P: because variable name is not allowed to be used with case statements.
The case statements only accept constant expression. Therefore the Error: Constant expression required at line case P: is occur.
13) Find out the error, if any in the below program?
- Error: in switch statement
- Error: in case 1*2+4 statement
- Error: No default specified
- No Error
The correct option is (d).
Explanation:
In switch statement constant expression are allowed therefore in case 1*2+4 statement it will give no error.
Therefore it prints 'Case1' in the output of program.
14) A long integer is at least 32 bits wide and a short integer is at least 16 bits wide
- True
- False
The correct option is (a).
Explanation:
The basic C compiler used is 16 bit compiler, below are the size of their data types:
The size of long int is 4 bytes wide i.e. 32 bits.
The size of short int is 2 bytes wide i.e. 16 bits
15) A char variable can store either a Unicode character or an ASCII character.
- True
- False
The correct option is (a).
Explanation:
Yes, char variable is allowed to store either a Unicode character or an ASCII character because encoding of character data type is done in Unicode or ASCII format.
Conditional statements, also known as selection statements, are used to make decisions based on a given condition. If the condition evaluates to True, a set of statements is executed, otherwise another set of statements is executed.
The if Statement: The if statement selects and executes the statement(s) based on a given condition. If the condition evaluates to True then a given set of statement(s) is executed. However, if the condition evaluates to False, then the given set of statements is skipped and the program control passes to the statement following the if statement. The syntax of the if statement is
The if-else Statement: The if – else statement causes one of the two possible statement( s) to execute, depending upon the outcome of the condition.
The syntax of the if-else statements is
Here, the if-else statement comprises two parts, namely, if and else. If the condition is True the if part is executed. However, if the condition is False, the else part is executed.
To understand the concept of the if -else statement, consider this example.
Example : A code segment to determine the greater of two numbers
Nested if-else Statement: A nested if-else statement contains one or more if-else statements. The if else can be nested in three different ways, which are discussed here.
• The if – else statement is nested within the if part.
The syntax is
• The if-else statement is nested within the else part.
The syntax is
• The if-else statement is nested within both the if and the else parts.
The syntax is
To understand the concept of nested if-else, consider this example.
Example : A code segment to determine the largest of three numbers
The if-else-if ladder, also known as the if-else-if staircase, has an if-else statement within the outermost else statement. The inner else statement can further have other if-else statements.
C# If Else Example
The syntax of the if -e1se- if ladder is
To understand the concept of the if-else-if ladder, consider this example.
Example: A program to determine whether a character is in lower-case or upper case
The output of this program is
Enter an alphabet: $
It is not an alphabet
Conditional Operator as an Alternative: The conditional operator ‘? :’ selects one of the two values or expressions based on a given condition. Due to this decision-making nature of the conditional operator, it is sometimes used as an alternative to if-else statements. Note that the conditional operator selects one of the two values or expressions and not the statements as in the case of an if-else statement. In addition, it cannot select more than one value at a time, whereas if-else statement can select and execute more than one statement at a time. For example, consider this· statement.
This statement assigns maximum of x and y to max
The switch Statement: The switch statement selects a set of statements from the available sets of statements. The switch statement tests the value of an expression in a sequence and compares it with the list of integers or character constants. When a match is found, all the statements associated with that constant are executed.
The syntax of the switch statement
The C++ keywords case and default provide the list of alternatives. Note that it is not necessary for every case label to specify a unique set of statements. The same set of statements can be shared by multiple case labels.
The keyword default specifies the set of statements to be executed in case no match is found. Note that there can be multiple case labels but there can be only one default label. The break statements in the switch block are optional. However, it is used in the switch block to prevent a fall through. Fall through is a situation that causes the execution of the remaining cases even after a match has been found. In order to prevent this, break statements are used at the end of statements specified by each case and default. This causes the control to immediately break out of the switch block and execute the next statement.
To understand the concept of switch statement, consider this code segment.
Example : A code segment to demonstrate the use of switch statement
C++ If Statement Example
In this example, depending upon the input, an appropriate message is displayed. That is, if 2 are entered, then the message Option 2 is selected is displayed. In case, 5 is entered, then the message Invalid option! is displayed.
Multiple If Else Statements Java
Similar to if and if-else statements, switch statements can also be nested within one another. A nested switch statement contains one or more switch statements within its case label or default label (if any).