C Programming Question Collection
 Posted by Engineering Helpline Pvt. Ltd
 Categories c programming, C Programming Question With Answers, First Semester
 Date April 1, 2018
 Comments 0 comment
41. What do you mean by selective structure? When are they used?
Selective structures are those statements which are used to change the order of execution of statements based on certain decision out of many conditions. These structures are used when we
have to make decisions; which operation to carry out for a given situation.
42.Write the syntax, working flow and flowchart for the following
•Simple if
•Syntax:
if(condition)

True


Condition


{


True statements;

False


}

True Statements


Next statements;


Next Statements


•Working flow: when the condition is true then the true statements will be executed and then the next statements will follow but if the condition is false then the true statements are bypassed executing next statements.
•if else statement
•Syntax:
if(condition)

False

True


{


Condition


Statements_T ;
}
elseStatements_F Statements_T
{
Statements_F;
}
Next Statements
•Working flow: If the condition is true then Statements_T will execute but in case if the condition is false then the Statements_F will be in execution.
•Examples where if else can be used:
oTo check if the number is even or odd.
oTo determine if a number is greater than another given value.
23
•else if ladder
• Syntax:
if(Condition 1)

True


{

Condition 1

Statements_1


Statement_1;

False


}


else if(Condition 2)


{

True


Statements_2


Statement_2;

Condition 2


}


else if(Condition n)

False


{


Statement_n;

True


}

Condition n

Statements_n


else


{

False


Default_Statements;


Default_Statements


}


•Working Flow:
oWhen particular condition is true, then its corresponding statements will get executed and subsequent remaining conditions will not be checked.
oIf condition 1 is true then its corresponding statement is executed but if it is false then condition 2 is checked.
oIn case, condition 2 results in true then statement_2 will be executed otherwise
condition 3 will be checked and the flow will just go on.
oIf all conditions are checked and none of them are true then, the statement within the else block will be executed.
oThis structure works without the use of else block as well.
•Examples than can use else if ladder:
oTo determine whether the number is positive, negative or zero.
oTo find the relation of two numbers; greater, equal or less.
oTo calculate roots of quadratic equations; real and equal, real and unequal, and imaginary roots
24
•Nested if ladder
• Syntax:
if(condition 1)
{
if(condition 2)
{
Statement 1;
}
}
else
{
If(condition 3)
{
Statement 2;
}
else
{
Statement 3;
}
}
•Flow Diagram:
False

True


Condition1


True

False

False

True


Condition2

Condition3


Statement 1

Statement 3

Statement 2


Next Statements
•Working flow:
o As its structure can vary, the flow can be different based on the type of nested if structure used. Here, we have used simple if within if statement while if else statement within else part to form a nested if structure.
25
oIn above case, first the condition 1 is checked, if it’s true then condition 2 will be
examined and statement 1 will be executed in true case.
oIf the condition 1 is false then condition 3 will be checked, if it is true then statement 2 is executed otherwise statement 3 will be executed.
•Example that can use nested if:
oTo check whether the year is leap year or not
43.In which case, simple if and if else should be used? Give few examples
When only one condition is to be checked then simple if is used. But if there are two conditions which are mutually exclusive (both condition cannot be true at the same time) then if else statement must be used. Look at the following case:
Case 1: To check even or odd
•A number can either be even or odd but cannot be both at the same time, so this condition is a mutually exclusive condition.
•Since there are only two conditions, if else should be used.
Case 2: To find the sum of even numbers out of 10 numbers
•In this problem, only sum of even numbers is required so the condition for odd part is not required, so simple if can be used.
44.When is else if ladder needed?
When more than two mutually exclusive conditions are to be checked then else if ladder is required. Example 1: Determine whether a number is positive, negative or zero.
In this case, there are three mutually exclusive (only one can be true for a given number) conditions, so else if ladder should be used.
Example 2: Check the relation of two number; greater, smaller or equal.
Similarly, in this problem, there are three mutually exclusive conditions, so use else if ladder.
45. What sort of conditions necessitates the use of nested if?
When there are two or more conditions which are not mutually exclusive then nested if should be used.
Example 1: To check whether the number is even and it is greater than 30
Since a number can be even and also be greater than 30, this condition is not mutually exclusive, so in this case, nested if should be used. First check if the number is even or not and then check if it is greater than 30 or not, constructing a nested structure of if.
26
46. Demonstrate the conversion of nested if structure to a else if ladder with the help of logical
operators
Let us take an example to demonstrate the conversion. Write a program to find the largest among 3 numbers. First using nested if
if(a>b)


{


if(a>c)


{


largest = a;

// a>b and a>c

}


else


{


largest = c;

//a>b and c>=a

}


}


else


{


if(b>c)


{


largest = b;

//b>=a and b>c

}


else


{


largest = c;

//b>=a and c>=b

}


}

In above code, the possible conditions can be listed as
a>b and a>c

a largest

a>b and c>=a

c largest

b>=a and b>c

b largest

b>=a and c>=b

c largest

Now the same problem can be solved using else if ladder with the help of logical operators
if(a>b && a>c)
{
largest = a;
}
else if(b>=a && b>c)
{
27
largest = b;
}
else
{
largest = c;
}
47. Write the general form of switch case.
The general form of switch case is:
switch(expression)
{
case constant1:statement1;break;
case constant2:statement2;break;
default:
default statement; break;
}
48. What kind of condition determines whether the switch or if else is to be used?
If the condition given is in the form of range, such as greater than zero, less than zero then it is better to use if else statement, but if the condition to be checked is an integer constant or character
constant then either if else or switch can be used. So, it is always better to use switch case when the given condition is in the form of constants rather than range.
49. What kind of variables or expressions can be used within the expression of switch?
In general, those variables or expressions which results in either integer or character value must be used within the expression of switch.
50. What can be the values of case label/constant in switch case?
It can either be integer constant or character constant.
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …