Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Flipp Book Class-8 new1

Flipp Book Class-8 new1

Published by Memorica Graphics, 2021-03-11 10:26:05

Description: Flipp Book Class-8 new1

Search

Read the Text Version

String Operator Plus sign ( + ) is also called a string operator which is used to combine two or more than two strings. The process of combining two or more than two strings is called string concatenation. Syntax : first string + second string ……… Example: PRINT “Maitidevi” + “Kathmandu” It joins two strings and gives the output: MatideviKathmandu. Operand Operator Operands are the string or numeric values comes with the expres- sion. It may be constant or variable on which mathematical, logical S=A + 20 or string operation take place. Operand EXPRESSION An expression can be a valid single or combination of elements such as vari- able ,constant or combination of constant, variable with operators. N$, X, A$+B$, A+B, A<10, B>=10, A=10, x+10 etc. are some examples of expres- sions. QBASIC supports three types of expressions: 1. Arithmetic expression 2. Logical expression 3. String expression Arithmetic Expression Arithmetic expression contains numeric constant, variable and operators which returns a numeric value after computing. It may return computational value directly on screen or assigned the value in the variable. Examples: LET S=A+B*20 A+B*20 is an arithmetic expression which returns computational value and assigns in the S variable. PRINT 40+30/15 Smart Computer Science Book-8 345

40+30/15 is an arithmetic expression which returns the computational value on the screen because PRINT statement is used. QBASIC performs all the algebraic operation but it does not support the alge- braic expression directly. It needs to change algebraic expression to QBASIC expression to perform the operation. Some of the common algebraic expres- sion and their valid QBASIC expression are given as: Algebraic Expression Valid QBASIC Expression A+B (A+B)/5 5 X^2+Y^2 x2+Y2 48^0.5 P*T*R (PxTxR) Logical Expression The logical expression contains constant, variable, relational operators and logical operators which evaluates and returns true or false values (-1) or (0) respectively. The value returned by logical expression is called Boolean value. Examples: PRINT 6>5 It returns true (-1) value because expression is true PRINT 4>10 AND 10>5 It returns false ( 0 ) value because expression is false PRINT A>B It returns the result true (-1) or false (0) which depends on value of A and B PRINT A<20 It returns the result true (-1) or false (0) which depends on value of A. PRINT 10=10 It returns true (-1) because expression is true. PRINT 20 <> 10 346 Operator, Operand & Expression

It returns true (-1) because expression is true. String Expression String expression is a combination of two or more than two string data or string variables with the plus (+) sign is called string expression. After computation, it returns a single string in combination form. Examples: LET N$= “Rahul” + “Khadka” It combines two string “Rahul” and “Khadka” and the computational value Ra- hulKhadka is assigned in N$ variable. PRINT “Rahul” + “Khadka” It combines two string “Rahul” and “Khadka” returns the computational value Ra- hulKhadka and displays on the screen. LET X$=A$+B$ It combines contain of A$ and B$ and assigns in X$ variable. Lab Activities 1. Rahul and Rohit are eager to practice the different types of expressions in the immediate mode. Computer science teacher has provided the following expressions to them. Expression Result Type of Exp. PRINT 2+10 * (5-3) PRINT 20>60 PRINT 20>60 AND 40>50 PRINT 20<60 AND 40>50 PRINT 70>60 OR 40>50 PRINT 20<60 AND 40>50 PRINT “RAM” + \" \" + “CHHETRI” PINT “Raju” + \" \" + “Raja” + \" \" + “Ram” Smart Computer Science Book-8 347

Points to Know • Operators are special symbols which performs the operation during the program execution. • QBASIC supports four types of operators: Arithmetic, Relational Logical and String operators. • Arithmetic operators always operate on numeric constant and variable to perform the mathematical operation. • Relational operators are used to compare two values that is string or numeric which come with logical expression. • Logical operators are used to combine two or more relational expressions or condi- tions and returns single value true (-1) or false (0). • AND, OR and NOT are common logical operators supported by QBASIC. • Plus sign ( + ) is also called a string operator because it is also used to combine two or more than two strings. • Operands are the string or numeric values comes with the expression. • An expression can be a variable or constant or combination of constant, variable or both with operators. • QBASIC supports three types of expressions which are Arithmetic expression, Logi- cal expression, String expression. • The value returned by logical expression is called Boolean value. • It needs to change algebraic expression to QBASIC expression to perform the op- eration.. Terms to Know Operator : Special Symbols which perform operation. Expression : Combination of elements with operators which return a result. Operand : Values exist with expression. Logical : Relational analysis String : A data type that can not perform the mathematical operation. Concatenation : Joining two or more strings. 348 Operator, Operand & Expression

Worksheet Objective Questions 1. Fill in the blanks: a. ............................ perform the operation during the program execution. b. ............................. operators perform mathematical operation. c. ........................... symbol has first priority of operation among the arithmetic operators. d. MOD has the .......................... priority of the operation. e. ......................................... operators are used to compare two values.. f. ................................ operator logically reverse an outcome. g. .......................... are values come with expression. h. The value returned by logical expression is called ................................ value. 2. State wether the following statements are true of false: a. Computer can perform the task itself. b. Operators always comes with expression. c. Result prepared by operators will be displayed or assigned in vari- able. d. MOD is a logical operator. e. If expression contains the same order of priority of operators the operations are done from right to left. f. Integer division has the second priority of the operation. g. Relational operators are used to compare the value. h. Relational operators comes with the logical expression. i. Logical expression always returns the true (-1). Smart Computer Science Book-8 349

j. Logical operators are also called conjunction operators. k. AND returns true (-1) when all the conditions are satisfied. l. OR operators returns true when all the conditions are false (0). m. + sign is the arithmetic as well as string operators. n. Operands are always variable. o. QBASIC supports algebraic expression directly. Descriptive Questions 1. Answer the following questions: a. What do you mean by operators? b. Write the type of operators supported by Q-BASIC. c. What do you mean by arithmetic operators? d. What are the common logical operators? e. Why are logical operators called conjunction operators. f. What do you mean by string concatenation? g. What do you mean by operand? h. What is expression? i. Write the function of arithmetic, logical and string expression? j. What do you mean by hierarchy of arithmetic operators? Analytical Questions 1. Solve the following expression in the basis of priority of operation. a. 5 + 3 * 6 - 2 b. 6 * 3^2 - 4 c. 18 / 9 + 3 ^ 2 - 3 d. 18 \\ 5 + 10 - 2 ^ 2 e. 2 + 3 * 5 - 4 ^ 2 f. 5 * 3 ^ 2 \\ 2 + 5 g. 4 * 5 MOD 4 h. 24 / 6 * 3 ^ 2 350 Operator, Operand & Expression

