Loops in c language While For Do While MCQ
Table of Contents
Learn Loops in c language While For Do While MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
Loops help to repeatedly execute a set of instructions as long as loop condition is satisfied. Condition can be a combination of relational operators and logical operators.
Loops in c language
C language comes with three loops.
- while loop
- for loop
- do while loop
C language provides two more keywords BREAK and CONTINUE to provide more control over loops.
break; Statement
break; statement with a semicolon at the end breaks the current loop where it is present. It can not affect outer loop in a nested loop program.
continue; Statement
continue; statement with a semicolon at the end skips executing next statements in the current loop and execution goes to the beginning of loop with next loop counter.
Increment Operator
++ is called Increment operator. It adds +1 to the variable.
Decrement Operator
— is called Decrement operator. It subtracts -1 from the variable.
1. WHILE Loop
‘while’ loop executes a group of statements under the loop until a loop condition is true. Loop counter is maintained to exit the loop after crossing a certain number in most of the cases. If the loop condition fails for the first time itself, no statements are executed. Program control goes to the first statement after the while loop block.
Syntax:
while ( condition ) statements;
Loops in c language examples
Example 1:
int main() { int a=1; while(a <= 10) { printf("%d,", a); a++; //increment or decrement } return 0; } //output: 1,2,3,4,5,6,7,8,9,10
In the above example, we printed all numbers from 1 to 10. If you forget to increment a using a++ statement, loop will never end and the program crashes.
Loops in c language
Example 2: While with break statement
int main() { int a=1; while(true) //true makes while loop infinite loop { printf("%d,", a); a++; if(a>10) break; //breaks the while loop } return 0; } //output: 1,2,3,4,5,6,7,8,9,
Here we are depending on break statement to come out of loop instead of LOOP CONDITION.
Loops examples
Example 3: While with continue statement
int main() { int a=0; while(a<=10) //true makes while loop infinite loop { a++; if(a%2 == 0) printf("%d", a); else continue; //it skips printf("*") print("*"); } return 0; } //output: 2*4*6*8*10
Loops in c language
Example 4: Nesting of While loop
You can nest a while inside another while loop. You can use break and continue with inner and outer loops if required.
int main() { int a=1, b=1; //outer loop while(a <= 3) { b=a; //nested while loop //inner loop while(b <= 3) { printf("%d,", b); b++; } printf("\n"); a++; } return 9; } //output //1,2,3, //2,3, //3,
2. FOR Loop
A FOR loop works like a WHILE loop except that FOR loop includes both INITIALIZATION section and INCREMENT section along with CONDITION section.
Syntax:
for(INITIALIZATION ; CONDITION ; INCREMENT OR DECREMENT) statements;
Loops in c language
Example 1:
int main() { int a; for(a=1; a<=5; a++) printf("%d,", a); return 9; } //output: 1,2,3,4,5
The variable ‘a’ is initialized with 1 in the for loop. a++ increments the value by 1.
Order of execution of FOR loop
- Initialization part is executed only for the fist time.
- For loop condition is checked. If the condition is satisfied, loop statements are executed.
- After execution of loop statements, program control goes to INCREMENT or DECREMENT part. This completes First Iteration.
- Second Iteration starts with CONDITION check. If the condition is satisfied, loop statements are executed for the second time followed by INCREMENT / DECREMENT.
Note: You can skip all three sections of FOR loop like for( ; ; ){ }. You should add code to break the loop with break; statement. Other wise, it becomes an infinite loop.
Nesting of FOR loop:
You can nest one for loop inside another for loop. You can also nest a while inside for loop, for loop inside do-while loop and so on. Here is an example.
int main() { int a, b; //outer for loop for(a=1; a<=3; a++) { //inner for loop for(b=a; b<=3; b++) { printf("%d,", b); } printf("\n"); } return 9; } //output: // 1,2,3 // 2,3 // 3
You can use break and continue statements inside for loop for better coding and performance.
3. DO WHILE loop
A do-while loop is different from FOR loop and WHILE loop because a do-while loop executes loop statements at least once even if the loop condition fails for the very first time.
Syntax:
do { STATEMENTS; }while( CONDITION ); //SEMICOLON IS REQUIRED.
C Programming Loops While For Do While
Example 1:
int main() { int a=1; do { printf("%d,", a); a++; }while( a <=10 ); } //output: //1,2,3,4,5,6,7,8,9,10
Order of execution of DO WHILE loop
- Statements of a DO-WHILE loop is executed at least once. First iteration is over.
- CONDITION is checked. If the condition is satisfied, second iteration starts. If the condition fails, control goes to the next statement after DO-WHILE.
Nesting of DO WHILE loop
You can nest a do-while loop inside another do-while loop or any other loop like FOR or WHILE. To better control a do-while loop, you can use break and continue statements.
Example:
int main() { int a=1, b=1; do { b=a; do { printf("%d,",b); b++; }while(b <=3); print("\n"); a++; }while(a<=3); return 9; } //output //1,2,3, //2,3, //3,
[WpProQuiz 28]
Loops in c language MCQ
1) Choose a right C Statement.
A) Loops or Repetition block executes a group of statements repeatedly.
B) Loop is usually executed as long as a condition is met.
C) Loops usually take advantage of Loop Counter
D) All the above.
Answer [=] D
2) Loops in C Language are implemented using.?
A) While Block
B) For Block
C) Do While Block
D) All the above
Answer [=] D
3) Which loop is faster in C Language, for, while or Do While.?
A) for
B) while
C) do while
D) All work at same speed
Answer [=] D
4) Choose correct C while loop syntax.
A)
while(condition) { //statements }
B)
{ //statements }while(condition)
C)
while(condition); { //statements }
D)
while() { if(condition) { //statements } }
Answer [=] A
5) Choose a correct C for loop syntax.
A)
for(initalization; condition; incrementoperation) { //statements }
B)
for(declaration; condition; incrementoperation) { //statements }
C)
for(declaration; incrementoperation; condition) { //statements }
D)
for(initalization; condition; incrementoperation;) { //statements }
Answer [=] A
Explanation:
increment or decrement operation at third place.
6) Choose a correct C do while syntax.
A)
dowhile(condition) { //statements }
B)
do while(condition) { //statements }
C)
do { //statements }while(condition)
D)
do { //statements }while(condition);
Answer [=] D
Explanation:
Semicolon after while(condition) is a must.
7) What is the output of C Program.?
int main() { while(true) { printf("RABBIT"); break; } return 0; }
A) RABBIT
B) RABBIT is printed unlimited number of times.
C) No output
D) Compiler error.
Answer [=] D
Explanation:
while(TRUE) or while(true) does not work. true is not a keyword.
8) What is the output of C Program.?
int main() { int a=5; while(a==5) { printf("RABBIT"); break; } return 0; }
A) RABBIT is printed unlimited number of times
B) RABBIT
C) Compiler error
D) None of the above.
Answer [=] B
Explanation:
If there is no BREAK statement, while loop runs continuously util the computer hangs. BREAK causes the loop to break once and the statement below the while if any will be executed.
9) What is the output of C Program.?
int main() { int a=5; while(a=123) { printf("RABBIT\n"); break; } printf("GREEN"); return 0; }
A) GREEN
B) RABBIT GREEN
C) RABBIT is print unlimited number of times.
D) Compiler error.
Answer [=] B
Explanation:
while(a=123) = while(123) = while(Non Zero Number). So while is executed. BREAK breaks the loop immediately. Without break statement, while loop runs infinite number of times.
10) What is the output of C Program.?
int main() { int a=5; while(a >= 3); { printf("RABBIT\n"); break; } printf("GREEN"); return 0; }
A) GREEN
B) RABBIT GREEN
C) RABBIT is print infinite times
D) None of the above
Answer [=] D
Explanation:
Notice a semicon(;) after while condition. It makes the printf and break statement blocks isolate.
while(a >= 3) { ;//infinite loop } { printf("RABBIT\n"); break; }
11) What is the output of C Program.?
int main() { int a=25; while(a <= 27) { printf("%d ", a); a++; } return 0; }
A) 25 25 25
B) 25 26 27
C) 27 27 27
D) Compiler error
Answer [=] B
Explanation:
a++ is equivalent to a=a+1;
a is incremented each time.
12) What is the output of C Program.?
int main() { int a=32; do { printf("%d ", a); a++; }while(a <= 30); return 0; }
A) 32
B) 33
C) 30
D) No Output
Answer [=] A
Explanation:
Even before at least once checking while(condition), the do block is executed. This generates 32. While (32 = 30) attempts to loop twice, it fails. So the loop is ended.
13) What is the output of C Program.?
int main() { int a=32; do { printf("%d ", a); a++; if(a > 35) break; }while(1); return 0; }
A) No Output
B) 32 33 34
C) 32 33 34 35
D) Compiler error
Answer [=] C
Explanation:
while(1) is infinite loop. So we kept if(condition) to break the loop. a++ is equivalent to a=a+1;
14) Choose a correct C Statement.
A) a++ is (a=a+1) POST INCREMENT Operator
B) a– is (a=a-1) POST DECREMENT Opeartor –a is (a=a-1) PRE DECREMENT Opeator
C) ++a is (a=a+1) PRE INCRMENT Operator
D) All the above.
Answer [=] D
15) Choose correct Syntax for C Arithmetic Compound Assignment Operators.
A) a+=b is (a= a+ b) a-=b is (a= a-b)
B) a*=b is (a=a*b) a/=b is (a = a/b)
C) a%=b is (a=a%b)
D) All the above.
Answer [=] D
16) What is the output of C Program.?
int main() { int k, j; for(k=1, j=10; k <= 5; k++) { printf("%d ", (k+j)); } return 0; }
A) compiler error
B) 10 10 10 10 10
C) 11 12 13 14 15
D) None of the above
Answer [=] C
Explanation:
You can initialize any number of variables inside for loop.
17) What is the output of C Program.?
int main() { int k; for(k=1; k <= 5; k++); { printf("%d ", k); } return 0; }
A) 1 2 3 4 5
B) 1 2 3 4
C) 6
D) 5
Answer [=] C
Explanation:
Semicolon at the end of for(); isolates the below print() block. After for loop is over, k value is 6.
for(k=1; k <= 5; k++) { ; } { printf("%d ", k); }
18) What is the output of C Program.?
int main() { int k; for(;;) { printf("TESTING\n"); break; } return 0; }
A) No Output
B) TESTING
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
for(;;) loop need not contain any initialization, condition and incre/decrement sections. All are optional. BREAK breaks the FOR Loop.
19) What is the output of C Program.?
int main() { int k; for(printf("FLOWER "); printf("YELLOW "); printf("FRUITS ")) { break; } return 0; }
A) Compiler error
B) FLOWER FRUITS
C) FLOWER YELLOW
D) FLOWER YELLOW FRUITS
Answer [=] C
Explanation:
Is acceptable for(anything; anything; anything). printf(“YELLOW”) displays YELLOW and returns 1. the for loop continues forever. Actually, breaking out of the for loop is saving us. The third section is only carried out following the conditional check and the loop statements. Without the increment/decrement sections, break causes the loop to end.
20) What is the way to suddenly come out of or Quit any Loop in C Language.?
A) continue; statement
B) break; statement
C) leave; statement
D) quit; statement
Answer [=] B
Explanation:
eg. while(condition) { break; }