TOPIC: REAL NUMBER - Bishop scott boys school

12
Page 1 of 12 BISHOP SCOTT BOYS’ SCHOOL (Affiliated to CBSE, New Delhi) Affiliation No.: 330726, School Campus: Chainpur, Jaganpura, By-Pass, Patna 804453. Phone Number: 7061717782, 9798903550. , Web: www.bishopscottboysschool.com Email: [email protected] ONLINE STUDY MATERIAL SUBJEC-COMPUTER SESSION-2020-21 CLASS-VII TOPIC:QB64-PROGRAMMING STATEMENTS-II (CHAPTER-6) DAY-1 NTRODUCTION TO LOOPS While programming, you may want to repeat the execution of a certain set of statements multiple times. This can be done using loops. A loop is used to repeat the execution of a group of statements many times. Statements that enable us to create loops are known as looping statements. We will learn the following looping statements in QBASIC. 1. FOR….. NEXT 2. DO…..LOOP Using FOR…NEXT Statement for Looping The FOR…NEXT statement is used when you want a group of statements to be executed a specific number of times. The general form of the FOR…NEXT statement is given below. FOR counter = initial value to final value [STEP value] The loop body comprising a set of statements to be executed NEXT counter The statements written between FOR and NEXT form the body of the loop. In the above-mentioned FOR…NEXT statement, 1. counter is a numeric variable that controls the loop and hence is also called the control or the index variable. Its value changes every time the loop body gets executed. 2. step value is the value by which the counter variable is incremented or decremented every time the loop body is executed. It can be a positive or a negative value, but it cannot be zero. The step value is optional. In case it is omitted, the counter variable is increased by one every time the loop is executed. Consider the following example based on FOR…NEXT loop. In this example, Control variable: A Initial value of control variable: 1

Transcript of TOPIC: REAL NUMBER - Bishop scott boys school

Page 1 of 12

BISHOP SCOTT BOYS’ SCHOOL (Affiliated to CBSE, New Delhi) Affiliation No.: 330726, School Campus: Chainpur, Jaganpura,

By-Pass, Patna 804453. Phone Number: 7061717782, 9798903550. , Web: www.bishopscottboysschool.com Email: [email protected]

ONLINE STUDY MATERIAL

SUBJEC-COMPUTER SESSION-2020-21

CLASS-VII

TOPIC:QB64-PROGRAMMING STATEMENTS-II

(CHAPTER-6)

DAY-1

NTRODUCTION TO LOOPS

While programming, you may want to repeat the execution of a certain set of statements multiple times. This can be done using loops. A loop is used to repeat the execution of a group of statements many times. Statements that enable us to create loops are known as looping statements. We will learn the following looping statements in QBASIC.

1. FOR….. NEXT 2. DO…..LOOP

Using FOR…NEXT Statement for Looping The FOR…NEXT statement is used when you want a group of statements to be executed a specific number of times.

The general form of the FOR…NEXT statement is given below.

FOR counter = initial value to final value [STEP value]

The loop body comprising a set of statements to be executed

NEXT counter

The statements written between FOR and NEXT form the body of the loop. In the above-mentioned FOR…NEXT statement,

1. counter is a numeric variable that controls the loop and hence is also called the control or the index variable. Its value changes every time the loop body gets executed.

2. step value is the value by which the counter variable is incremented or decremented every time the loop body is executed. It can be a positive or a negative value, but it cannot be zero. The step value is optional. In case it is omitted, the counter variable is increased by one every time the loop is executed.

Consider the following example based on FOR…NEXT loop. In this example, Control variable: A Initial value of control variable: 1

Page 2 of 12

Final value of control variable: 3 Step value: 1

Thus, this loop runs three times. The string “Hello” appears three times on the output screen (Fig. 5.1). When the loop ends, the statements written after the loop are executed. Consider the

following example.

The output of this program is:

Page 3 of 12

VIDEO-LINKS https://www.youtube.com/watch?v=7j7vorSobWo

DAY-2

Using the step value You can give a step value to specify the value by which the control variable should change. In the following program, the control variable X changes by 2 every time the body of the loop gets executed.

FOR X = 1 to 10 STEP 2

PRINT X

NEXT X

The output of this program is:

You can also assign a negative number as a step value. In this case, the value of the control variable is decreased by the step value every time the loop is executed. When the step value is negative, the initial value of the control variable is more than the final value.

FOR X = 10 to 1 STEP -2

PRINT X

NEXT X

The output of this program is:

Page 4 of 12

The step value can also be a real number.

FOR I = 1 TO 2 STEP 0.25

PRINT I

NEXT I

The output of this program is:

