Loop Constructs Short
Questions and Answers. This is the first
post for Computer ICS Part-2 | Computer
Science 2nd Year Chapter No.12 Loop Constructs. Find the most
important short questions with answers for the final exams.
We tried our best to choose the most important and conceptual
short questions and answers from this Chapter No.12: Loop Constructs in C
language. This post covers the topics i.e., introduction to
iteration/repetition/loop, while loop, do-while loop, for loop, nested loop,
sentinel value, sentinel loop, and conditional loop in C programming language.
Chapter No.12: Loop Constructs
Short Questions and Answers
Q.1: What is iteration (loop or repetition)?
Answer:
Iteration (loop or repetition) is the third control structure, which
repeats a statement or group of statements in a program. There are three loop
control statements in C, these are:
- while
- do-while
- for
Q.2: What is a condition?
Answer:
A condition is an expression that either evaluates to true (1) or false
(0). The result of this evaluation can be assigned to a variable
Q.3: What is while statement?
Answer:
The while loop keeps repeating associated statements until the specified
condition becomes false. This is useful where the programmer does not know in
advance how many times the loop will be traversed.
Syntax of the while statement is:
while (condition)
{
Statement(s);
}
Q.4: What is loop control variable?
Answer:
A variable whose value controls the number of iterations is known as a loop control variable.
Q.5: Define do while loop?
Answer:
This is very similar to the while loop except that the test occurs at the
end of the loop body. This guarantees that the loop is executed at least once. This
loop is frequently used where data is to be read, the test then verifies the
data, and loops back to read again if it was unacceptable.
Syntax:
do
{
statement(s);
} while(condition);
Q.6: Define the keyword “do” of the do-while statement?
Answer:
The keyword "do" let the program flow to move into the body
of the loop without checking any test condition. It means, whatever is written
in the loop body will always be executed at least once.
Q.7: Differentiate between while and do-while loop?
Answer:
While Loop: In the while loop, the body of the loop
may or may not execute depending on the evaluation of the test condition.
Do-while Loop: In the do-while loop, first the body of
the loop is executed and then the test condition is checked. Hence, it always
executed at least once.
Q.8: What is for loop?
Answer:
The for statement is another way of implementing loops in C. Because of
its flexibility, most programmers prefer the for statement to implement loops. For
loop is also known as counter loop.
Syntax:
for(initialization ;
condition ; increment / decrement)
{
statement(s);
}
Q.9: How many times the following loop will execute?
void main(void)
{
int count=1;
while(count<10)
{
printf(“%d”,count);
count++;
}
}
Answer:
The loop in the above code will execute 9 times.
Q.10: What is a counter loop?
Answer:
A common type of program loop is one that is controlled
by an integer that counts up from an initial value to an upper limit, such a loop is
called a counting loop.
Q.11: Write a program for the following output?
-3
-1
1
3
Answer:
For the above output, follow the code below:
#include<stdio.h>
void main(void)
{
int i=-3;
while(i<=3)
{
printf("%d\n",i);
i=i+2;
}
}
Q.12: What is sentinel value?
Answer:
In computer programming, a sentinel value (also referred to as a flag
value) is a special value that uses its presence as a condition of termination,
typically in a loop or recursive statement.
Q.13: What is a sentinel loop?
Answer:
A sentinel-controlled loop is sometimes called indefinite loop because
it is not known in advance how many times the loop will be executed. It is a loop procedure for solving a
problem by using a sentinel value (signal value, flag value, or dummy
value) to indicate the end of data entry.
Q.14: Differentiate between Sentinel Controlled and Counter Controlled
Loop?
Answer:
Sentinel Controlled Loop:
Sentinel Controlled Loop, exactly how many times loop body will
be executed is not known in an advance.
Counter Controlled Loop:
In a Counter Controlled Loop, how many times loop body will be
executed is known in an advance.
Q.15: Write the output for the following code?
#include<stdio.h>
void main(void)
{
int i=1, count=0;
do
{
--count;
printf("%d\n",count);
i++;
} while(i<=5);
}
Answer:
Output for the above code is as under:
-1
-2
-3
-4
-5
Q.16: What is nested loop?
Answer:
Nested loop means a loop inside the body of another loop. Nesting can be
done up to any level. But, as the level of nesting increases, the complexity of
the program also increases. There is no restriction on the type of loops
(while, do-while, or for) that may be placed in the body of another loop.
Q.17: In nested repetition, which loop will execute a smaller number of
times?
Answer:
In the nested loop, the outer loop will execute a smaller number of times
whereas the inner loop will execute a greater number of times.
Q.18: What is the goto statement?
Answer:
The goto statement performs an unconditional transfer of control to the named
label. The label must be in the same function.
The general form of goto statement is as follows:
goto label;
statement(s);
label:
statement(s);
➤ Loop Constructs - MCQs Set-1
➤ Loop Constructs - MCQs Set-2
➤ Loop Constructs - Exercise Programs
➤ Decision Constructs - MCQs
➤ Decision Constructs - Short Questions
➤ Decision Constructs - Exercise Programs
👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍
ReplyDeletePost a Comment
Your feedback is highly appreciated and will help us to improve. So, please give your good suggestions.