2. Sundar Budhathoki has been assigned a task to form the expression to solve for the following cases in the immediate mode of QBASIC. What expressions would Sundar Budhathoki form? a. To find the sum of his obtained marks of eight subjects. b. To find the total amount of 10 pieces of pen of rate Rs.45. c. To calculate total amount of pen, copy and eraser, the rate of respective items are : 45, 80 and 10 . d. To find the sum of square value of any two numbers. e. To find the simple interest of principal amount 50,000, interest rate 13% and time is 3 years. Smart Computer Science Book-8 351

Q-basic Statements CHAPTER 16 Juna : Actually, what is meant This chapter includes: of statement, Ma'am? • Assignment Statements Muna : Good question. Most of • Input/Output State- the instruction starts with certain reserved ment word called statement. • Other Common State- ments Juna : Oh! How many types of statement are there, Ma'am? Muna : There are different types on the basis of its functionality. Juna : Is there any difference between statement and expression ? Muna : Expression always produces the calculation but statement instructs the computer to perform the task. Finally, all the expressions are derived by statement. INTRODUCTION OF STATEMENT Computer needs instructions to perform a task. Most of the instruction starts with certain reserved word of QBASIC followed by certain rule is called state- ment. Statements are formed with reserved word and expression. Rules fol- lowed by statement is known as syntax. A reserved word is also known as 352 Q-basic Statements

keyword which has specific meaning to perform the task for interpreter of QBASIC. Statement of QBASIC is classified into different types some of com- mon type are : a. Assignment Statement b. Input/output statement c. Non Executable Statements d. Control flow statements e. Declaration Statements f. Color and Graphics statements Assignment Statement The statements which assign numeric or string value to a variable is called assignment statement. The common assignment statements of QBASIC are: LET, SWAP, READ-DATA. LET statement Function : LET is an assignment statement, which is used to assign a value string or numeric to a variable. It is an optional statement. Syntax : [LET] variable=data Variable is a string or numeric variable and data is a value to be as- signed to the variable which may either string or numeric data Example : LET A=20 LET B= 10 LET N$= “Ramu” LET AD$= “Udayapur, Gaighat -5” Or A=20 B= 10 N$= “Ramu” AD$= “Udayapur, Gaighat -5” READ-DATA statement Function : READ-DATA statement is an assignment statement. READ is used to read the data whether numeric or string given with the DATA statement and assign to the variable listed. Syntax : READ variable1, variable2, variable3……….. DATA item1, item2, item3…………………. • DATA is a non executable statement, which provides any type of data numeric Smart Computer Science Book-8 353

or string to the variable read with READ statement. Example : CLS READ N$, AD$, PH PRINT N$, AD$, PH DATA “Rohan”, “Katari-4”, 035-4430914 END • Here, First data is supplied in first variable, second date is supplied in second variable and third data is supplied in third variable. SWAP statement Function : SWAP statement is an assignment statement, which is used to inter- change the two similar type of variable. Syntax : SWAP variable1, variable2 Example : CLS A=20 B=30 PRINT A-B SWAP A,B PRINT A-B END • In this example, before using the SWAP statement, it gives the output -10 but after using the SWAP statement, it gives output 10 because value of A & B vari- able is interchanged. Input Output statement The statements which are used to input data to the computer and print the result after processing is called Input/output statement. QBASIC supports dif- ferent types of input output statements, such as CLS , INPUT, LINEINPUT, PRINT, etc. CLS statement Function : It is a input/output statement which is used to clear the screen. Syntax : CLS Example : CLS 354 Q-basic Statements

INPUT statement Function : It’s function is to allow to read data from the keyboard. It is a standard way of input data. Syntax : INPUT [prompt][;][,] variable1, variable2…. Example : INPUT N$ INPUT PH • Here, INPUT N$ and INPUT PH, they do not display any prompt, it displays simply question marks and program will halts for keyboard entry. You have to type name and press enter key and type phone number and press enter key. It is very difficult to know, what to input. It is poor input system INPUT \"Enter Your Name:\"; N$ INPUT \"Enter Your Phone Number:\"; PH • Here, display prompt as : Enter Your Name? and Enter Phone Number?. After display a prompt, program will halts for keyboard entry. It is very easy what you have to type. It is a standard input system. • Here, single INPUT is receiving single value. After typing a value, you should press enter key for next INPUT. INPUT \"Enter name address and phone number:\"; N$, AD$, PH • Here, Single INPUT is receiving multiple values. When you run your program, you should type your data separated by comma as given: Enter name, Address and Phone number: Lakpa, Ratopul, 4438703 LINE INPUT statement Function : Its function is to enter long sentence from the keyboard including spe- cial characters such as commas, quotes, slashes, semicolon and oth- ers characters supported by QBASIC. It allows to enter up to 255 char- acters long sentences. The LINE INPUT is used only for string type of data. Syntax : LINE INPUT [prompt;] variable$ LINE INPUT #file number%, variable$ Example : CLS LINE INPUT “Enter long string:”;S$ PRINT S$ END • This program allows to enter long sentence up to 255 characters contains Smart Computer Science Book-8 355

spaces, commas, semicolon, slashes, etc. PRINT statement Function : Its function is to display the output result on the soft copy output device called monitor. Syntax : PRINT [prompt][Expression] separator [expression] separator …. • Expression may be constant or variable, Separator may be comma , or semi colon ;. Semicolon allows to print immediately after the last data leaving one space but Comma allows to print in next zone. Example : PRINT “RAju Raja Ram” PRINT N$ PRINT “MAdhu” + “Gautam” PRINT N PRINT 120 PRINT N$, AD$ PRINT N$;AD$ PRINT 10, 20, 30, 40, 50 PRINT 10;20;30;0;50 PRINT X+Y PRINT X$+Y$ PRINT 10+20-10 PRINT 10+20*5, 50-10 +5 Trick & TIPS • If a number is displayed by the PRINT statement, it takes a leading blank space before a positive number and a minus sign before a negative number. • ? Question mark stands stands for PRINT. Other Common Statements REM statement Function : It is a declaration statement; also called non executable statement be- cause it does not give any output result. It is used to write important note or explanation in the program for remembrance. Syntax : REM remark text ' remark text 356 Q-basic Statements

Example : REM This is my first QBASIC program ' This program calculates simple interest • Apostrophes symbol ‘ stands for REM, in place of REM you can use ‘ apostro- phes symbol. END statement Function : It is a declaration statement that ends a BASIC program, procedure or block. Syntax : END [FUNCTION | IF | SUB | SELECT] Example : END END IF END SELECT END SUB END FUNCTION Sample Programs 1. Write a program to ask any two numbers and find their sum with and without IN- PUT and PRINT prompt. REM : With INPUT & PRINT prompt REM : Without INPUT & PRINT CLS prompt INPUT \"Enter first number:\";A CLS INPUT \"Enter second number:\";B INPUT A S=A+B INPUT B PRINT \"Sum=\"; S S=A+B END PRINT S END Smart Computer Science Book-8 357