Let us consider some more examples of FOR…NEXT loop. Example 1: To add natural numbers from 1 to 20.

REM To add numbers from 1 to 20

CLS

Sum=0

For I = 1 to 20

Sum = Sum + I

NEXT I

PRINT "Sum of numbers from 1 to 20 =" ; sum

The output of this program is:

Example 2: To add all even numbers from 1 to 20.

REM To find the sum of all even numbers from 1 to 20

CLS

Sum=0

For I = 2 to 2 0 STEP 2

Sum = sum + I

NEXT I

Print "Sum of even numbers from 1 to 20 ="; sum

The output of this program is:

Example 3: To display the table of a number entered by the user.

Page 5 of 12

REM To display the table of a number entered by the user

PRINT "Enter a number"

INPUT A

For I = 1 to 10

PRINT A; "x"; I; "="; A*I

NEXT I

The output of this program is:

VIDEO-LINKS https://www.youtube.com/watch?v=7j7vorSobWo

DAY-3

Using DO…LOOP/or Looping

You may not always know the number of times a loop has to be executed. The execution of the statements may depend upon a condition. In such a situation, the use of a DO…LOOP is preferred over a FOR…NEXT loop.

There are two variations of the DO…LOOP.

1. DO WHILE…LOOP 2. DO UNTIL…LOOP

DO WHILE..LOOP The DO WHILE…LOOP is executed as long as the specified condition is TRUE. The general form of the DO WHILE…LOOP is:

DO WHILE condition

The loop body comprising a set of statements to be executed

LOOP

Page 6 of 12

The statements written between DO WHILE and LOOP form the body of the loop. Let us look at an example of this loop.

A=1

DO WHILE A<3

PRINT A

A=A+1

LOOP

The above loop is executed as long as the value of A is less than 3. If the condition in the DO WHILE…LOOP is not true, the loop is not executed at all. Consider the following example.

X=10

DO WHILE X<10

PRINT X

LOOP

In this case, condition X<10 is not true and hence the loop body is not executed. Let us look at a few more examples of the DO WHILE…LOOP.

Example 1: To display odd numbers less than 30.

REM To display odd numbers less than 30

CLS

X=1

DO WHILE X<=30

PRINT X

X=X+2

LOOP

The output of this program is:

Page 7 of 12

Example 2: To add numbers entered by the user as long as the user wants to.

REM To add numbers entered by user

CLS

A$="Y"

sum=0

DO WHILE A$="Y"

INPUT "ENTER A NUMBER"; NUM

sum=sum+num;

INPUT "ENTER Y to add more numbers "; A$

LOOP

PRINT "The sum of numbers = "; sum

The output of this program is :

DO UNTIL….LOOP The DO UNTIL…LOOP is similar to the DO WHILE…LOOP. The only difference between the two loops is that in the DO UNTIL…LOOP, the execution of the loop continues as long as the condition is false.

The general form of the DO UNTIL…LOOP is:

DO UNTIL condition

The loop comprising a set of statements to be executed

LOOP

Page 8 of 12

Let us look at an example of this loop.

A=1

DO UNTIL A>3

PRINT A

A=A+1

LOOP

PRINT "LOOP Over"

The output of this program is:

The above loop is executed as long as the condition A>3 is false or as long as A is less than or equal to 3.

Let us write the same program using the two types of DO…LOOP.

VIDEO-LINKS https://www.youtube.com/watch?v=7j7vorSobWo

DAY-4

Page 9 of 12

The two programs give the same output:

The condition in the DO…LOOP can also be placed at the end of the loop. In this case, the working of the loop remains the same except for one difference. When the condition is at the end of the loop, the loop is executed at least once even if the condition is false.

Page 10 of 12

EXITING A LOOP You can exit from a loop before the loop comes to an end on its own.

For this, you use the EXIT FOR statement in the FOR…NEXT loop and the EXIT DO statement in the DO WHILE…LOOP or the DO UNTIL…LOOP.

The following program finds the perimeter of 10 squares. The FOR…NEXT loop ends if the value of the side entered by the user is negative.

REM To find perimeter of 10 squares

CLS

FOR 1=1 to 10

PRINT "Enter the side of the square" INPUT side

IF side<0 THEN EXIT FOR

PRINT "Perimeter of Square = "; 4*side

NEXT I

PRINT "The Program Ends"

Words to Know

1. Loop: It is used to repeat the execution of a group of statements many times. 2. Looping statements: Statements that enable you to create loops.

VIDEO-LINKS

https://www.youtube.com/watch?v=7j7vorSobWo

Page 11 of 12

DAY-5 EXERCISE:

Page 12 of 12