(Part 4 of this series of Q-A is here: 16 marks, Q-A, structures in C, Unit 4. )
16 marks Q-A on loops (Unit 1), Nov/Dec 2022, Anna Univ, CS 3251 (Programming in C)
Pls see the images below.





Anna University Past Years Exam Q & A
CS3251 : Programming in C
Nov / Dec 2022, 16 Marks, Unit 1
Question
Explain the looping statement in C with suitable examples.
Answer
- Loops help us to easily execute a block of code repeatedly.
- The code is repeatedly executed as long as the loop condition is true.
- Loops help to make the program shorter, easier to read, and easier to maintain.
There are 3 loops in C:
- for
- while
- do-while
for loop:
- Initialization, checking of condition, and updation is written compactly in a single line.
// Prints numbers from 1 to 5. <stdio.h>int main(){ int i; for (i = 1; i <= 5; i++) { printf("%d\n", i); } return 0;}
output: 12345
while loop
- Condition is checked before executing the statements within the loop body.
// Prints numbers from 1 to 5. <stdio.h>int main(){ int i = 1; while (i <= 5) { printf("%d\n", i); i++; } return 0;}
output:12345
do-while loop
- condition is checked after executing the statements within the loop body.
- The loop body necessarily gets executed at least once.
// Prints numbers from 1 to 5. <stdio.h>int main(){ int i = 1; do { printf("%d\n", i); i++; } while (i <= 5); return 0;}
output: 12345
(Next part of this series is here: Part 6, Functions (Unit 3), 16 marks Q&A)
Leave a comment