2. Write a program to ask any two num- CLS bers and fin their product. Use Single INPUT \"Enter principal Amount:\"; P INPUT to receive tow number and INPUT \"Enter Time:\"; T each INPUT to each number. INPUT \"Enter Rate:\", R SI = (P * T * R) / 100 REM : With single INPUT with PRINT \"Principal Amount:\"; SI Prompt END CLS INPUT \"Enter two number:\";A Test Your Skill P=A*B PRINT \"Product=\";P 1. Write a program to ask a number and END display it's square value. 3. Write a program to read 10, 20, 30, 40 …………………………………………… and 50 using READ -DATA statement and find their sum. …………………………………………… CLS READ A, B, C, D, E …………………………………………… S=A+B+C+D PRINT \"Sum=\";S …………………………………………… DATA 10, 20, 30, 40, 50 END ……………………………………………... 2. Write a program to ask length and 4. Write a program to ask two numbers and find their sum, product, difference breadth of an rectangle and find out and product. it's area. Hint-: A=L*B. CLS INPUT \"Enter two numbers:\"; A, B …………………………………………… S=A+B D=A-B …………………………………………… P=A*B PRINT \"Sum=\"; S …………………………………………… PRINT \"Difference=\"; D PRINT \"Product=\"; P …………………………………………… END …………………………………………….. 5. Write a program to ask principal 3. Write a program to ask quantity and amount, time and rate and find the simple interest. SI= (P*T*R)/100 rate of pen and find out total amount. …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………….. 358 Q-basic Statements

4. Write a program to ask three numbers of 5 divided by 2. Use READ---DATA. and find their average and sum. ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................. ………………………………….................. 8. Write a program to ask Meter and ................................................................ convert into Kilometer. ................................................................ ……………………………………………… ..................................................... ……………………………………………… 5. Write a program to ask your name, ad- ……………………………………………… dress and telephone number and print them. ……………………………………………… ……………………………………………… …………………………………................. 9. Write a program to ask Kilometer and ……………………………………………… convert into meter. ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................. ……………………………………………… ................................................................ ……………………………………………… ................................................................ ………………………………….................. 10. Write a program to ask Nepali curren- ................................................................ 6. Write a program to ask a number and cy and convert into Indian currency. Hint-: IC Rs. 1 = NC Rs. 1.60 divided by 2 and print quotient ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................. ………………………………….................. 7. Write a program to print the remainder Smart Computer Science Book-8 359

Points to Know • Each instruction starts with certain reserved word of QBASIC followed by certain rule is called statement. • Rules followed by statement is known as syntax. • A reserved word is also known as keyword which is understood by interpreter of QBASIC to perform the task. • Common type of statements supported by QBASIC are : a. Assignment Statement b. Input/output statement c. Non Executable Statements d. Control flow statements e. Declaration Statements f. Color and Graphics statements • The statement which does not produce the output result is called non executable statement. Terms to Know Interpreter : Translates a program line-by-line. Swap : Interchange the values of two variables. Statement : Instruction start with reserved word. Syntax : Rules followed by statement Keyword : Reserved word of QBASIC. Input : Supply data to the computer. Output : Result produced by computer. Worksheet Objective Questions 1. Fill in the blanks: a. ............................ statement is used to ends a Basic program. b. REM is a non ............................. statement. c. ..................... statement is used to display the output. d. ...................................... statement allows to enter up to 255 characters long 360 Q-basic Statements

sentence. e. SWAP statement is used to ................................ the two similar types of vari- able. f. LET is used to .......................... a value to a variable. 2. State wether the following statements are true of false: a. LET is an optional assignment statement. b. REM produces the result. c. SWAP statement is an assignment statement. d. END is a declaration statement. e. LINE INPUT is a standard way of input data. f. DATA statement is non executable statement. Descriptive Questions 1. Answer the following questions: a. What is statement? b. What do you mean by syntax? c. What is reserved word? d. What do you mean by assignment statement? e. What do you mean by Input/Output statement? f. What do you mean by non executable statement? Analytical Questions PRINT S PRINT P 1. Write the output of: PRINT D END a. CLS LET A = 10 b. CLS LET B = 20 READ A, B LET S = A + B LET P = A * B LET D = A - B Smart Computer Science Book-8 361

R = A MOD B b. REM : Read the length and breath of Q=A/B rectangle and display area. Q1 = A \\ B READ LENGTH, BREADTH A=A+1 LET A=L*B B=B+1 DISPLAY \"Area=\";A Q2 = A \\ B DATA 4,3 PRINT R END PRINT Q PRINT Q1 3. Write a program to ask name, gender, PRINT Q2 age of a person and print. DATA 18, 4 END 4. Write a program to ask sales amount of sales man and calculate his 10% sales c. CLS commission. A = 10 B = 20 5. Write a program to ask yearly income D=A-B and expenditure of a person and find PRINT D out his saving. SWAP A, B D=A-B 6. Write a program to ask unit consumed PRINT D of electricity and rate. Now, find out to- END tal amount to pay. 2. Debug the following programs: a. REM : Enter five subject marks and find the average marks. CLS ENTER \"Input 5 subject Marks:\"; M, S, E, N, C TOTAL MARKS=M+S+E+N+C AVG MARKS=T/5 DISPLAY \"Total Marks=\";T PRINT \"Average Marks=\";AV END 362 Q-basic Statements

17Control Flow Statements CHAPTER Juna : Wow! I saw computer can make decision. But I This chapter includes: am surprised. • Introduction of Control Muna : Yes, Computer can take Flow Statement decision on the basis of condition . It can take • IF statement • SELECT-ENDSELECT State- ment • GOTO Statement decision such as pass or fail, hot or cold, etc. Juna : How is it possible? Muna : Due to conditional statement, it is possible. Juna : What types of conditional statement, Ma'am? Muna : IF, Select Case, GO TO statements are used to take decision. Using these statement, we can easily instruct to the com- muter to take decision on the basis of our data. CONTROL FLOW STATEMENTS When you execute a program, it starts to execute from beginning to last one after another instructions which is called program execution flow. This is also called default flow of the program execution. But in the program, some time it should be alter the program execution depending on the problem. The state- ments which are used to alter the execution flow or control the execution flow is called control statements. The QBASIC Supports the different types of Con- Smart Computer Science Book-8 363

