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 9

Flipp Book class 9

Published by Memorica Graphics, 2021-03-11 12:04:10

Description: Flipp Book class 9

Search

Read the Text Version

3. Circle for the correct option: a. Programming languages are: i) Java Script ii) Python iii) C++ iv) All of these b. Programmer performs the tasks: i) Coding ii) Testing iii) Debuging iv) All of these c. Team members in SLDC : ii) Program Manager i) Project Manager iv) All of these iii) Software Engineers d. Program Design tools : i) Algorithm ii) Flowchart iii) Both i) and ii) iv) Source program e. Pseudocode is a: ii) Program design tool i) Program iv) None of these iii) Both i) and ii) Descriptive Questions 1. Answer the following questions: a. What is Pseudo code? b. What do you mean by flowchart? c. What is algorithm? d. Why do you use the program design tool? e. What is a computer program? f. What do you mean by programming? g. Who is known as a computer programmer? h. Write any three advantages of Flowchart. i. Write some rules of flowchart. j. What does system design include? k. What is coding? l. What is the importance of system evaluation? m. What is program implementation? Logic based Questions 1. Answer the following questions logically: Smart Computer Science Book-9 201

a. Why is SLDC important for the programming? b. Why is feasibility study is necessary? c. Why do you use a system design tool? Problem solving base questions: 1. Write an algorithm, pseudo code and draw a flowchart : a. To read marks in science, maths and computer in variable S, M and C respec- tively and find the total and average marks. T=S+M+C and AV=(S+M+C)/3. b. To read principal amount, time and rate in variable P, T and R and find out simple interest. Hint: SI=(P*T*R)/100. c. To read the length and breadth of a rectangle in variable L and B and calculate perimeter and area. Hint: P=2*(L+B) and A=L*B. 2. To read the age of a person in variable A and find whether he is a senior citizen of the nation or not. If his age is above 60, senior citizen otherwise he is not a senior citizen. To solve this problem, design a program with algorithm and flowchart. 3. To read the age of a person and find age status in the basis of following conditions: Age Status of Age <5 Baby <10 Child <18 Teenager <40 Old <60 Adult >=80 Old to solve this problem, design a program with algorithm and flowchart. 4. To read 10 numbers in a N variable and find their sum. S=S+N. To solve this prob- lem, design a program with pseudocode, algorithm and flowchart. 5. To read a number and display multiples up to 10th terms. 6. To display first 10 natural numbers (1 to 10) with their sum. 7. To read two numbers in variable NU and DENO. Divided the number NU by DENO and print the quotient, provided the variable DENO is not equal zero (0). If DENO is zero (0), Print the error message \"You can not divided by zero\". 8. Write an algorithm and flowchart to read two numbers from the keyboard and find the sum, product and difference. 9. Write an algorithm and flowchart to two numbers and find the smaller number. 10. Write an algorithm and flowchart to read three numbers and find out smallest number. 202 Programming Concept

Activities 2. Organize the digital presentation in the following topic: ii) Program Design Tool 3. Write an article for your school's news bulletin in School Bulletin the topic \"Software Development Life Cycle\" Smart Computer Science Book-9 203

10Programming in QBASIC CHAPTER Chapter Includes • Introduction of common terms • Programming Language • System Development Life Cycle • Pseudo Code • Algorithm and Flowchart INTRODUCTION QBASIC is a high level programming language which stands for Quick Beginners All Purpose Symbolic Instruction Code. It was developed by Microsoft Corporation in USA. QBASIC was released with the operating system MS-DOS (Microsoft Disk Operating System) in 1985. It is a successor version of BASIC, which was developed by John J. Kemeny and Thomas E. Kurtz in 1964 at Dartmouth college , New Hampshire. It is an interpreter based programming language. It provides user friendly environment with pull down menus, which makes user easier to work. QBASIC is useful to beginners to learn and develop the programming skill. It is also used to develop software package, games, etc. FEATURES OF QBASIC a. Smart Editor b. Simple and easy to understand c. Supports modular programming d. Supports graphical user interface e. It provides user friendly environment. f. It supports line number and line label. g. It supports versatile Looping and decision making LOADING THE QBASIC To load from the Windows environment, follow the following steps: Step-1 : Find the QBASIC folder. Step-2 : Open the folder. Step-3 : Double click on QBASIC.EXE file Now, You get the welcome Screen of QBASIC. You should press the 'Enter Key' to clear the welcome screen. 204 Programming in QBASIC

Title Bar Menu Bar View Window Immediate Window Status Bar COMPONENTS OF Q-BASIC WINDOW The Q-BASIC window consists of menu bar, title bar, status bar, view window, immediate window. View Window : It is the upper part of the window which allows to write a series of instructions. The actual program is written in this window. To get the output result of a set of instructions, you should give run command. Immediate Window : It is the lower part of the window which allows to write only one instruction at a time. After typing instruction and press- ing the Enter, you get the output result. Press F6 to switch the view window to immediate window vice versa in 32 bits of QBASIC. In the new version 64 bits of QBASIC, the im- mediate window is used to display the status of instruction. CODING Coding is the process of writing programs using commands to solve a problem. The coding is done in the view window. If any error comes, editing can be done in the same window. Type the following programs as given below and run it. Sample Program REM : Testing QBASIC Program CLS PRINT “Hello Programming World!!!” PRITNT PRINT “It is my First Program.” END RUN A PROGRAM To get the output result of the above coded programs, you must run the program. Smart Computer Science Book-9 205

To run a program, follow the following steps: Step 1 : Select ‘Start’ option from ‘Run’ menu. Now program starts to run. You get the following output. Output Hello Programming World!!! It is my First Program. Trick & TIPS • You can press Shift+F5 to run a program firm beginning and F5 keep continue the ‘Run’ command from the current position previous execution. SAVE A PROGRAM Save a program is to store a program under a file name in the disk permanently for the future use. When you save your program file, QBASC automatically add .BAS extension. To save a program, follow these steps: Steps to Follow: Step-1 : Press Alt+F to access the File menu. Step-2 : Select Save or Save As option and press Enter key. Step-3 : Now, Save dialog box appears as given. Step-4 : Select the location and type the file name. Step-5 : Now, press the Enter key or click the 'Ok' button. OPEN A SAVED PROGRAM The ‘Open’ command is used to load an already saved program from the disk into the memory. To perform this task, do the following: Steps to Follow: Step-1 : Select the ‘Open’ option of the ‘File’ menu. Step-2 : The ‘Open’ dialog box appears. 206 Programming in QBASIC

Step-3 : Select the file name from the list of files by moving the cursor using arrow or directional keys. Step-4 : Select the file and press enter key. Now, you will get the program on the view window. STARTING NEW PROGRAM The ‘New’ command is used to start new program. To use this command do the following: Steps to Follow: Step-1 : Select ‘New’ from the ‘File’ menu. It displays the dialog box, if the exist- ing program file is not saved. Step-2 : Select ‘Yes’ to save or ‘No’ to clear the screen without saving. EXIT FROM QBASIC The ‘Exit’ command is used to exit from the QBASIC program. To perform this task, do the following: Step-1 : Select ‘Exit’ from the ‘File’ menu. If the file is not saved it displays the message box to alert you to save the file. Trick & TIPS • You can press ‘Alt+F’ to activate the ‘File’ menu. Then using pull down menu and select required command. ELEMENTS OF QBASIC The programming language consists of different types of elements. Each element has own common function in the programming which are most useful to develop the computer program. Some of the common elements of QBASIC are: a. Character set b. Constant c. Variable d. Data e. Reserve word f. Operators g. Statement Character Set All the characters or symbols which are accepted by QBASIC are called character set. The character set includes the alpha character A to Z or a to z, number 0 to 9 and special symbols: + - * / \\ ^ \" ' , ; : = > < ( ) . ? $ % & ! # space. Smart Computer Science Book-9 207

Constant Constant refers to the data item whose values do not change during the execution of program. Such as “Ramu”, “Kathmandu”, 50, 10, etc . There are Numeric Constant and String Constant which are known as literal constant. Numeric Constant: Numeric constant may positive or negative numbers which performs the mathematical operations such as addition, subtraction, multiplication, etc. Rules for numeric constants • Comma is not allowed in the numeric constants. 1,000 is invalid but 1000 is valid. • Blank space is not allowed in a numeric constant. 5 000 is in valid, but 5000 is valid. • Alpha character is not used in the Numeric constant. Rs. 25 is invalid, but 25 is valid. • Minus sign ‘ – ‘ is used to represent a negative number and ‘+’ sign is used to represent a positive number. But + sign positive value takes by default. It is not necessary to write. +25 or 25 represents a positive number and -25 represents a negative number. Sample Program • In this program, 500 and 10 are the nu- meric constants. CLS QP = 500 RP = 10 TA = QP * RP D = 10 * TA / 100 NT = TA - D PRINT \"Total Amount=\"; TA PRINT \"Discount=\"; D PRINT \"Net Total=\", NT END String Constant : String constant is the combination of alphabets, numbers and special symbols written within quotation marks. String constant or string constant can not be used for the mathematical operations. Rules for string constant • String constant must be written within double quotation marks. For example RAMU is an invalid string constant but “RAMU” is a valid string constant. • Space can be given in a string constant. “Rahul Mandal” is a valid string constant. 208 Programming in QBASIC

• Number, special characters, roman number can be included in the string con- stant. For example: “Maitidevi- 33”. Sample Program • In this program Rahul Mandal\", \"Shira- ha, Lahan-5.\" and “033-2550607.\" are CLS string constant. N$ = \"Rahul Mandal\" AD$ = \"Shiraha, Lahan-5.\" PH$ = \"033-2550607.\" PRINT N$ PRINT AD$ PRINT PH$ END Symbolic Constant: Symbolic constant is name that substitute for a sequence of character that cannot be changed. The character may represent a numeric constant, a character constant, or a string. When the program is run, each occurrence of a symbolic constant is replaced by its corresponding character sequence, it won't change. They are usually defined at the beginning of the program. Sample Program • In this program PI is a numeric constant, the value of PI never replaced with any CLS other numeric data or value obtained CONST PI =3.14 expression. INPUT \"Enter Radious:\";R A=PI*R^2 PRINT \"AREA OF CIRCLE=\"; A END Variable Variable is a character or group of character which represents reference or name to any data for the computer memory. The value of a variable may be changed during the execution of the program. For example A, N$, ROLL, R!, Q#, X& are some valid variables. There are two types of variables: a. String variable b. Numeric variable String Variable: A variable which stores string type of data or value is called string variable. For example: “Ram”, “Maitidevi-33” “Class –IV” etc. N$, AD$, CL$ are valid string variables. $ dollar sign is used in the end of the string variable. Smart Computer Science Book-9 209

