10th Computer Chapter 5

Chapter 5: Loop Control Structure

10th Computer Chapter 5

Write short answers to the following questions

I. Differentiate between the for loop and the while loop.

for loopwhile loop
1. Initialization, condition checking, iteration statements are written at the top of the loop.1. Only Initialization and condition checking, is done at the top of the loop.
2. The ‘for’ loop used only when we already knew the number of iterations.The ‘while’ loop used only when the number of iteration are not exactly Known.
3. In for loop the initialization once done is never repeated.In while loop if initialization is done during condition checking, then initialization is done each time the loop iterate.
4. In ‘for’ loop iteration statement is written at top, hence, executes only after all statements in loop are executed.In ‘while’ loop, the iteration statement can be written anywhere in the loop.

ii. Differentiate between a while loop and a do-while loop.

while loopdo while loop
1. While loop is pre-tested loop.Do while loop is post-tested loop.
2. In while loop the controlling condition appears at the start of the loop.In do while loop the controlling appears at the end of the loop.
3. The iteration do not occur if the condition at the first iteration appears false.The iteration occurs at least once even if the condition is false at the first iteration.

iii. What will be the output of the following code?

            int k;

            for (k=1;k<=5;k++)

                        printf(“\n I am a student”);

                        printf(“\n GOODBYE”);

Answer:

            I am a student

            I am a student

            I am a student

            I am a student

            I am a student

            GOODBYE

iv. What will be the output of the following code?

            int n;

            for (n=30;n>=10;n=n-5);

Answer

            30

            25

            20

            15

            10

v. Find errors in the following code.

            int k,a

            a=3;

            k=1;

            while (k<10);

            [

                        Print (“\n%f\t%f,k,k*a-1);

                        k=k+2;

            ]

Answer

Corrected Code:

            int k,a;

            a=3;

            k=1;

            while (k<10)

            {

                        Print (“\n%f\t%f”,k, k*a-1);

                        k=k+2;

            }

vi. Convert the following for loop into a while loop.

            int k=25;

            for (k=25;k>0;k=k-3)

                        printf (“\n%d”,k);

Answer

int k;

k=25;

            while (k > 0)

                        {

                                    printf (“\n%d”,k);

                                    k=k-3;

                        }

EXTENSIVE QUESTIONS

Q1. What is a looping structure? Explain for loop with examples.

Answer

Looping Structure

A loop is a structure that enables the programmer to execute the same sequence of statements repeatedly until a particular condition is met. A loop is a statement in a programming language that allows one or more statements to be repeatedly executed as many times as required.

FOR statement

The for is a looping statement which is used to execute a set of statements repeatedly for a fixed number of times. It is also known as a counter-loop.

The general form of the FOR loop is given below:

for (initialization; test condition; increment/decrement )

{

Body of the loop

}

Explanation

When for statement is executed, a variable (also known as loop variable) is assigned an initial value in the initialization part of the loop, such as k=1 or count=0.

The value of the loop variable is checked with the given test condition. The test condition is a relational expression, such as k<10. If the condition is true, the control enters the body of the loop otherwise it will exit the loop.

After the loop’s body is run, the control moves back to the part where the loop variable is increased or decreased. The variable is updated, like using k = k + 1. The new value is then rechecked against the condition. If the condition is true, the loop’s body runs again. This continues until the condition becomes false.

Examples

The following statements are valid.

for(;;)

for(int i=1;;)

for(;k<10;k++)

Q2. Explain while and do-while loops with examples.

WHILE Statement

The WHILE statement is used to implement a repetition structure when the number of iterations is not known in advance and the repetition continues until the test condition remains true.

General Form

The while statement has the general form:

while (test condition)

{

Body of the loop

}

Explanation

When a while statement is executed, the computer first evaluates the test condition. If it is true, the body of the while loop is executed.