trol flow statements, some of the common control flow statements are: a. IF Statement b. SELECT- END SELECT Statement c. GOTO Statement d. LOOP statements IF statement Function : It is a conditional control flow statement, also called decision making statement or conditional branching statement. It is used to select a statement or statement block depending on the conditions. If the condi- tion is satisfied, it executes one block of statements; otherwise, it ex- ecutes another block of statements. Syntax : it has three different types of structure of syntax. Let’s discuss one by one. Entry Point Structure 1 : IF <condition> THEN <statement > OR Is condition Yes Execute True block IF <condition> THEN True ? <Statement block> No ........................ Exit Point END IF Next Statement • Here, condition may be string or numeric expression and statement block is a series of instructions. Example : (i) CLS INPUT “Enter Average Marks:”; AV IF AV>40 THEN PRINT “You are Passed” END (ii) CLS INPUT “Enter Average Marks:”; AV IF AV>40 THEN PRINT “You are Passed” END IF END • In these programs, if the value of the AV variable is more than 40 then condition 364 Control Flow Statements

will be satisfied and execute after the THEN and print ‘You are passed’ other- wise execution will take place at the next statement. Structure 2: IF <condition> THEN <statement block> ELSE <statement> Or Entry Point IF <condition> THEN <Statement block> ……………………. Is condition Yes Execute True block ……………………. ELSE True ? <Statement block> No …………….......... Execute False block ……………........... Exit Point END IF • In this case if the condition is satis- Next Statement fied, it executes the statement given after the THEN; otherwise it executes the statement given after the ELSE. Example : (i) CLS INPUT “Enter Average Marks:”; AV IF AV>40 THEN PRINT “You are Passed” ELSE PRINT “You are Failed” END (ii) CLS INPUT “Enter Average Marks:”; AV IF AV>40 THEN PRINT “You are passed” ELSE PRINT “You are failed” END IF END • In these programs, if the value of the AV variable is more than 40 then condition will be satisfied and execute after the THEN and print ‘You are passed’ other- wise execution will take place after the ELSE part and displays ‘You are failed’. Smart Computer Science Book-8 365

Structure 3 : Entry Point IF <condition1> THEN <Statement block1> Is condition Yes Execute statement block-1 ………………….. ELSEIF <condition2> THEN True ? <Statement block2> ………………….. No ELSEIF <condition3> THEN <Statement block3> Is condition Yes Execute statement block-2 ………………….. ........................... True ? [ELSE ELSEIF <condition4> THEN No <Statement block4> Execute statement block-3 …………………..] Exit Point END IF Next Statement • In this case, if the condition1 is satisfied, it executes statement block1 and exit form IF-END IF block. If the condition1 is not satisfied, it tests the condition2. If the condition2 is satisfied, it executes statement block2 and exit from the IF-END IF and so on. If all the condition are not satisfied then ELSE block of statement will be executed. The ELSE is an optional clause. If you do not apply, IF-END IF does not produce any action, if all the conditions are false. Example : CLS INPUT \"Enter Your Age:\"; A IF A <= 10 THEN PRINT \"You are Child\" ELSEIF A > 10 AND A <= 20 THEN PRINT \"You are Teenager\" ELSEIF A > 20 AND A <= 40 THEN PRINT \"You are Young\" ELSE PRINT \"You are Old\" END IF END 366 Control Flow Statements

• When you execute the above program, it allows you to enter your age and that is assigned in the A variable. If the value of A is smaller than 10, it displays ‘You are child’ because the first condition is satisfied. If first condition is not satisfied, it tests second condition, if the value of A become more than 10 and less than or equal 20, it displays ‘You are Teenager’. If this condition also not satisfied, it tests next condition. If the value of A become more than 20 and less than or equal 40, it displays, ‘You are Young’. If this also not satisfied, it takes place after the ELSE and displays ‘You are Old’. Trick & TIPS • Single line If is not necessary to close by END IF. But double line and multiple line IF must be closed by END IF. Sample Programs PRINT \"Greatest is :\"; B ELSE 1. Write a program to ask any two num- PRINT \"Greatest is :\"; C bers and find out greater number. END IF CLS END INPUT \"Enter first number:\"; A INPUT \"Enter second number:\"; B 3. Write a program to ask any three num- IF A > B THEN bers and find out middle numbers. PRINT \"Greater is :\"; A CLS ELSE INPUT \"Enter first number:\"; A PRINT \"Greater is :\"; B INPUT \"Enter second number:\"; B END IF INPUT \"Enter Third number:\"; C END IF A > B AND A < C OR A > C AND A < B THEN 2. Write a program to ask any three num- PRINT \"Middle is:\"; A bers and find out greatest numbers. ELSEIF B > A AND B < C OR B > CLS C AND B < A THEN INPUT \"Enter first number:\"; A PRINT \"Middle is\"; B INPUT \"Enter second number:\"; B ELSE INPUT \"Enter Third number:\"; C PRINT \"Middle is :\"; C IF A > B AND A > C THEN END IF PRINT \"Greatest is :\"; A ELSEIF B > A AND B > C THEN 4. Write a program to ask a number and Smart Computer Science Book-8 367

find out it is divisible by 3 or not. ……………………………………………… CLS ………………………………….................. INPUT \"Enetr a number:\"; N ……………………………………………… R = N MOD 3 …………………………………................. IF R = 0 THEN ............................................................... PRINT \"It is visible\" ............................................................... ELSE PRINT \"It is not divisible\" 2. Write a program to ask any three num- END IF bers and find out smallest numbers. END 5. Write a program to ask 5 subject ……………………………………………… marks and find out total and average ……………………………………………… marks. and the program should find ……………………………………………… out you are passed or failed. If your ……………………………………………… average marks is above 40,you are ………………………………….................. passed otherwise failed. ……………………………………………… CLS ………………………………….................. INPUT \"Enter 5 subject Marks:\"; A, B, C, D, E 3. Write a program to ask your age and T=A+B+C+D find out you can vote in the national AV = T / 5 election or not. If your age is above IF AV > 40 THEN 18, you can vote. Otherwise you can PRINT \"Your are passed\" not vote. ELSE PRINT \"You are failed\" ……………………………………………… END IF ……………………………………………… END ……………………………………………… ……………………………………………… Test Your Skill 1. Write a program to ask any two num- bers and find out smaller number. ……………………………………………… ………………………………….................. ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................. 368 Control Flow Statements