Numeric Variable: A variable which stores numeric type of data or value is called numeric variable. Numeric type of data may negative of positive. For example: 50, 500,400, etc. there are four types of numeric variables: a. Integer Variable b. Long Integer Variable c. Single Precision Variable d. Double Precision Variable Integer Variable: Integer variable stores integer number which may negative or positive whole number. Percentage (%) sign is used in the end of integer variable. Integer variable supports the range of value -32768 to 32767. For example: A%=50, A% is an integer variable and 50 is the whole number. Long Integer Variable: Long integer variable stores long range of integer number. It can support the range of data -2,147,483,648 to 2,147,483,647. Ampersand (&) symbol is used in the end of long integer variable. For example: N&=234, N& is a long integer variable and 234 is a whole number. Single Precision Variable: The single precision variable can store the number with up to seven digits accuracy. It is a default variable. It consumes 4 bytes memory space. Exclamation ( ! ) symbol is used in the end of single precision variable name. For example: A!=50.50,here A! is a single precision variable and 50.50 is a fraction value. Double Precision Variable: Double Precision variable stores long range of fractional value. It gives up to 15 digits accuracy to store data. It reserves 8 bytes memory space. The hash symbol ( # ) is used in the end of double precision variable. For example: N#=789.45, here N# is a double precision variable and 789.45 is a fractional value. Rules for Naming a Variable • Variable name starts with an alpha character (A to Z or a to z). For example N$, N% are valid variable name. • Variable name may be given up to 40 characters long. • Blank space is not allowed in the variable name. For example: ROLL NO is not 210 Programming in QBASIC

valid, but ROLLNO is a valid variable name. • Keywords or reserve words of QBASIC are not allowed as a variable name. IN- PUT!, LET% are invalid variable name. DECLARATION OF VARIABLES The declaration of variables is most important in the programming. QBASIC also supports to declare a variable name with its data type. Variable can be declared in two different methods in QBASIC: a. Implicit Declaration b. Explicit Declaration. Implicit declaration of variable The declaration of variable with assigning a value to a variable is called implicit declaration. The implicit declaration is declared with the variable suffix symbols (% ! $ &) according to the data types. Examples: • Here, N$ is a string variable and N! is a single precision variable which are declared implicitly because data is as- LET N$ = \"Biratnagar\" signed with the variable at the same time of declaration. LET N! = 50 Explicit declaration of variable The declaration of a variable without assigning the data to a variable is called an implicit variable. This explicit declaration is done at the beginning of the program which helps to reduce the repetition of a variable in the program. The DIM statement is used to declare the variable explicitly. Syntax: DIM variable name AS types of variable Example: • In this program, N is a string variable which is declared explicitly and W$ is also string variable which is declared DIM Name AS STRING implicitly. DIM MARKS AS INTEGER DIM A%,W$ Example: CLS DIM N AS STRING N= \"Ram\" PRINT N W$ = \"Kathmandu\" PRINT W$ END Smart Computer Science Book-9 211

ELEMENTARY DATA TYPES Data is a collection of raw information which is given to the computer for processing. QBASIC supports two types of data: i) String and ii) Numeric. String Type of Data The data which can not perform any mathematical operation is called string type of data. It may alpha character, number or special symbols. String variable is used to assign the string types of data. QBASIC supports two types of string type of data: i) Variable-length string ii) Fixed-length string Variable-length String: This is the common data type,It may include 0 to 32767 characters in QBASIC, which can be supplied from the keyboard or fixed in the program enclosed by inverted comma. Example • In this example, name and address are fixed in the program. School name will be entered from the N$=\"Lakpa Sherpa\" keyboard. A$=\"Boudha\" INPUT \"Enter your School Name:\"; N$ Fixed-length string: The fixed length string type of data has the length of characters fixed to store in a variable in the program. So, such type of data is declared with explicit declaration. Example • In this example, N is a string variable which holds only five characters because its length is fixed by 5. So, we DIM N AS STRING*5 get the output LAKPA and extra characters are lost. N = \"LAKPA SHERPA\" PRINT N Numeric type of data The data which are used to perform the mathematical operation is called numeric type of data. The numeric type of data may include all the positive and negative numbers. There are four types of numeric type of data: i) Integer ii) Long Integer iii) Single precision iv) Double precision i) Integer : All the negative and positive whole numbers are known as the integer number. The range of integer number is -32,768 to 32767. Each Integer number needs 2 bytes of memory space to store. The % symbol is used as a suffix of with variable name as a suffix, such as A%, X%, N%, etc. ii) Long Integer : A long integer is a data type in QBASIC whose range is greater 212 Programming in QBASIC

than the standard data type integer. It is also called long range integer data type which represents all the positive and negative whole number. The range of long integer is -2,147,483,648 to 2,147,483,647. It needs four bytes memory space to store each Long Integer data type. The & symbol is used as a suffix with variable name, such as A&, X&, N&, etc. Single Precision : Commonly the number which comes with the fraction is called precision data type. Single precision number. Up to 7 digits any negative or positive with a decimal point number is known as the single precision number. The range of long integer is -3.37*10^38 to 3.37*10^38. It needs four bytes memory space to store each single precision data. Exclamation (! ) symbol is used as a suffix with variable name, such as A!, X!, N!, etc. It is not necessary to write the suffix for the single precision data type because QBASIC automatically treats the single precision data if suffix is not used. Example: N!=39.2932 Double Precision: The double precision data type is long range of precision data. It can hold up to 15 digits accurately. The range of long integer is -1.67*10^308 to -1.67*10^308 . It needs 8 bytes memory space to store each double precision data. Hash (#) symbol is used as a suffix with variable name, such as A#, X#, N#, etc. Example: N#= 983354288.1878 Consideration for writing the numeric Data • Comma is not used in a number digits. Comma represents separate data. • The number which comes without a sign that presents positive number. • We must mention the negative sign with negative number. • Double precision numeric data scientific notation is D and single precision scientific notation is E. Keywords Keywords are those words which have special meanings in QBASIC. Keywords are formed by using the characters of QBASIC Characters Set. Keywords are statements, commands and functions (built in functions) . The keywords are also called Reserved Words. Some reserved words are CLS, REM, INPUT, LET, PRINT, FOR, DO, SELECT, MID$, ASC, SQR, LEN, LEFT$, TIME$ and INT. Smart Computer Science Book-9 213

Points to Know • QBASIC is a high level programming language. It is used to develop application programs. • Actual program is written in the view window of the QBASIC. • Immediate window is used to run the single instruction. • Coding is the process of writing programs using commands to solve a problem. • Characters or symbols which are accepted by QBASIC are called character set. • The elements of QBASIC consist of the character set, constants, variables, opera- tors and the statements. • Constant refers to the data item whose values do not change during the execution of the program. There are two types of constant string and numeric constant. • Variable is a character or group of character which represents reference or name to any data for the computer memory. • The value of a variable may be changed during the execution of the program. • Variable is classified into two common groups string and numeric variable. • Numeric variables are four types which are Integer Variable, Long Integer Variable, Single Precision Variable, Double Precision Variable. • Variable can be declared in two different methods in QBASIC: a. Implicit Declaration b. Explicit Declaration. • QBASIC supports two types of data: i) String and ii) Numeric • There are four types of numeric type of data: i) Integer ii) Long Integer iii) Single precision iv) Double precision • Keywords are statements, commands and functions (built in functions) . The key- words are also called Reserved Words. Terms to Know Variable : Reference or name to any data for the computer memory. Constant : Data items whose values do not change during the execution of the program. String : A data type used in programming to represent text rather than num- bers. Precision : Refers to the closeness of two or more measurements to each other. Integer : Number which may negative or positive whole number. 214 Programming in QBASIC