After the execution of the body of the loop, the test condition is again evaluated and if it is true, the body of the loop is executed once again. This process continues until the test condition becomes false. When it becomes false, the control is transferred to the first statement following the end of the body of the loop. The body of the loop can be a single statement or it can be multiple statements.

If the body of the loop consists of a single statement then the braces are not required but if it consists of more than one statement then braces must be used.

DO WHILE Statement

The do-while statement is used to implement a loop structure when it is required to execute the loop at least once.

General Form

The general form of the do-while loop is given below.  

do

{

Body of the loop

}

while (test condition);

Explanation

The statement while (test condition) is placed at the end of the loop so that the body of the loop is executed at least once whether the condition is true or false. There is a semicolon after the test condition because it is at the end of the loop. If the body of the loop contains a single statement then braces are not required.

Q3. What is a nested loop? Give two examples.

Answer

Nested Loop

In C language, it is allowed to nest loops within another loop. The nested loop can be of any kind.

Example 1

The following program uses a nested loop to print the products of numbers.

#include<stdio.h>

#include<conio.h>

int main()

{

int j,k,prod;

                        for(j=1;j<=3;j++)

                                    for(k=1;k<=4;k++)

                                    {

Prod=j*k;

printf(“\n%2d x %2d = %2d ”,j,k,prod);

            }

getch();

}

Explanation

  1. The loop variable is assigned its initial value 1 and the nested loop is executed. This calculates and displays the first four products, 1×1, 1×2, 1×3 and 1×4.
  2. The value of j is then incremented by 1 and the inner loop is executed again. This calculates and displays the next four products, 2×1, 2×2, 2×3 and 2×4.
  3. Finally, j is incremented to 3, giving the last four products, 3×1, 3×2, 3×3 and 3×4.
  4. In this program, braces must be used in the inner for loop because there is more than one statement to be executed.

Example2

The following program uses a nested loop to calculate the sum of the integers for each integer from 1 to n; where n is the value that the user of the program enters.

#include<stdio.h>

#include<conio.h>

int main()

{

int j, k, sum, n;

printf(“\nEnter the upper limit(n):”);

scanf(“%d”,&n);

printf(“\n\tInteger\t\tSum”) ;

for (j=1; j<=n; j++)

            {

                        Sum=0

for (k=1;k<=j; k++)

sum=sum+k

                                    printf(“\n\t%3d\t\t%3d”, j, sum);

}

getch();

}

Select the best answer for the following MCQs.

i. Which Structure enables the programmer to execute a set repeatedly until a particular condition is met?

A. selection                                       

B. Sequence

C. choice 

D. Loop

ii. Which of the following is also called a counter loop?

A. do-while 

B. While

C for                                                    

D. if-else

iii. Which of the following ends a multiple-statement while loop?

A. right bracket 

B. right brace

C. Semicolon 

D. colon

iv. Which loop is used to execute a set of statements repeatedly for a fixed number of times?

A. for loop 

B. do-while loop

C. while loop 

D. nested loop

v. Which loop is used when it is required to execute the loop at least once?

A. for loop                                         

B. do-while loop

C. while loop 

D. nested loop

vi. Which loop is preferred to use when the number of times the loop will execute is not known in advance?

A. for loop                                         

B. do-while loop

C. while loop 

D. nested loop

vii. In which loop condition is placed at the end?

A. for loop 

B. do-while loop

C. while loop 

D. nested loop

viii. Which statement is used to exit from a loop as soon as a certain condition is met?

A. break                                             

B. Continue

C. if statement 

D. default

ix. What is a pass through a loop called?

A. execution 

B. Iteration

C. Enumeration 

D. culmination

x. Which of the following should be placed at the beginning and the end of a for-loop if it consists of more than one statement?

A. ( )                                                 

B. <>

C. [ ] 

D. { }

 

Lab Activities

1. Write a program to print the sum of even numbers from 1 to 50.

Answer

Program

#include<stdio.h>

#include <conio.h>

int main()