................................................................ 2. Write a program to ask marks in Math- ematics and find out you are eligible ................................................................ to study optional mathematics or not . If marks is not below 60, you are eli- 4. Write a program to ask a number and gible, other wise you are not eligible. it is even or odd. CLS INPUT \"Enter Maths Marks:\"; M ……………………………………………… IF NOT (M < 60) THEN PRINT \"You are eligible\" ……………………………………………… ELSE PRINT \"You are not eligible\" ……………………………………………… END IF END ……………………………………………… 3. Write a program to ask 5 subject ………………………………….................. marks and find out you are failed or passed. If any one subject marks is ……………………………………………… below 40, you are failed otherwise passed. …………………………………................. CLS ................................................................ INPUT \"Enter 5 subject marks:\"; A, B, ................................................................ C, D, E Sample Programs IF A < 40 OR B < 40 OR C < 40 1. Write a program to ask marks in com- OR D < 40 OR E < 40 THEN puter and Maths and find out you are PRINT \"You are failed\" eligible to study computer or not. If your both subject marks is above 60, ELSE you are eligible to study computer in the next class, otherwise you are not PRINT \"You are Passed\" eligible to study computer. CLS END IF INPUT \"Enter computer Marks:\"; C INPUT \"enter marks of Math:\"; M END IF C > 60 AND M > 80 THEN PRINT \"You are eligible.\" 4. Write a program to ask your percent- ELSE PRINT \"You aren't eligible.\" age marks and find out your division. END IF Percentage Division END <40 Failed <50 Third <60 Second <80 First >=80 Distinction Smart Computer Science Book-8 369

CLS END INPUT \"Enter percentage mark:\"; P IF P < 40 THEN Test Your Skill PRINT \"Failed\" ELSEIF P < 50 THEN 1. Write a program to ask your marks PRINT \"Third division\" of computer, maths and science and ELSEIF P < 60 THEN find out you are eligible or not to study PRINT \"Second division\" computer science in class IX. If any ELSEIF P < 80 THEN one subject marks is above 80, you PRINT \"First Division\" are eligible otherwise you are not eli- ELSE gible. PRINT \"Distinction\" END IF Ans:......................................................... END ................................................................ 5. Write a program to calculate simple in- terest ask principal amount and time. ................................................................ The rate of interest is given below: ................................................................ Time Rate ................................................................ 1 12% ................................................................ 2 13% ................................................................ 3 15% ................................................................ 4 18% ................................................................ Above 4 20% 2. Write a program to ask your gender CLS and find out you are male, female or third gender. INPUT \"Enter Principal Amount\"; P Ans:......................................................... INPUT \"Enter Rate of Amount:\"; T ................................................................ IF T = 1 THEN R = 12 ................................................................ IF T = 2 THEN R = 13 ................................................................ IF T = 3 THEN R = 15 ................................................................ IF T = 4 THEN R = 18 ................................................................ IF T > 4 THEN R = 20 ................................................................ SI = (P * T * R) / 100 ................................................................ PRINT \"Simple Interest=\"; SI 370 Control Flow Statements

................................................................ Ans:......................................................... ................................................................ ................................................................ ................................................................ 3. Write a program to ask your national- ................................................................ ity and age and find out you are eli- ................................................................ gible to get Nepali Citizenship or not. ................................................................ If you are NEPALI and age is above ................................................................ 18, you are eligible otherwise you are ................................................................ not eligible. SELECT CASE Statement Function : It is a control flow decision making statement or also called condition- al branching statement. It executes one statement block out of many blocks depending on the value of CASE expression. Syntax : SELECT CASE Test-expression CASE Expression-list Statement block1 …………………. …………………... CASE Expression-list Statement block2 …………………. …………………... CASE Expression-list Statement block3 …………………. …………………... [CASE ELSE Statement block4 …………………. …………………...] END SELECT Smart Computer Science Book-8 371

• It is as like as third structure of IF. It evaluates the test expression and returned value is compared with each CASE clause of expression list. It executes the block of state- ments; if the test result is matched with the any CASE clause and rest of other blocks are ignored. CASE ELSE is an optional clause. If all the test is not matched with any CASE expression list, it executes CASE ELSE, if it is exist, otherwise it does not produce any action. Characteristics of SELECT- END SELECT a. Test expression may be numeric or string. b. IS keyword is must be used with any relational operator. c. Expression list may be applied as given below: • CASE 1 • CASE 1, 2, 3, ……………… • CASE 1 TO 10 • CASE “A” • CASE “A”, “B”, “C”, ………… • CASE “A” TO “Z” d. Expression list can have any one of the above format or a combination of them, separated by comma. Sample Programs END 1. Write a program to ask your age and • In this program only one CASE clause find out you are old or not. If age is is used. If the value of test expression above 60, old otherwise you are not A is matched with the CASE clause, it old. displays the message ‘You are Old’ and execution keep continue after the END- CLS SELECT. If condition is not matched, it starts to execute the statements men- INPUT “Enter Your Age:”;A tioned after END-SELECT. SELECT CASE A 2. Write a program to ask a number and find the even or odd. CASE IS>60 CLS INPUT “Enter a number:”;N PRINT “You are Old” R= N MOD 2 CASE ELSE PRINT \"Not Old\" END SELECT 372 Control Flow Statements

SELECT CASE R 4. Write a program to ask a character CASE 0 and find out vowel or constant. PRINT “Even Number” CLS CASE 1 INPUT \"Enter a character:\"; C$ PRINT “Odd number” SELECT CASE C$ END SELECT CASE \"A\", \"E\", \"I\", \"O\", \"U\", \"a\", \"e\", \"i\", \"o\", \"u\" END PRINT \"it is vowels\" • In this program, CASE ELSE clause is not used. If the value of test expression CASE ELSE is not matched with any CASE expres- sion, it doe not come in any action and PRINT \"It is not vowel\" keep continue the execution after END SELECT. END SELECT END 3. Write a program to ask a character 5. Write a program to ask a number and and find out input character is capital find out that is your lucky number or small or other character. not. If input numbers are 3,7,11 then message should display \"It is my CLS lucky number\" otherwise \"It is not my lucky number\". INPUT “Enter a character:”;C$ CLS SELECT CASE C$ INPUT \"enter Lucky number:\"; L CASE “A” TO “Z” SELECT CASE L PRINT “Capital letter” CASE 3, 7, 11 CASE “a” TO “z” PRINT \"It is my lucky PRINT “Small letter” number\" CASE ELSE CASE ELSE PRINT “Other character” PRINT \"It is not my END SELECT lucky number\" END END SELECT • In this program, CASE ELSE clause is END used. If the value of test expression is not matched with any CASE expres- 6. Write a program to ask class and find sion, it executes CASE ELSE clause out your group for the quiz competi- statement block. And keep continue the tion organized by the School in the execution after END SELECT. basis of given conditions: Class Group 1 to 3 A Smart Computer Science Book-8 373

