(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

Explain the looping statement in C with suitable examples.


  • 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.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
return 0;
}
output:
1
2
3
4
5

while loop
  • Condition is checked before executing the statements within the loop body.
// Prints numbers from 1 to 5.
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
output:
1
2
3
4
5

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.
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
output:
1
2
3
4
5

(Next part of this series is here: Part 6, Functions (Unit 3), 16 marks Q&A)

Posted in

2 responses to “Loops in C, CS3251, Anna Univ Past Years Q-A, Programming in C – Part 5.”

  1. […] Loops in C, CS3251, Anna Univ Past Years Q-A, Programming in C – Part 5. […]

    Like

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In