Loops in C While For Do While Tutorial and MCQ
Table of Contents
Learn Loops in C While For Do While MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
For as long as the loop condition is met, loops assist in repeatedly executing a set of instructions. Condition can be a combination of relational operators and logical operators.
Type of Loops in C
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 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.
C Programming Loops
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 in C
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
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;
C Programming Loops While For Do While
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
- Only the initialization phase is run.
- Condition for loop is verified. If the criterion is met, the looping statements are carried out.
- 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.
The CONDITION is examined. The second iteration begins if the condition is met. The following statement follows DO-WHILE if the condition is false.
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 29]
Loops in C MCQ
1) Choose facts about continue; statement is C Language.
A) continue; is used to take the execution control to next iteration or sequence
B) continue; statement causes the statements below it to skip for execution
C) continue; is usually accompanied by IF statement.
D) All the above.
Answer [=] D
2) What is the output of C Program.?
int main() { int a=14; while(a<20) { ++a; if(a>=16 && a<=18) { continue; } printf("%d ", a); } return 0; }
A) 15 16 17 18 19
B) 15 18 19
C) 15 16 20
D) 15 19 20
Answer [=] D
Explanation:
Between 16 – 18, continue statement skips all other statements below it. So a will not be printed during that time.
15 printed
16 not printed
17 not printed
18 not printed
19 printed
20 printed
3) Choose a correct statement about C break; statement.?
A) break; statement can be used inside switch block
B) break; statement can be used with loops like for, while and do while.
C) break; statement causes only the same or inner loop where break; is present to quit suddenly.
D) All the above.
Answer [=] D
4) Choose a correct statement about C language break; statement.
A) A single break; statement can force execution control to come out of only one loop.
B) A single break; statement can force execution control to come out of a maximum of two nested loops.
C) A single break; statement can force execution control to come out of a maximum of three nested loops.
D) None of the above.
Answer [=] A
5) Choose a correct C Statement regarding for loop.
for(; ;);
A) for loop works exactly first time
B) for loop works infinite number of times
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
We are not specifying condition to exit the loop. Eg. for(a=0;a<10;a++)
6) What is the output of C Program.?
int main() { int a=10, b, c; b=a++; c=++a; printf("%d %d %d", a, b, c); return 0; }
A) 10 11 12
B) 12 10 12
C) 12 11 12
D) 12 12 12
Answer [=] B
Explanation:
a++ first assigns 10 to b. Next a is increment separately. ++a increments from 11 to 12. Final ++a value is assign to the left side variable C.
7) What is the output of C Program.?
int main() { int a=0, b=0; while(++a < 4) printf("%d ", a); while(b++ < 4) printf("%d ", b); return 0; }
A) 0 1 2 3 1 2 3 4
B) 1 2 3 1 2 3 4
C) 1 2 3 4 1 2 3 4
D) 1 2 3 4 0 1 2 3
Answer [=] B
Explanation:
(++a < 4) first increments and compares afterwards. (b++ < 4) first compares and increments afterwards.
8) What is the output of C Program.?
int main() { int a=10,b=20; if(a==9 AND b==20) { printf("Hurray.."); } if(a==10 OR b==21) { printf("Theatre"); } return 0; }
A) Theatre
B) Hurray Theatre
C) No output
D) Compiler error
Answer [=] D
Explanation:
There are no keywords like AND / OR in C language. Logical OR is represent with two Pipes ||. Logical AND is represent with two Ampersands &&.
9) What are C ASCII character ranges.?
A) A to Z = 65 to 91
B) a to z = 97 to 122
C) 0 to 9 = 48 to 57
D) All the above
Answer [=] D
Explanation:
All remaining characters are special characters or symbols in C Language. 0 to 47, 58 to 64, 91 to 96, 123 to 127.
10) Expand or Abbreviate ASCII with regard to C Language.
A) Australian Standard Code for Information Interchange
B) American Standard Code for Information Interchange
C) American Symbolic Code for Information Interchange
D) Australian Symbolic Code for Information Interchange
Answer [=] B
Explanation:
There were only 128 Characters with 7 Bits in Original ASCII specification. Present character standard in all modern programming languages is UNICODE which covers all languages, Emojis and other special symbols all over the world.