4 to 6 B ............................................................... 7 to 10 C ............................................................... ............................................................... CLS ............................................................... ............................................................... INPUT \"Enter Class [1- 10]:\", G ............................................................... ............................................................... SELECT CASE g ............................................................... ............................................................... CASE 1 TO 3 ............................................................... ............................................................... PRINT \"you are in ............................................................... ............................................................... group*A*\" ............................................................... ............................................................... CASE 4 TO 6 2. Write a program to ask a number and PRINT \"your are in find out it is divisible by 3 or not. group *B*\" Ans:....................................................... CASE 7 TO 10 ............................................................... ............................................................... PRINT \"you are in ............................................................... group *C*\" ............................................................... ............................................................... CASE ELSE ............................................................... ............................................................... PRINT \"Wrong entry !\" ............................................................... ............................................................... END SELECT 3. Write a program to calculate simple END Test Your Skill 1. Write a program to ask name of day of the week in 1 to 7 and display day such as 1 for Sunday, 2 for MON- DAY and so on. Ans:....................................................... ............................................................... ............................................................... ............................................................... 374 Control Flow Statements

interest the program should ask prin- 4. Write a program to ask destination to cipal amount, time and the rate is de- travel and number of passenger and fined in the basis of following condi- find out total amount to pay. The rate tions: per person is as : Time (year) Rate Destination Rate 1 to 3 5% Pokhara 400 4 to 6 8% Janakpur 600 7 to 10 10% Biratnagar 1000 Above 10 15% Butwal 500 Ans:........................................................ Ans:......................................................... ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................. ................................................................ ................................................................. Smart Computer Science Book-8 375

GOTO statement Function : It is an unconditional control flow statement. It is used to transfer the execution flow of a program one part of the program to another part without condition. Syntax : GOTO [line number][label name] Example-1 Output Example-2 Output CLS CLS I=1 I=1 abc: IF I<=5 THEN 10 IF I<=5 THEN PRINT I I=I+1 PRINT “Ram” GOTO abc NED IF I=I+1 GOTO 10 NED IF END END • Here, Line number is written as GOTO • Here, label name is written as GOTO 10. It transfers the execution follow where is mentioned line 10. abc. It transfers the execution follow where is labeled abc. Test Your Skill : abc P=A*B 1. Write the output of following pro- PRINT \"Difference=\";D grams: END CLS REM :Example of without condition Output A=20 B=10 S=A+B GOT 10 20 PRINT “Difference=”;D GOTO abc 10 PRINT “Sum=”;S D=A-B GOTO 20 376 Control Flow Statements

Points to Know • The program execution goes from beginning to last one after another instructions which is called program execution flow. • The statements which are used to alter the execution flow or control the execution flow is called control statements. • QBASIC Supports the following common control flow statements are: a. IF Statement b. SELECT- END SELECT Statement c. GOTO Statement d. LOOP statement Terms to Know Instruction : An order given to a computer processor by a computer pro- gram. Control flow : The order instructions are executed or evaluated when a pro- gram is running. Execution : Carrying out of a plan, order, or course of action. Branching : Split the control flow into two or more pathways which are usu- ally the result of conditional statements Conditional statement : A set of rules performed if a certain condition is met. Statement block : A sequence of zero or more statements enclosed in braces. Worksheet Descriptive Questions 1. Answer the following questions: a. When do you use control flow statement? b. What do you mean by program execution flow? c. Write the name of control flow statements supported by QBASIC. d. When do you use GOTO statement. 2. Write a program to ask a number and find out it is divisible by 5 or not. 3. Write a program to ask a number and it is divisible by both 2 and 3 or not. 4. Write a program to ask three numbers and find out smallest number. Smart Computer Science Book-8 377

5. Write a program to ask marks of computer and find out you can study computer or not. if any one subject marks is above 80 you can study otherwise you can not study. Project Work a. Write the algorithm, flowchart and QBASIC programming codes in the chart paper to present in the class room. Your problem is to ask eight subject marks, calculate total and percentage marks and find out your grade in the basis of following condi- tions: Percentage Division >=90 A+ >=80 A >=70 B+ >=60 B >=50 C+ <50 C 378 Control Flow Statements

Looping Statements CHAPTER Juna : Oh ! Some times com- 18 puter has to repeat a task many times. If my This chapter includes: computer has to print my name 100 times, how • Introduction of Loop Structure • FOR-NEXT statement • WHILE-WEND Statement • DO-LOOP Statement does computer do ? Muna : Hahaaa ! It's too simple. There is the Loop State- ment. Loop Statements instructs the computer to repeat a task as we like. Juna : How does computer know to repeat this several times? Muna : Well ! We can define condition, Until and unless match our condition, it repeats the task. Juna : Are there different types of Loop statements ? Muna : Yes, On the basis of nature of repetition of task, there are different types of Loop statements. I will illustrate in detail. INTRODUCTION OF LOOP In our daily life, a lot of our tasks are repeated. In the same way, In the com- puter also, some tasks are repeated. To repeat a task in the computer, there is a looping technique. The statement which is used to repeat a task during the program execution Smart Computer Science Book-8 379

is called loop statement. Loop statements allow to execute one or more than one statement for a number of times as long as condition is matched. Some time you face a situation to repeat a task many number of times. It is very tedious to write same code repeatedly many numbers time, in such situation, you can use loop statement for the best performance. Qbasic supports the following loop statements. a. FOR-NEXT b. WHILE-WEND c. DO - LOOP FOR- NEXT statement Function : Its function is to repeat a given instructions specified number of times. It has a defined a range of values which is controlled by control variable and represents number of repetition of loop. In every iteration or loop, counter variable receives a new value and loop will be over when it takes whole range. Syntax : FOR <counter variable> = <initial value> TO <final value> [STEP incre- ment/decrement] <Statement block> …………………….. …………………….. NEXT [same counter variable] Examples: (i) : CLS FOR I= 1 TO 10 STEP 1 PRINT “Ram Chandra” NEXT I END • Here, loop repeats ten numbers of times and it prints the given string ‘Ram Chandra’ ten times. Loop will be over, when value of I is greater than 10 be- cause it written in increment form. (ii) : CLS FOR I= 10 TO 1 STEP -1 PRITN I NEXT I END 380 Looping Statements

• Here, loop repeats ten numbers of times and it prints the value carrying in I variable. First time value of I is 10, second time 9, third time 8, so on. Loop will be over, when value of I is greater than 1 because it written in decrement form. Characteristics of FOR a. Loop block is started from FOR statement and closed with NEXT. b. Counter variable is always numeric type which controls repetition of the loop. c. STEP is an optional clause. Its function is to increase or decrease the initial value of loop during the repetition of loop. d. If you have to increase the initial value of loop by 1, STEP clause is not necessary to mention, QBASIC supports this function by default. But rest of others increment or decrement we must mention. e. FOR –NEXT does not take any action in the following two situations. i) If the initial value is smaller than the final value and STEP is in decrement. ii) If the initial value is greater than the final value and STEP is in increment. f. Initial value and final value may be a variable or expression, not only value. g. Counter variable is an optional with NEXT. It is enough only NEXT. But if you are willing to apply, you must use same variable which is used with FOR. Sample Programs • By default it takes STEP+1. It is not necessary to mention. (a) CLS • Semicolon with PRINT statement displays output in same FOR I = 1 TO 10 line. PRINT I ^ 2; NEXT I END Output: (b) CLS • Expression can be also used as initial or final value to con- FOR I = 1 TO 10 / 2 trol the loop. PRINT I ^ I; NEXT END Output: Smart Computer Science Book-8 381

