logo

Introduction to Loops: SEBA Class 10 Computer Chapter 4 notes

Leave a Comment

post

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.

If you notice any errors in the notes, please mention them in the comments

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.

Textual questions and answers

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.

Answer: False

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

Answer: False

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

Answer: True

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

Answer: False

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

Answer: True

Programming exercises

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

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

Answer: #include <stdio.h>

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³

Answer: #include <stdio.h>
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)

Answer: #include <stdio.h>
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.

Answer: #include <stdio.h>
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

Answer: #include <stdio.h>
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

Answer: #include <stdio.h>
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

Answer: #include <stdio.h>
int main() {
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = 5; j >= i; j–) {
            printf(“%d”, j);
        }
        printf(“\n”);
    }
    return 0;
}

Extra/additional questions and answers

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. 

Answer:

  • while loop
  • do-while loop
  • for loop

Q. Explain the working of a while loop with an example. 

Answer: A while loop is a control flow statement that allows repeated execution of a set of statements as long as a given condition evaluates as true. The working of a while loop involves the following steps:

  • The condition of the loop is checked first.
  • If the condition evaluates as true, the statements inside the loop are executed.
  • After executing the statements, the condition is checked again.
  • If the condition is still true, the statements are executed again, and this process continues until the condition becomes false.
  • When the condition evaluates as false, the control comes out of the loop and executes the statements outside the loop.

For example, consider a program with a while loop that prints a message five times:

int i = 0;
while (i < 5) {
printf(“Hello, World!\n”);
i++;
}

Q. Describe how to make a while loop run for a dynamic number of times based on user input. 

Answer: To make a while loop run for a dynamic number of times based on user input, you can follow these steps:

  • Declare an integer variable, for example, “n”, and another variable, such as “index”, to act as a counter.
  • Take input from the user for the variable “n”. This value will determine the number of times the loop should run.
  • Initialize the counter variable “index” to 0.
  • Use a while loop with the condition “index < n”. This means the loop will run as long as the value of “index” is less than the value of “n”.
  • Inside the loop, perform the desired operations or statements.
  • Increment the value of “index” by 1 in each iteration of the loop.
  • When the value of “index” becomes equal to the value of “n”, the loop condition will become false, and the loop will terminate.

Here’s an example of a program that demonstrates these steps:

#include <stdio.h>
int main() {
int n, index = 0;
printf(“Enter the number of times to run the loop: “);
scanf(“%d”, &n);
while (index < n) {
    printf(“This is loop iteration %d\n”, index);
    index++;
}
return 0;
}

Q. What is the purpose of a do-while loop in C? 

Answer: The purpose of a do-while loop in C is to execute the statements within the loop at least once, irrespective of the condition, and then continue executing the statements as long as the given condition remains true. It is particularly useful when you want the program to execute a set of statements and then give the user an option to stop or continue the loop based on their input.

Q. What are the three portions of a for loop in C programming language? (3 marks)

Answer: A for loop in C programming language has three portions:

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

These expressions are separated by semicolons and are used to control the execution of the loop.

Q. What is the purpose of a for loop in C? 

Answer: The purpose of a for loop in C is to execute a set of statements for a specific number of iterations, based on the initialization, condition checking, and update expressions. It is commonly used when the number of iterations is known beforehand, and you want to perform an operation or process for each iteration.

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.

Extra/additional MCQs

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

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

Answer: C. Loop

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

Answer: A. while

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

Q. Which of the following is NOT a loop construct in C programming language?

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

Answer: C. if

Q. What operation is performed on the ‘index’ variable at the end of each iteration in a while loop?

A. Decrement B. Increment C. Multiplication D. Division

Answer: B. Increment

Q. Which loop construct in C executes the statements inside the loop at least once, regardless of the initial condition?

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

Answer: B. do-while

Q. In a while loop, when are the statements inside the loop executed?

A. When the condition is true B. When the condition is false C. Before the condition is checked D. After the loop ends

Answer: A. When the condition is true

Q. In a do-while loop, when are the statements executed?

A. Before the condition is checked B. After the condition is checked C. Never D. Randomly

Answer: B. After the condition is checked

Q. What loop construct is used when you want to execute a set of statements at least once, regardless of the condition?

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

Answer: C. do-while loop

Q. In the do-while loop, what should the user enter to stop further execution of the loop?

A. 1 B. -1 C. Any number D. 0

Answer: D. 0

Q. What type of loop is typically used when the number of iterations is known beforehand?

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

Answer: B. for loop

Q. Which expression in a for loop contains the initialization statements?

A. Initialization expression B. Condition checking expression C. Update expression D. None of the above

Answer: A. Initialization expression

Q. Which expression in a for loop contains the condition checking statements?

A. Initialization expression B. Condition checking expression C. Update expression D. None of the above

Answer: B. Condition checking expression

Q. Which expression in a for loop contains the statements that need to be executed in each iteration?

A. Initialization expression B. Condition checking expression C. Update expression D. None of the above

Answer: C. Update expression

Q. How many semicolons are used to separate the expressions in a for loop signature?

A. One B. Two C. Three D. None

Answer: B. Two

Q. What is the purpose of the update expression in a for loop?

A. Initialize variables B. Check conditions C. Execute statements D. Modify variables

Answer: D. Modify variables

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

Answer: A. Yes

Ron'e Dutta

Ron'e Dutta

Ron'e Dutta is a journalist, teacher, aspiring novelist, and blogger who manages Online Free Notes. An avid reader of Victorian literature, his favourite book is Wuthering Heights by Emily Brontë. He dreams of travelling the world. You can connect with him on social media. He does personal writing on ronism.

1 comment

  1. Noobie Bhai October 4, 2023 at 11:27 pm

    Please provide me everything

Leave a comment

Your email address will not be published. Required fields are marked *

Only for registered users

Meaning
Tip: select a single word for meaning & synonyms. Select multiple words normally to copy text.