# Introduction to Loops: SEBA Class 10 Computer Chapter 4 notes

Share with others

Get answers, questions, notes, textbook solutions, extras, pdf, mcqs for Computer chapter 4 Introduction to Loops of class 10 (HSLC/Madhyamik) for students studying under the Board of Secondary Education, Assam (SEBA). These notes/answers, however, should only be used for references and modifications/changes can be made wherever possible.

#### Introduction

In programming languages, loops provide an efficient way to perform repetitive tasks. Loops are essential constructs for repeated execution of a set of statements in a program. They simplify the code and make it more readable and manageable, especially when dealing with tasks that need to be performed multiple times.

There are three main types of loops in C programming: while, do-while, and for. Each loop has its own unique syntax and use case, but all of them function by executing statements repeatedly based on a specific condition. A loop will continue to run as long as the condition is true, and it will terminate when the condition becomes false.

Loops can be used in various scenarios, such as running a fixed number of times or running based on user input. They enable developers to create dynamic and efficient programs that can handle complex tasks with minimal code.

##### Exercise

1. Why do we use a loop in a C program?

Answer: We use a loop in a C program to perform a task many times efficiently. There are different types of loops that we can use in C programming. By using loops, we can write interesting programs that can perform repetitive tasks quickly and easily.

2. Do we need to use only one type of loop in a C program? Justify your answer by writing a C program.

Answer: No, we can use multiple types of loops in a C program depending on the requirements of the program. Here is an example program that uses both for and while loops:

#include <stdio.h>
int main() {
int i;
// for loop to print numbers from 1 to 10
printf(“Numbers from 1 to 10:\n”);
for(i = 1; i <= 10; i++) {
printf(“%d “, i);
}
printf(“\n”);
// while loop to print even numbers from 2 to 10
printf(“Even numbers from 2 to 10:\n”);
i = 2;
while(i <= 10) {
printf(“%d “, i);
i += 2;
}
return ;
}

3. What will happen if we write a while loop with 1 in place of the condition ? Try it in a simple C program. Hint:

while (1)
{
printf(“We must raise our voice against corruption \n”);
}

Answer: If you write a while loop with 1 in place of the condition, it will create an infinite loop because the condition will always evaluate to true. In the given C program, it will keep printing “We must raise our voice against corruption” indefinitely. Here’s the full code:

#include <stdio.h>
int main() {
while (1) {
printf(“We must raise our voice against corruption \n”);
}
return 0;
}

4. Name different portions of a for loop. Can we put more than one statement within a portion?

Answer: The different portions of a for loop are:

• Initialization expression
• Condition checking or testing expression
• Update expression

We can put more than one statement within each portion of the for loop. For example, in the initialization expression, we can have multiple statements separated by commas. Similarly, in the body of the loop, we can have multiple statements enclosed within curly braces.

##### Answer with TRUE or FALSE

(i) If the condition of the while loop is false, the control comes to the second statement inside the loop.

(ii) We can use at most three loops in a single C program.

(iii) The statements inside the do-while loop executes at least once even if the condition is false.

(iv) Only the first statement inside the do-while loop executes when the condition is false.

(v) In a do-while loop, the condition is written at the end of the loop.

##### Programming exercises

A. Write a C program to find the summation of the following series

(a). 12 +2²+ 3² +4² +.. + N²

int main() {
int n, i, sum = 0;
printf(“Enter the value of N: “);
scanf(“%d”, &n);
for(i = 1; i <= n; i++) {
sum += i*i;
}
printf(“Summation of the series is: %d”, sum);
return 0;
}

(b). 1³ + 23 +3³ +..+N³

int main() {
int n, i, sum = 0;
printf(“Enter the value of N: “);
scanf(“%d”, &n);
for(i = 1; i <= n; i++) {
sum += i*i*i;
}
printf(“Summation of the series is: %d”, sum);
return 0;
}

(c). 1*2 + 2*3 + 3*4 +.. + N*(N+1)

int main() {
int n, i, sum = 0;
printf(“Enter the value of N: “);
scanf(“%d”, &n);
for(i = 1; i <= n; i++) {
sum += i*(i+1);
}
printf(“Summation of the series is: %d”, sum);
return 0;
}

B. Write a C program to continuously take a number as input and announce whether the number is odd or even. Hint: use do-while loop.

int main() {
int number;
char choice;
do {
// Take input
printf(“Enter a number: “);
scanf(“%d”, &number);
// Check if the number is odd or even
if (number % 2 == 0) {
printf(“%d is an even number.\n”, number);
} else {
printf(“%d is an odd number.\n”, number);
}
// Check if the user wants to continue
printf(“Do you want to continue (y/n)? “);
scanf(” %c”, &choice);
} while (choice == ‘y’ || choice == ‘Y’);
return 0;
}

C. Write a C program to display the following pattern.

1
11
111
1111
11111

int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf(“1”);
}
printf(“\n”);
}
return 0;
}

D. Write a C program to display the following pattern.

5
54
543
5432
54321

int main() {
int i, j;
for (i = 5; i >= 1; i–) {
for (j = 5; j >= i; j–) {
printf(“%d”, j);
}
printf(“\n”);
}
return 0;
}

E. Write a C program to display the following pattern.

54321
5432
543
54
5

int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 5; j >= i; j–) {
printf(“%d”, j);
}
printf(“\n”);
}
return 0;
}

1. What is the purpose of a loop in programming languages?

Answer: The purpose of a loop is to enable repeated execution of a set of statements in a program.

2. List the three types of loop constructs available in the C programming language.

• while loop
• do-while loop
• for loop

8. Can more than one statement be used in an expression of a for loop in C?

Answer: Yes, more than one statement can be used in an expression of a for loop in C.

1. Which programming construct allows repeated execution of a set of statements?

A. Function B. Conditional C. Loop D. Recursion

2. In the C programming language, which loop construct checks the condition before executing the statements inside?

A. while B. do-while C. for D. if

3. In a while loop, when is the control transferred out of the loop?

A. When the condition is true B. When the condition is false C. After a fixed number of iterations D. When a user interrupts the program

Answer: B. When the condition is false

18. Can you use multiple statements in a for loop expression?

A. Yes B. No C. Only in the initialization expression D. Only in the update expression