(c) CLS INPUT \"Enter Initial value and Final Value For LOOP:\"; X, Y FOR I = X TO Y • Variable can be used as initial or final PRINT I; value to control the loop. NEXT • Write the output if value of x=5 and END value of Y=10 Output: (d) CLS • Fraction values can be used as initial or FOR I = 1.5 TO 10.5 final value to control the loop.. PRINT I; NEXT I END Output: (e) CLS • Negative value can be used as final FOR I = 10 TO 1 STEP -1 value of loop to repeat a loop backward. PRINT I; NEXT I • Fraction value can be used in incre- END ment or decrement values. Output: (f) CLS FOR I = 10 TO 1 STEP -1.5 PRINT I NEXT I END (g) CLS • Comma is used with PRINT statement to FOR I = 1 TO 100 STEP +20 display the output in different zones. PRINT I, • + sign is an optional with increment. If it is not mentioned , by default it takes +sign with positive number. 382 Looping Statements

NEXT • Here, it is not using comma or semico- END lon in the end of PRINT statement. So, it gives the output in vertical form. Output: (h) CLS FOR I = 1 TO 5 PRINT I NEXT END Output (i) CLS • The Loop does not come in action, if FOR I = 5 TO 1 the initial value is greater than the final PRINT I; value and decrement STEP is missing. NEXT Such function of loop is called, skipped END loop. Output: • The Loop does not come in action, if the initial value is smaller than the final value (j) CLS and decrement STEP is mentioned. Such FOR I = 1 TO 5 STEP-1 function of loop is also called, skipped PRINT 5*I; loop. NEXT END Smart Computer Science Book-8 383

Sample Programs END 1. Write a program to ask your name 5. Write a program to ask your name and print 10 times. and print 'n' number of times. CLS CLS INPUT \"Enter your name :\"; N$ INPUT \"Enter your name :\"; N$ FOR I = 1 TO 10 INPUT \"Enter Number of times:\";N PRINT N$ FOR I = 1 TO N NEXT PRINT N$ END NEXT END 2. Write a program to ask 10 numbers and find their sum. 6. Write a program to ask 'n' numbers CLS and find their sum. FOR I = 1 TO 10 CLS INPUT \"Enter number:\"; N INPUT \"Enter total number to S=S+N input:\";X NEXT I FOR I = 1 TO X PRINT \"Sum=\"; S INPUT \"Enter number:\"; N END S=S+N NEXT I 3. Write a program to ask a number and PRINT \"Sum=\"; S display its multiples up to 10th terms. END CLS INPUT \"Enter a number:\"; N 7. Write a program to display first 10 FOR I = 1 TO 10 natural numbers. PRINT N * I CLS NEXT I FOR I= 1 TO 10 END PRINT I NEXT I 4. Write a program to ask a number and END display its multiplication table up to 10th terms. 8. Write a program to find the sum of CLS first 10 natural numbers. INPUT \"Enter a number:\"; N CLS FOR I = 1 TO 10 FOR I= 1 TO 10 PRINT N; \"*\"; I; \"=\"; N * I S=S+I NEXT I NEXT I 384 Looping Statements

PRINT \"Sum of first 10 natural END numbers:\";S END f. 1, 8, 27, .......................100 CLS 9. Write a program to display following: FOR I=1 TO 10 PRINT I^2; a. 1, 3, 5, 7...................19 NEXT I CLS END FOR I= 1 TO 19 STEP 2 PRINT I; Test Your Skill NEXT I END 1. Write the output a. CLS b. 10, 9, 8, 7...............1 CLS FOR I=1 TO 10 STEP 1.5 FOR I= 10 TO 1 STEP-1 PRINT I; PRINT I; NEXT I NEXT I END END CLS c. 0, 5, 10, 15, 20,...........50 Output: CLS FOR I= 0 TO 50 STEP 5 b. CLS PRINT I; FOR I= 1 TO 10/2 NEXT I S=S+I END NEXT I PRINT S d. 1, 4, 9, 16......................100 END CLS FOR I=1 TO 10 Output: PRINT I^2; NEXT I c. CLS END P=1 FOR I=1 TO 5 e. 100, 81, 64.................1 P=P*I; CLS NEXT I FOR I=10 TO 1 STEP-1 PRINT P PRINT I^2; END NEXT I Output: Smart Computer Science Book-8 385

d. CLS ................................................................ FOR I= 1 TO 5 PRINT I^2*2; ................................................................ NEXT I 3. Write a program to display only even END numbers from 2 to 100. Output: Ans:......................................................... e. CLS A=5 ................................................................ FOR I=1 TO 5 PRINT A^I; ................................................................ NEXT I END ................................................................ Output: ................................................................ ................................................................ ................................................................ 4. Write a program to display only odd numbers from 1 to 100. f. CLS Ans:........................................................ A=10 ................................................................ FOR I= 1 TO 10 ................................................................ PRINT A; ................................................................ A=A-1.5 ................................................................ NEXT I ................................................................ END ................................................................ 5. Write a program to find the sum of 20 Output: to 30. 2. Write a program to ask a number and display multiplication table up to 10th Ans:........................................................ terms. ................................................................ ................................................................ Ans:......................................................... ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ 386 Looping Statements

................................................................ 8. Write a program to display: 6. Write a program to print your school a. 5, 25, 125.............up to 5th terms name 10 times. Ans:........................................................ ................................................................ Ans:........................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ b. -25, -20, 15,.........up to 20th terms. ................................................................ 7. Write a program to find the product of Ans:......................................................... ................................................................ 1×2×3×..........6 ................................................................ ................................................................ Ans:......................................................... ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ WHILE-WEND statement Function : Its function is to repeat a series of statement or a statement mentioned in a loop block as long as condition is true. When condition returns false then loop exits and execute next statement. Syntax : WHILE <condition> <Statement block> …………………….. ………………………. WEND • Condition is an expression that will return non-zero (true)or zero (false). Smart Computer Science Book-8 387

• Statement block is any number of statements on one or more lines which are to be executed as long as condition is true. Example : (i) REM: Repeat the LOOP forward with the increment +1 CLS I = 1 WHILE I <= 10 PRINT \"Ram\" I = I + 1 WEND END • Here, initial value of loop is defined I=1 which is smaller than 10 which returns the true value. So, statement mentioned between WHILE-WNED will be ex- ecuted as long as condition is true. In this case, the condition is true when the value of I is smaller or equal to 10. The value of I is increased by 1. So, loop will be repeated 10 numbers of times. (ii) REM: Repeat the LOOP backward with the decrement -1 CLS I = 10 WHILE I >= 1 PRINT I S=S+I I=I-1 WEND PRINT PRINT \"Sum=\"; S END • Here, initial value of I is defined 10 and final value is defined 1. First time value of I =10 is greater than 1. So, condition is true, it executes statements men- tioned within a loop. Value of I is decreased by 1. So, condition will return true ten times. Characteristics of WHILE-WEND a. Variable used in the test condition must be initialized in any place before 388 Looping Statements