Worksheet Objective Questions 1. Fill in the blanks: a. QBASIC is a ................................. level programming language. b. Actual program is written in............................. window. c. ................................ is the process of writing program. d. .............................. is used to keep continue the previous run command. e. QBASIC was released in........................ f. ............................... constant is quoted with quotation mark. g. ................................. is a name to any data for the computer memory. h. .......................... variable is always followed by dollar sign. i. Double precision gives up to ............................ digits accuracy. j. Single precision gives up to .............................. digits accuracy. k. Variable name may be up to ............................... characters long. 2. State whether the following statements are true of false: a. Computer can perform the task itself. b. Immediate window is used to run multiple instructions at a time. c. Constant value does not change during the program execution. d. 1, 000 is a valid numeric constant. e. Blank space is not allowed in numeric constant. f. Value of a variable may be changed during the program execution. g. Precession variable does not store the fractional value. h. Integer is a default variable. i. String variable is always followed by dollar sign. Smart Computer Science Book-9 215

3. Match the Following a. Single precession % b. Double precession F5 c. Long integer ! d. Integer $ e. String Shift +F5 f. Keep continue previous run command # g. Run from initial phase & Descriptive Questions 1. Answer the following questions: a. What is QBASIC ? b. Write any four features of QBASIC. c. Differentiate between view window and immediate window. d. What do you mean by coding? e. Write the name of common type of QBASIC elements. f. What does character set include? g. What do you mean by constant? h. Write the rules for numeric constant. i. Write the rules for string constant. j. What do you mean by variable? k. Write the rules for naming a variable. l. What is numeric variable? Write the types of numeric variable. m. What do you mean by precession variable? Write their types. n. What do you mean by integer variable? 216 Programming in QBASIC

CHAPTER 11Operator, Operand & Expression Chapter Includes • Introduction of operators • Types of Operators • Hierarchy of Arithmetic Operator • Introduction of Expression and its types • Hierarchy of Operation OPERATORS Operators are special symbols which come with expression to perform the operation during the program execution. Such as mathematical operation, logical operation, string operation and relational operation. QBASIC supports four types of operators: a. Arithmetic Operators b. Relational Operators c. Logical Operators d. String Operators Arithmetic Operators The symbols which are used to perform the mathematical operation is called arithmetic operators. Arithmetic operators always operate on numeric constant and variable. The result prepared by arithmetic operators will be displayed on the screen or assigned in the variable. QBASIC supports different types of arithmetic operators, shown in the table. Operators Function Example + Addition A+B - Subtraction A-20 * Multiplication 20*5 / Real division A/B \\ Integer Division A\\B ^ Exponential A^2 MOD Modulo Arithmetic (Return remainder of division calculation.) A MOD B Hierarchy of Arithmetic Operator If arithmetic expression contains more than one operator, then operators are operated one after another in the basis of their priority. The order of operational priority is called hierarchy of operation. The hierarchy table is shown as follows: Smart Computer Science Book-9 217

Arithmetic Operators Symbol Priority Exponential ^ 1st Multiplication and division *and / 2nd Integer division / 3rd Modulo Arithmetic MOD 4th Addition and subtraction + and - 5th Example: 10 + 20 / 4 * 5 - 2 ^ 2 Priority Operation Output 1st 2^2 10+20/4*5-4 2nd 20/4 10+5*5-4 3rd 5*5 10+25-4 4th 10+25 35-4 5th 35-4 31 (Final Result) Trick & TIPS • If expression is inside a bracket ( ), then it is operated first. If an expression contains the same order of priority of operators, the operations are done from left to right. Lab Activities 1. Class IX students have been assigned computer lab work to type the following expression in immediate mode of QBASIC in the computer and note down the result. Expression Result Expression Result PRINT 7+10*5 PRINT (5+10 *15/3) PRINT 5^3+4*2 PRINT (10+2)^2 PRINT 10 MOD 3 + 5 PRINT (4+2)/(2+1) PRINT 9/6\\4 PRINT 10 MOD 3 + 5^ 218 Operator, Operand & Expression