{

int i,rem, sum;

for (i=1;1<=50; i++)

{

rem = 1%2;

if(rem==0)

{  

sum = sum + I;

}  

}

printf(“Sum of even numbers: %d”, sum);

getch();

}

2. Write a program to print the given sequence of numbers using a loop on a single line.

5          10        15        20        25        30        35        40        45        590

Answer

Program

#include<stdio.h>

#include <conio.h>

int main()

{

int n;

printf(“\n”);

for (n=5;n<=45 ;n+=5 )

printf(” %d”,n);

}

getch();

}

3. Write a program to print the sum of squares of all the numbers from using a loop.

sum = 12 +22 +32 + 42 +52 + 6+ 7+ 82 +92 + 102

Answer

Program

#include<stdio.h>

#include <conio.h>

int main()

{

int n, sum;

for (n=1;n<=10;n++)

{

printf(“\n %d”,n*n);

sum=sum+n*n;

                        }

printf(“\n %d”,sum);

getch();

            }

4. Write a program that prints all the upper-case letters in reverse order on a single line using a loop.

            ZYXWVUTSRQPPONMLKJIHGFEDCBA

Answer

Program

#include<stdio.h>

#include <conio.h>

int main()

{

char c;

for(c = ‘Z’;c>=’A’;c–)

 printf(“%c “,c);

return 0;

getch();

            }

5. Write a program that reads a number and prints its table using a while loop.

Answer

Program

#include<stdio.h>

#include <conio.h>

int main()

{

int table,i;

printf(“Please add table number:”) ;

scanf(“%i” ,&table);

for (i=1;i<=10; i++)

{

printf(“\n %d * %d = %d”, table,i, table*i);

                        }

getch();

            }

6. A class of 15 students takes an examination in which marks range from 1 to 100. Write a program to find and print the average marks.

Answer

Program

#include<stdio.h>

#include <conio.h>

int main()

{

int s[15],i, sum=0;

float avg=0.00;

for (i=1;i<=15 ; i++)

{

printf(“\n Enter Student %d Marks :”,i);

scanf(“%i”,&s[i]);

if(s[i]>100 || s[i]<1)

{

printf(“\n Marks are not valid”);

printf(“\nPlease reEnter Student %d Marks :”,i);

scanf(“%i”,&s[i]);

}

sum = sum + s[i];

}

avg = sum/15;

printf(“\n\n THE AVERAGE IS %0.2f”, avg);

getch();

            }

7. A class of 20 students takes an examination in which marks range from 1 to 100. Write a program to print the number of students passed and failed. Passing marks are 33.

Answer

#include<stdio.h>

#include <conio.h>

int main()

{

int s[20], i, passStudents=0, failStudents=0;

for(i=1;i<=20; i++)

{

printf(“\n Enter Student %d Marks:”,i) ;

scanf(“%i”,&s[i]);

if(s[i]>=33)

{

            passStudents++;

}

                        else

                                    {

                                                failStudents++;

                                    }

                        }

printf(“\n\n Number of students PASSED %d”, passStudents) ;

printf(“\n\n Number of students FAIL %d” , failStudents) ;

getch();

            }

8. Write a program that prints a line of 60 asterisks (*) using do while loop.

Answer 

#include<stdio.h>

#include <conio.h>

int main()

{

int n=0;

printf(“\n”);

while(n<60)

{

printf(“*”);

n++;

}

getch();

            }

9. Write a program that prints a table of squares of all the numbers from 1 to 10 as shown below.

NumberSquare
11
24
39
416
525
636
749
864
981
10100

Answer

#include<stdio.h>

#include <conio.h>

int main()

{

            int n=0;

            printf(“Number \t Square”);

            for(n=1;n<=10;n++)

            {

                        Printf(“\n%d \t %d”,n,n*n);

            }

            getch();

}

Download 10th Computer Chapter 5 Notes in pdf

Next Chapter 6: Computer Logic and Gates