started the WHILE. b. The body of the loop must changes the value of the variable used in the tested condition, otherwise, condition will remain true in every repetition and loop will never terminate. Such loop is called infinite loop. c. String expression can be used in the test condition. Example Programs WEND PRINT \"Sum=\"; S a. REM: Display multiples of a number CLS END I=1 INPUT \"Enter a Number:\"; N d. REM: Example of Infinite loop WHILE I <= 10 CLS PRINT N * I I=1 I=I+1 WHILE I <= 10 WEND PRINT \"Richa\" END WEND END b. REM: Calculate factorial value of 5. CLS • Here, value of I remains same. So test I=1 condition always returns true and loop F=1 goes on, it never stops. To prevent the WHILE I <= 5 problem of infinite loop, body of the loop F=F*I should change the value of I: I=I+1 WEND e. REM: Example of modification of Infi- PRINT \"Factorial Value:\"; F nite loop END CLS I=1 c. REM: Use of string expression in the WHILE I <= 10 test conditions PRINT \"Richa\" CH$ = \"Y\" I=I+1 WHILE CH$ = \"Y\" OR CH$ = \"y\" WEND INPUT \"Enter Number:\"; N END S=S+N • Here, value of I will be changed by INPUT \"Do you want to con tinue? [Y/N]:\"; CH$ body of the poop. In every repetition, val- ue of I is increased by 1. So, the loop will Smart Computer Science Book-8 389

execute till the value of I is smaller than 4. Write a program to display follow- or equal to 10. When the value of I will be ing: 11, test condition will return false value and loop will stop. a. 10, 9, 8,.............................1 CLS Sample Programs I = 10 WHILE I >= 1 1. Write a program to ask your school PRINT I; name and print 10 times. I=I-1 CLS WEND INPUT \"Enter your school name:\"; S$ END I=1 WHILE I <= 10 b. 2, 4, 6, 8,....................20 PRINT S$ CLS I=I+1 I=2 WEND WHILE I <= 20 END PRINT I; I=I+2 2. Write a program to ask 10 numbers WEND and find their sum. END CLS I=1 c. 1, 4, 7, 10, 13.....................19 WHILE I <= 10 CLS INPUT \"Enter number:\"; N I=1 S=S+N WHILE I <= 19 I=I+1 PRINT I; WEND I=I+3 PRINT \"Sum=\"; S WEND END END 3. Write a program to display first 10 nat- Test Your Skill ural numbers. CLS 1. Write the output: I=1 a. CLS WHILE I <= 10 PRINT I; N=4 I=I+1 I=1 WEND WHILE I <= 5 END PRINT N; 390 Looping Statements

N = (N * 10) + 4 END I=I+1 Output: WEND END 2. Write a program to display multiples of 5 up to 10th terms. Output: Ans:........................................................ b. CLS ................................................................ N = 11111 ................................................................ I=1 ................................................................ WHILE I <= 5 ................................................................ PRINT N; ................................................................ N = (N - 1) / 10 ............................................................... I=I+1 WEND 3. Write a program to display square END values of first 10 natural numbers. Output: Ans:........................................................ ................................................................ c. CLS ................................................................ I=1 ................................................................ WHILE I <= 5 ................................................................ PRINT I ^ 2 * 2; ................................................................ I=I+1 ................................................................ WEND ................................................................ END ............................................................... Output: 4. Write a program to input numbers and find their product. The program d. CLS should terminate in user's choice. I=1 WHILE I <= 15 Ans:........................................................ IF I MOD 3 = 0 THEN PRINT I; I=I+1 WEND Smart Computer Science Book-8 391

................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ............................................................... ................................................................ 6. Write a program to display those num- ................................................................ ................................................................ bers divisible by 2 and 3 from 1 to 20. ................................................................ ................................................................ Ans:......................................................... ............................................................ ................................................................ 5. Write a program to ask a number and ................................................................ ................................................................ find it's factorial value. ................................................................ ................................................................ Ans:........................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ Trick & TIPS • To break down the infinite execution of loop, you should press Ctrl+PauseBreak combination of keys from the keyboard. DO-LOOP statement DO-LOOP statement is more powerful and versatile loop than WHILE-WEND loop statement. It repeats statement block mentioned within a loop WHILE or UNTIL condition is matched. The DO-LOOP statement has two structures: a. Pre-Test b. Post Test Pre-Test: This structure controls the loop in the entry point. It checks the con- dition in the entry point and if it returns the true result, it enters within a loop, 392 Looping Statements

otherwise it does not enter within a loop. At least , execution may not come once within a loop. Syntax : DO [WHILE | UNTIL]< condition> Statement block ………………… ………………… LOOP Examples : (i) REM: Example of Pre-Test with WHILE condition CLS I = 1 DO WHILE I <= 10 PRINT I ^ 2; I = I + 1 LOOP END Output: (ii) REM: Example of Pre-Test with UNTIL condition CLS I = 1 DO UNTIL I > 10 PRINT 5 * I; I = I + 1 LOOP END Output: • DO WHILE -LOOP is as like as WHILE-WEND, it executes statement block as long as condition is true. DO UNTIL-LOOP repeat a statement block as long as until condition is satisfied. Both they check the condition in the loop entry point. Post Test: This structure controls the loop in the exit point. It enters within a loop block and executes mentioned statement block and checks the condition in the Smart Computer Science Book-8 393

exit point. If it returns the true result, it repeats a loop, otherwise it does not repeat a loop and exits from this point. At least , execution comes once within a loop. Syntax : DO [Statement block ………………... ………………..] LOOP [WHILE | UNTIL] condition] Example : (i) REM: Example Of Post Test with WHILE condition CLS I = 1 DO PRINT I ^ 3; I = I + 1 LOOP WHILE I <= 10 END Output: (ii) REM: Example of Post Test with UNTIL condition CLS I = 1 DO PRINT 5 * I I = I + 1 LOOP UNTIL I > 10 END Output: • DO - LOOP WHILE repeats statement block as long as WHILE condition is true. DO– LOOP UNTIL repeats a statement block as long as UNTIL condition is satisfied. Both they check the condition in the exit point of loop. They execute statement block at least once. EXIT statement Function : It is a control flow statement that exits, DO...LOOP and FOR...NEXT loop. 394 Looping Statements


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook