Class 10 Unit 5
Class 10 Unit 5
COMPUTER
NOTES
BY Mustafa shahid
St. Francis Grammar School
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #5) By: Mustafa Shahid
============================SHORT QUESTIONS============================
Q.No.1 Differentiate between for loop and while loop.
For loop:
The for statement is used to execute a set of statements repeatedly for a fixed number of times in a
program.
While loop:
The while statement is used to implement repetition when the number of repetitions is not known in
advance. In this case, repetition continues until some condition remains true.
Q.NO.2 Differentiate between while loop and do while loop.
WHILE () loop DO-WHILE () loop
While () loop is pre-tested loop. Do-While loop is post-tested loop.
The Syntax or general form of while () The Syntax or general form of do-while ()
loop is: loop is:
While(condition) Do{
{ Statements //body of loop
Statements; //body of loop }
} while(condition)
In 'while' loop the controlling condition In 'do-while' loop the controlling condition
appears at the start of the loop. appears at the end of the loop.
The iterations do not occur if, condition at the The iteration occurs at least once even if the
first iteration appears false. condition is false at the first iteration.
1|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #5) By: Mustafa Shahid
2|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #5) By: Mustafa Shahid
============================LONG QUESTIONS============================
Q.No.1 What is looping structure? Explain for loop with examples.
Loop Structure:
A loop is a structure that enables the programmer to execute the same sequence of statements repeatedly
until a particular condition is met.
For loop / 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 counter loop. It has the general form:
for (initialization; test condition; increment/decrement)
{
body of the loop}
• When for statement is executed, a 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 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 execution of the body of the loop, the control is transferred back to the
increment/decrement part of the loop. The loop variable is incremented or decremented using an
assignment statement such as k = k+1. The new value of loop variable is again checked with the
test condition. If the condition is satisfied then the body of the loop is again executed. This
process goes on till the test condition becomes false.
• Body of the loop may have one or more statements. If it contains only a single statement then
braces are not needed.
Example:
For example, the following all for statement are valid.
For(;;)
For(int i=1;;)
For(I k<10; k++)
For(;; k++)
For(: x<12;)
Q.No.2 Explain while and do-while loops with examples.
While loops/The While Statement:
A repetition structure when the number of iterations is not known in advance and the repetition
continues until test condition remains true.
The while statement has the general form:
while (test condition)
{
Body of the loop
}
• When a while statement is executed, the computer first evaluates the test condition. If it is true,
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 statement following the end of the body of loop.
3|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #5) By: Mustafa Shahid
• 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 loops / The Do While Statement:
• The do while statement is used to implement loop structure when it is required to execute the loop at
least once. The general form of the do while loop is given below.
do
{
Body of the loop
{
While (test condition);
• 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 loop if the body of loop contains a single statement, then braces
are not required.
Q.No.3 Explain the purpose of Break and continue statements with one example each.
The Break Statement:
C language provides the break statement to exit from a loop as soon as certain condition occurs. It is
used in for, while and do while statements. Break statement is also used to exit the body of switch
statement after executing the statements under a case and transfers control to the first statement
following the end of the switch statement.
The Continue Statement:
Sometimes during the execution of a loop, when a certain condition occurs after executing a statement, it
may be required to skip the remaining statements within the body of the loop and continue loop for next
iteration until the test condition in met. C language provides the continue statement to achieve this task.
The continue statement causes the loop to be continued with the next iteration after skipping the
remaining statements within the body of the loop.
Q.no.4 What is nested loop? Give two examples.
Nested Loops:
In C language, it is allowed to nest loops within another loop. Nested loops can be of any kind. For
example, the programmer can nest a for loop inside a while loop or inside a do while loop. Loops can be
mixed in anyway as required.
Example # 1:
Program uses nested loop to print the products
1x1=1
1x2=2
1x3=3
1x4=4
2x1=2
2x2=4
2x3=6
2x4=8
3x1=3
3x2=6
3x3=9
3 x 4 = 12
4|Page