Relational Operators The symbols which are used to compare two values is called relational operators. Relational operators are used in logical expression which returns the value true (-1) or false (0) called Boolean value. QBASIC supports the following relational operators: Operators Name of Operator Example > Greater A>B < Smaller A<B >= Greater than or Equal to A>=B <= Smaller than or Equal to A<=B = Equal A=B <> Not equal to A <> B Lab Activities 1. Class IX students have been assigned computer lab work to type the following expression in immediate mode of QBASIC in the computer and note down the result. Expression Result Expression Result PRINT 9>7 PRINT 2=3 PRINT 5>3 PRINT 5<>4 PRINT 6=6 6>=8 PRINT 4<>4 PRINT 50<80 Logical Operators Logical operators are used to combine two or more relational expressions or conditions and returns single a value true (-1) or false (0). So, Logical operators are also called conjunction operators. QBASIC supports different types of logical operators. Some of the common operators are: i). AND ii) OR iii) NOT Smart Computer Science Book-9 219

AND operator AND is a logical operator which returns true value (-1), if all the conditions are satisfied, otherwise returns false value ( 0 ). Syntax: first condition AND Second Condition ……….. Truth Table of AND operator Condition AND operator Condition Result TRUE (-1) AND TRUE (-1) TRUE (-1) TRUE (-1) AND FALSE ( 0 ) FALSE ( 0 ) FALSE ( 0 ) AND TRUE (-1) FALSE ( 0 ) FALSE ( 0 ) AND FALSE ( 0 ) FALSE ( 0 ) Examples: PRINT 10>5 AND 10> 8 It returns true value (-1) because all the conditions are satisfied PRINT 10>5 AND 10>20 It returns false value ( 0 ) because only first condition is satisfied but second condition is not satisfied. OR operator OR is a logical operator returns true values (-), if any one or all the conditions are satisfied otherwise returns false values (0) Syntax: first condition OR Second Condition ……….. The truth table of OR operator Condition AND operator Condition Result TRUE (-1) OR TRUE (-1) TRUE (-1) TRUE (-1) OR FALSE ( 0 ) TRUE (-1 ) FALSE ( 0 ) OR TRUE (-1) TRUE ( -1 ) FALSE ( 0 ) OR FALSE ( 0 ) FALSE (-1 ) Examples: PRINT 10>5 OR 10> 8 It returns true value (-1) because both conditions are satisfied PRINT 10>5 AND 10>20 It returns false value (-1 ) because first condition is satisfied but second condition is not satis- fied. PRINT 10>5 OR 10>8 It returns false value (0), because both conditions are not satisfied. 220 Operator, Operand & Expression

NOT operator NOT is a logical operator which operates only one condition. It logically reverses an outcome making a true condition false or a false condition true. Syntax : NOT (condition) Examples: PRINT NOT (20>30) It returns true value (-1) because condition is not satisfied, but NOT operator reverses and returns the result. PRINT NOT (30>20) It returns the false value ( 0 ) because condition is not satisfied, but NOT operator reverses and returns the result. The truth table of NOT operator Condition Result TRUE (-1) FALSE ( 0 ) FALSE (0) TRUE (-1) Lab Activities 1. Anamika Yadav and her friends have been assigned a practical work to test the following conditions in the immediate window and note down the output result in the output column. Expression Output Expression PRINT 2>4 AND 4>2 PRINT NOT (4>2) PRINT 2>4 OR 4>2 PRINT NOT(2>4 OR 2>5) PRINT 4>1 AND 4>2 PRINT NOT(2>4 OR 5>3) PRINT 2>1 AND 1>2 PRINT NOT(2>4 AND 2>5) PRINT 2>4 OR 1>2 PRINT NOT(2>4 AND 5>3) PRINT 2>1 OR 1>2 PRINT NOT(2>4) PRINT NOT (4>2) PRINT 7<10 or 10>5 Smart Computer Science Book-9 221

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” Operator It joins two strings and gives the output: MatideviKathmandu. Operand S=A + 20 Operands are the string or numeric values comes with the expression. It may be constant or variable on which mathematical, Operand logical or string operation take place. EXPRESSION An expression can be a valid single or combination of elements such as variable ,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 expressions. QBASIC supports three types of expressions: i) Arithmetic expression ii) Logical expression iii) 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 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 algebraic expression directly. It needs to change algebraic expression to QBASIC expression to perform the operation. Some of the common algebraic expression and their valid QBASIC expression are given as: 222 Operator, Operand & Expression

Algebraic Expression Valid QBASIC Expression A+B (A+B)/5 5 X^2+Y^ 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 the value of A and B. PRINT A<20 It returns the result true (-1) or false (0) which depends on the value of A. PRINT 10=10 It returns true (-1) because expression is true. PRINT 20 <> 10 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 RahulKhadka is assigned in N$ variable. PRINT “Rahul” + “Khadka” Smart Computer Science Book-9 223

It combines two string “Rahul” and “Khadka” returns the computational value RahulKhadka and displays on the screen. LET X$=A$+B$ It combines contain of A$ and B$ and assigns in X$ variable. Hierarchy of Operation There may be arithmetic, relational and logical operations in the relational or logical expression. If parentheses “( )” are present then the operations inside the parentheses are performed first. In case of absence of parentheses, arithmetic operations take place first whereas relational operations and secondly logical operations take place. The following table shows the hierarchy of operations: Type of Operations Operator Order Arithmetic Operators Relational Operators ^, *, /, \\, MOD, +, - First Logical =, <>, <, <=, >, >= Second NOT, AND, OR Third Example: (First arithmetic operations) (4 MOD 5) = 1 AND (9 + 2) = 12 AND 8<>7 (Second relational operations) = 1 = 1 AND 11 = 12 AND 8 <> 7 (Third logical operations) = -1 AND 0 AND –1 =0 Lab Activities 1. Type the following expression in immediate mode and note down the result. 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” 224 Operator, Operand & Expression

Points to Know • Operators are special symbols which performs the operation during the program execu- tion. • QBASIC supports four types of operators: Arithmetic, Relational, Logical and String op- erators. • 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 conditions 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, Logical 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 opera- tion. 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. Smart Computer Science Book-9 225

Worksheet Objective Questions 1. Fill in the blanks: a. ............................ perform the operation during the program execution. b. ............................. operators perform mathematical operations. 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 an expression. h. The value returned by logical expression is called ................................ value. 2. State whether the following statements are true of false: a. Computer can perform the task itself. b. Operators always come with expression. c. Result prepared by operators will be displayed or assigned in variable. d. MOD is a logical operator. e. If the expression contains the same order of priority of operators the opera- tions 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 come with the logical expression. i. Logical expression always returns the true (-1). j. Logical operators are also called conjunction operators. 226 Operator, Operand & Expression

k. AND operator returns true (-1) when all the conditions are satisfied. l. OR operator 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 the 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 2. Sundar Budhathoki has been assigned a task to form expression to solve 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 the 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-9 227

Q-basic Statements CHAPTER 12 Chapter Includes • Introduction of statements • Types of statements • Assignment Statement • Input/output statement • Common Statements 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 statement. Statements are formed with reserved word and expression. The rules followed by statement is known as syntax. A reserved word is also known as keyword which has specific meaning to perform the task for interpreter of QBASIC. Statement of QBASIC is classified into different types some of common types 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 assigned 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 228 Q-basic Statements

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 or string to the variable read with READ statement. Example : CLS • Here, First data is supplied in READ N$, AD$, PH first variable, second date is PRINT N$, AD$, PH supplied in second variable DATA “Rohan”, “Katari-4”, 035-4430914 and third data is supplied in END 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 • In this example, before using A=20 the SWAP statement, it gives B=30 the output -10 but after using PRINT A-B the SWAP statement, it gives SWAP A,B output 10 because value of A PRINT A-B & B variable is interchanged. END 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 different 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 Smart Computer Science Book-9 229

INPUT statement Function : Its 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 halt 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 a 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 halt 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 spaces, commas, semicolon, slashes, etc. 230 Q-basic Statements

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 for PRINT. OTHER COMMON STATEMENTS REM statement Function : It is a declaration statement; also called non executable statement because 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 Example : REM This is my first QBASIC program ' This program calculates simple interest • Apostrophe symbol ‘ stands for REM, in place of REM you can use ‘ apostrophe symbol. Smart Computer Science Book-9 231

END statement Function : It is used to end a BASIC program, procedure or block. Syntax : END [FUNCTION | IF | SUB | SELECT] Example : END END IF END SELECT END SUB END FUNCTION SWAP statement Function : It is used to exchange the value of two variables or element of array variable. Syntax : SWAP varable1, variable2 Example : CLS A =10 B=5 PRINT A-B SWAP A, B PRINT A-B END • Here, the output before using SWAP statement is 5 and after using SWAP statement is -5 because it exchanges the value of variable. Sample Programs INPUT A 1. Write a program to ask any two INPUT B numbers and find their sum with and without INPUT and PRINT prompt. S=A+B REM : With INPUT & PRINT prompt PRINT S CLS INPUT \"Enter first number:\";A END INPUT \"Enter second number:\";B 2. Write a program to ask any two num- S=A+B PRINT \"Sum=\"; S bers and fin their product. Use Single END INPUT to receive tow number and each INPUT to each number. REM : Without INPUT & PRINT prompt CLS REM : With single INPUT with Prompt CLS INPUT \"Enter two number:\";A P=A*B PRINT \"Product=\";P END 232 Q-basic Statements

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 breadth of a rectangle and find out its area. Hint-: A=L*B. 4. Write a program to ask two numbers ……………………………………………… and find their sum, product, difference ……………………………………………… and product. ……………………………………………… CLS ……………………………………………… INPUT \"Enter two numbers:\"; A, B ………………………................………….. S=A+B 3. Write a program to ask quantity and D=A-B P=A*B rate of pen and find out total amount. PRINT \"Sum=\"; S PRINT \"Difference=\"; D ……………………………………………… PRINT \"Product=\"; P ……………………………………………… ……………………………………………… END ……………………………………………… …………………………………................. 5. Write a program to ask principal 4. Write a program to ask three numbers amount, time and rate and find the simple interest. SI= (P*T*R)/100. and find their average and sum. CLS INPUT \"Enter principal Amount:\"; P ……………………………………………… INPUT \"Enter Time:\"; T ……………………………………………… INPUT \"Enter Rate:\", R ……………………………………………… SI = (P * T * R) / 100 PRINT \"Principal Amount:\"; SI END ……………………………………………… Test Your Skill …………………………………................. ................................................................ 1. Write a program to ask a number and ................................................................ display it's square value. Smart Computer Science Book-9 233

................................................................ ……………………………………………… ................................................................ ……………………………………………… ……………………………………………… 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. …………………………………................. ................................................................ ……………………………………………… ................................................................ ................................................................ ……………………………………………… 6. Write a program to ask a number and ……………………………………………… divide by 2 and print quotient. ……………………………………………… ………………………………….................. 10. Write a program to ask Nepali curren- cy and convert into Indian currency. Hint-: IC Rs. 1 = NC Rs. 1.60 ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................. …………………………………................. 7. Write a program to print the remainder 11. Write a program to ask meter and of 5 divided by 2. Use READ---DATA. convert into kilo meter. ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................. …………………………………................. 8. Write a program to ask Meter and ............................................................... ............................................................... convert into Kilometer. 234 Q-basic Statements

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 types of statement 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 a 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 end 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. sentence. Smart Computer Science Book-9 235

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 whether 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 b. CLS READ A, B 1. Write the output of: R = A MOD B a. CLS Q=A/B Q1 = A \\ B LET A = 10 A=A+1 LET B = 20 B=B+1 LET S = A + B Q2 = A \\ B LET P = A * B PRINT R LET D = A - B PRINT Q PRINT S PRINT Q1 PRINT P PRINT Q2 PRINT D END 236 Q-basic Statements

DATA 18, 4 and expenditure of a person and find END out his saving. c. CLS 6. Write a program to ask unit consumed A = 10 of electricity and rate. Now, find out to- B = 20 tal amount to pay. D=A-B PRINT D SWAP A, B D=A-B PRINT D END 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 b. REM : Read the length and breath of rectangle and display area. READ LENGTH, BREADTH LET A=L*B DISPLAY \"Area=\";A DATA 4,3 END 3. Write a program to ask name, gender, age of a person and print. 4. Write a program to ask sales amount of sales man and calculate his 10% sales commission. 5. Write a program to ask yearly income Smart Computer Science Book-9 237

13Control Flow Statements CHAPTER Chapter Includes • Introduction of control flow statement • Types of control flow statements • IF statement • SELECT CASE statement • GOTO statement CONTROL FLOW STATEMENTS When you execute a program, it starts to execute from beginning to last one after another instruction 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 statements which are used to alter the execution flow or control the execution flow is called control statements. The QBASIC Supports the different types of Control 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 ........................ Next Statement END IF 238 Control Flow Statements

• Here, condition may be string or numeric expression and statement block is a series of instructions. Structure 2: Entry Point IF <condition> THEN <statement block> ELSE <statement> Is condition Yes Execute True block Or IF <condition> THEN True ? <Statement block> No ……………………. Execute False block ……………………. Exit Point ELSE <Statement block> Next Statement …………….......... ……………........... END IF • In this case if the condition is satisfied, 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-9 239

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 from IF-END IF block. If the condition1 is not satisfied, it tests the condition2. If the condi- tion2 is satisfied, it executes statement block2 and exit from the IF-END IF and so on. If all the conditions 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 : • When you execute the above program, it allows CLS you to enter your age and that is assigned in the INPUT \"Enter Your Age:\"; A A variable. If the value of A is smaller than 10, it IF A <= 10 THEN displays ‘You are child’ because the first condi- tion is satisfied. If the first condition is not satis- PRINT \"You are Child\" fied, it tests the second condition, if the value of ELSEIF A > 10 AND A <= 20 THEN A become more than 10 and less than or equal 20, it displays ‘You are Teenager’. If this condi- PRINT \"You are Teenager\" tion also not satisfied, it tests the next condition. ELSEIF A > 20 AND A <= 40 THEN If the value of A become more than 20 and less than or equal 40, it displays, ‘You are Young’. PRINT \"You are Young\" If this also not satisfied, it takes place after the ELSE ELSE and displays ‘You are Old’. PRINT \"You are Old\" END IF END 240 Control Flow Statements

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 IF A > B AND A < C OR A > C AND A < B THEN 1. Write a program to ask any two num- PRINT \"Middle is:\"; A bers and find out greater number. ELSEIF B > A AND B < C OR B > C CLS AND B < A THEN INPUT \"Enter first number:\"; A PRINT \"Middle is\"; B INPUT \"Enter second number:\"; B ELSE IF A > B THEN PRINT \"Middle is :\"; C PRINT \"Greater is :\"; A END IF ELSE PRINT \"Greater is :\"; B 4. Write a program to ask a number END IF and find out it is divisible by 3 or not. END CLS INPUT \"Enetr a number:\"; N 2. Write a program to ask any three num- R = N MOD 3 bers and find out greatest numbers. IF R = 0 THEN CLS PRINT \"It is visible\" INPUT \"Enter first number:\"; A ELSE INPUT \"Enter second number:\"; B PRINT \"It is not divisible\" INPUT \"Enter Third number:\"; C END IF IF A > B AND A > C THEN END PRINT \"Greatest is :\"; A ELSEIF B > A AND B > C THEN 5. Write a program to ask 5 subject PRINT \"Greatest is :\"; B marks and find out total and aver- ELSE age marks. The program should PRINT \"Greatest is :\"; C also find out you are passed or END IF failed. If your average marks is END above 40, you are passed other- wise failed. 3. Write a program to ask any three num- CLS bers and find out middle numbers. INPUT \"Enter 5 subject Marks:\"; A, CLS B, C, D, E INPUT \"Enter first number:\"; A T=A+B+C+D INPUT \"Enter second number:\"; B AV = T / 5 INPUT \"Enter Third number:\"; C IF AV > 40 THEN PRINT \"Your are passed\" Smart Computer Science Book-9 241

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. ................................................................ ……………………………………………… ................................................................ ……………………………………………… 4. Write a program to ask a number and ……………………………………………… find it is even or odd. ……………………………………………… ……………………………………………… ………………………………….................. ……………………………………………… ……………………………………………… ……………………………………………… …………………………………................... ……………………………………………… .................................................................. ………………………………….................. ................................................................. ……………………………………………… 2. Write a program to ask any three …………………………………................. numbers and find out smallest numbers. ................................................................ ……………………………………………… ................................................................ ……………………………………………… Sample Programs ……………………………………………… 1. Write a program to ask marks in com- ……………………………………………… puter and Maths and find out you are eligible to study computer or not. If ………………………………….................. your both subject marks is above 60, ……………………………………………… you are eligible to study computer in ………………………………….................. the next class, otherwise you are not eligible to study computer. 3. Write a program to ask your age and CLS find out you can vote in the national INPUT \"Enter computer Marks:\"; C election or not. If your age is above 18, INPUT \"enter marks of Math:\"; M you can vote. Otherwise you can not IF C > 60 AND M > 80 THEN vote. PRINT \"You are eligible.\" ……………………………………………… 242 Control Flow Statements

ELSE CLS PRINT \"You aren't eligible.\" INPUT \"Enter percentage mark:\"; P END IF IF P < 40 THEN END PRINT \"Failed\" ELSEIF P < 50 THEN 2. Write a program to ask marks in Math- PRINT \"Third division\" ematics and find out you are eligible to ELSEIF P < 60 THEN study optional mathematics or not . If PRINT \"Second division\" marks is not below 60, you are eligible, ELSEIF P < 80 THEN other wise you are not eligible. PRINT \"First Division\" ELSE CLS PRINT \"Distinction\" INPUT \"Enter Maths Marks:\"; M END IF IF NOT (M < 60) THEN END PRINT \"You are eligible\" ELSE PRINT \"You are not eligible\" 5. Write a program to calculate simple END IF interest ask principal amount and END time. The rate of interest is given be- 3. Write a program to ask 5 subject marks low: and find out you are failed or passed. If Time Rate any one subject marks is below 40, you are failed otherwise passed. 1 12% 2 13% CLS INPUT \"Enter 5 subject marks:\"; A, B, 3 15% C, D, E 4 18% IF A < 40 OR B < 40 OR C < 40 OR D < Above 4 20% 40 OR E < 40 THEN CLS INPUT \"Enter Principal Amount\"; P PRINT \"You are failed\" INPUT \"Enter Rate of Amount:\"; T IF T = 1 THEN R = 12 ELSE PRINT \"You are Passed\" IF T = 2 THEN R = 13 IF T = 3 THEN R = 15 END IF END IF T = 4 THEN R = 18 IF T > 4 THEN R = 20 4. Write a program to ask your percentage marks and find out your division. SI = (P * T * R) / 100 Percentage Division PRINT \"Simple Interest=\"; SI <40 Failed END <50 Third <60 Second Test Your Skill <80 First 1. Write a program to ask your marks of computer, maths and science and >=80 Distinction Smart Computer Science Book-9 243

find out you are eligible or not to study .............................................................. computer science in class IX. If any .............................................................. one subject marks is above 80, you are .............................................................. eligible otherwise you are not eligible. .............................................................. Ans:.......................................................... ............................................................. .................................................................. 3. Write a program to ask your nation- .................................................................. .................................................................. ality and age and find out you are .................................................................. eligible to get Nepali Citizenship or .................................................................. not. If you are NEPALI and age is .................................................................. above 18, you are eligible otherwise .................................................................. you are not eligible. ................................................................. Ans:...................................................... 2. Write a program to ask your gender .............................................................. .............................................................. and find out you are male, female or ............................................................. third gender. .............................................................. .............................................................. Ans:........................................................... ............................................................. .................................................................. ............................................................. .................................................................. .................................................................. ............................................................. .................................................................. .................................................................. SELECT CASE Statement Function : It is a control flow decision making statement or also called conditional 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 244 Control Flow Statements

Statement block2 • It is like as third structure of IF. It evalu- ates the test expression and returned …………………. value is compared with each CASE clause of expression list. It executes the …………………... block of statements; if the test result is matched with the any CASE clause and CASE Expression-list rest of other blocks are ignored. CASE ELSE is an optional clause. If all the test Statement block3 is not matched with any CASE expres- sion list, it executes CASE ELSE, if it is …………………. exist, otherwise it does not produce any action. …………………... [CASE ELSE Statement block4 …………………. …………………...] END SELECT 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 formats or a combination of them, separated by comma. Sample Programs 1. Write a program to ask your age and • In this program only one CASE clause is used. If the value of test expression find out you are old or not. If age is A is matched with the CASE clause, it displays the message ‘You are Old’ and above 60, you are old otherwise you execution keep continue after the END- SELECT. If the condition is not matched, are not old. it starts to execute the statements men- tioned after END-SELECT. CLS 2. Write a program to ask a number INPUT “Enter Your Age:”;A and find the even or odd. SELECT CASE A CLS INPUT “Enter a number:”;N CASE IS>60 PRINT “You are old” CASE ELSE PRINT \"You are not old\" END SELECT END Smart Computer Science Book-9 245

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

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 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 MONDAY and so ................................................................ on. Ans:........................................................... ................................................................ .................................................................. ................................................................ .................................................................. 3. Write a program to calculate simple interest the program should ask prin- .................................................................. cipal amount, time and the rate is de- .................................................................. fined in the basis of following condi- .................................................................. tions: .................................................................. Time (year) Rate .................................................................. 1 to 3 5% .................................................................. 8% .................................................................. 4 to 6 10% 15% 7 to 10 Above 10 .................................................................. Ans:........................................................ .................................................................. ................................................................ .................................................................. ................................................................ .................................................................. ................................................................ .................................................................. ................................................................ .................................................................. ................................................................ Smart Computer Science Book-9 247

.................................................................. Ans:....................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... 4. Write a program to ask destination to ............................................................... travel and number of passenger and ............................................................... find out total amount to pay. The rate ............................................................... per person is as: ............................................................... ............................................................... Destination Rate ............................................................... Pokhara 400 .............................................................. Janakpur 600 .............................................................. Biratnagar 1000 Butwal 500 .............................................................. 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 END GOTO 10 NED IF END • Here, Line number is written as GOTO • Here, label name is written as GOTO 10. It transfers the execution follow abc. It transfers the execution follow where is labeled abc. where is mentioned line 10. 248 Control Flow Statements

Test Your Skill P=A*B PRINT \"Difference=\";D 1. Write the output of following program: END CLS Output REM :Example of without condition A=20 B=10 S=A+B GOT 10 20 PRINT “Difference=”;D GOTO abc 10 PRINT “Sum=”;S D=A-B GOTO 20 : abc Points to Know • The program execution goes from beginning to last one after another instruction 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. Smart Computer Science Book-9 249

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. 5. Write a program to ask marks of the computer and find out you can study com- puter 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 250 Control Flow Statements


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