C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
51.Explain the role of break statement in switch case. What happens if break statement is not used in any case of switch statement?
Break statement is used to skip the part of code in switch case statement. When one case is true then break statement of that case will cause other remaining case not to be checked. But if the break statement is not used then each case will be checked and statements within true case along with the default case will get executed.
28
52. Write the general form and working flow of conditional operator?
The general form of conditional operator is given as:
Variable = expression1 ? expression2 : expression 3;
Here, expression 1 is a condition while expression 2 and expression 3 can be arithmetic, logical or conditional itself. If the condition is true then the value given by expression2 will be assigned to the variable, else value resulted by expression 3 will be assigned.
For example:
X = a>4 ? (a+3): (a-2)
Assuming, a = 5, since 5>4 is true, (a+3) will be assigned to X. so, X = 8
53. Explain why repetitive structures are required?
When certain operations are needed to be done repeatedly then we need a mechanism to repeat such operations and it is made possible by using repetitive structures. The operations may be input,
procession or output depending upon the problem.
54. What are the basic requirements for operating a loop? What is running variable?
There are three basic requirements for loop to operate.
•Initialization: starting point of the loop
•Condition: terminating point of the loop
•Update: represents how the value changes from starting to terminating point
Running variable is one that is used to operate the loop. It is used in initialization, condition as well as in update.
55. Write the syntax for compound statements, working flow and flowchart for
▪For loop
▪While loop
▪Do while loop
The various types of loop are explained below:
a.For loop: Syntax:for(initialization;condition;update)
{
|
statements;
|
//Body of loop;
|
Initialization
False
Condition
Update
condn
True
Statements
}
Figure 1: Flowchart of for loop
29
Working flow with example:
for ( x = 1 ; x < = 10 ; x + + )
F
T
printf(“%d”, x);
}
b.while loop: Syntax:
initialization;
while(condition)
{
Statements;
Update;
}
Initialization
False
Condition
Update
condn
True
Statements
|
Working flow with example:
|
Figure 2: Flowchart of while loop
|
x = 1;
while ( x < = 10 )
{T
printf(“%d”, x);
|
F
|
x + +;
|
}
30
c.do while loop: Syntax:
Initialization; do
{
Statements;
Update;
}while(condition);
Working flow with example:
x = 1;
do
{
} while (
|
Initialization
|
||||||
|
False
|
True
|
|||||
|
Condition
|
Statements
|
|||||
|
condn
|
||||||
|
Update
|
||||||
Figure 3: Flowchart of do while loop
|
printf(“%d”, x);
|
|
|
x + +;
|
T
|
|
x < = 10 );
|
F
|
56. When should we use for loop? Explain how it differs from while loop.
Usually, for loop is used when the number of iteration to be done is known in advance. Let us take an example of finding the sum of 10 numbers. In this case, we have prior information that the sum needs to be repeated 10 times, hence for loop is better to use. All kind of loop, however, can be used interchangeably. To differentiate for loop with while loop, for loop is used when there is prior information about how many times a loop to be executed. On the other hand, while loop is used
when there is no prior information about how many times a loop will get executed.
57. Explain when is while loop used? What happens if the initialization of running variable is done within the body of loop? And what results if the update of running variable is not done?
When the number of iteration is not known in advance rather it is known during execution of program. For instance, to determine the sum of all digits in any given integer, the number of times
31
the sum to be done will be determined by the input given by user during execution. If the entered number is 3 digit then sum is to be done 3 times, if the number is of 5 digits then sum should be done 5 times. Since, in this problem the number of iteration is not fixed since user can give input of any length. Hence, it is better to use while loop in such case as this.
Initialization of running variable should be done before its corresponding loop; otherwise it will lead to infinite loop. Similarly, update should be done within the body of loop; else it results in infinite loop.
58. Distinguish between while and do while loop.
The following table shows the difference between while and do while loop
|
SN
|
while loop
|
do while loop
|
|
1
|
Entry Controlled Loop
|
Exit Controlled Loop
|
|
2
|
Condition is checked before the execution
|
Condition is checked at the end of the after
|
|
of statements within loop
|
the execution of statements
|
|
|
3
|
Body of loop may or may not execute
|
Body of loop will execute at least once
|
|
4
|
In syntax, while terminated by semicolon
|
while is terminated using semicolon as a part
|
|
gives a different meaning or can give error
|
of syntax; gives error if semicolon is not used
|
|
|
5
|
Its flowchart is:
|
Its flowchart is:
|
|
Entry
|
Entry
|
|
|
False
|
Body of Loop
|
|
|
Condition
|
||
|
True
|
||
|
Condition
|
False
|
|
|
Body of Loop
|
||
|
True
|
||
|
Exit
|
Exit
|
|
59. Explain briefly about break and continue statement with example
Break and continue statements are, in general, used in loops to exit the loop and to skip the certain statements respectively.
Break Statement: Whenever there is a break statement in a loop, then that particular loop will get terminated after the execution of break statement.
32
The following diagram demonstrates the use of break statement.
x = 0;
while ( x < = 10 )
{T
x + +;
|
F
|
if (
|
x == 7
|
)
|
T
break; F
printf(“%d ”, x);
}
Output:
1 2 3 4 5 6
In this example, the loop will terminate when the value of x becomes 7. So, only values from 1 to 6 will be displayed.
Continue Statement: Whenever continue statement gets executed in a loop, then the statements
below the continue statement with not execute but it continues another iteration of the loop.
The following flow diagram demonstrates the use of continue statement in a loop.
x = 0;
while ( x < = 9 )
{T
x + +;
|
F
|
if (
|
x == 5
|
)
|
T continue;
F
printf(“%d ”, x);
Output:
1 2 3 4 6 7 8 9
In this example, the continue statement will skip the statement printf(“%d ”, x) when the value of x becomes 5. But the loop continues for next iteration.
}
33
60. Compare break and continue statements.
The comparison of break and continue statements is as shown below.
|
SN
|
Break Statement
|
Continue Statement
|
||||||||||||||||||||
|
1.
|
Used in loop and switch case
|
Used in loop
|
||||||||||||||||||||
|
2.
|
Terminates the loop, skips the statements
|
Skips the statements in loop but continues the
|
||||||||||||||||||||
|
in switch case
|
loop
|
|||||||||||||||||||||
|
3.
|
Condition is required before break
|
Condition is required before continue
|
||||||||||||||||||||
|
statement, otherwise the loop will run
|
statement, otherwise can lead to infinite loop
|
|||||||||||||||||||||
|
only once and gets terminated
|
||||||||||||||||||||||
|
4.
|
Its flow diagram within a loop
|
Its flow diagram within a loop
|
||||||||||||||||||||
|
Statements
|
||||||||||||||||||||||
|
Statements
|
||||||||||||||||||||||
|
Back to loop
|
||||||||||||||||||||||
|
True
|
||||||||||||||||||||||
|
True
|
||||||||||||||||||||||
|
Back to Loop
|
Condition
|
Back to Loop
|
Condition
|
|||||||||||||||||||
|
condn
|
||||||||||||||||||||||
|
condn
|
||||||||||||||||||||||
|
False
|
Out of Loop
|
|||||||||||||||||||||
|
False
|
||||||||||||||||||||||
|
Statements
|
||||||||||||||||||||||
|
Statements
|
||||||||||||||||||||||
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 …
