Approved by Curriculum Development Centre 151 Oasis Radiant Computer Science, Book 9 Brown #A52A2A rgb(165,42,42) White #FFFFFF rgb(255,255,255) Gray #808080 rgb(128,128,128) Black #000000 rgb(0,0,0) The illustration of CSS colours, which includes the above properties, is given below. Example <html> <head> <title>CSS hsl colour property</title> <style> h1{ text-align:center; } #rgb{ colour:rgb(255,0,0); } #rgba{ colour:rgba(255,0,0,0.5); } #hex{ colour:#EE82EE; } #short{ colour: #E8E; } #hsl{ colour:hsl(0,50%,50%); } #hsla{
Oasis Radiant Computer Science, Book 9 152 Approved by Curriculum Development Centre colour:hsla(0,50%,50%,0.5); } #built{ colour:green; } </style> </head> <body> <h1 id=”rgb”> Hello World. This is RGB format. </h1> <h1 id=”rgba”> Hello World. This is RGBA format. </h1> <h1 id=”hex”> Hello World. This is Hexadecimal format. </h1> <h1 id=”short”> Hello World. This is Short-hexadecimal format. </h1> <h1 id=”hsl”> Hello World. This is HSL format. </h1> <h1 id=”hsla”> Hello World. This is HSLA format. </h1> <h1 id=”built”> Hello World. This is Built-in colour format. </h1> </body> </html>
Approved by Curriculum Development Centre 153 Oasis Radiant Computer Science, Book 9 CSS Font CSS Font property is used to control the look of texts. By the use of CSS font property you can change the text size, colour, style and so on. You have already studied how to make text bold or underlined. Here, you will also learn how to resize your font using percentage. These are some important font attributes: 1. CSS Font Colour: This property is used to change the colour of the text. (standalone attribute) 2. CSS Font Family: This property is used to change the face of the font. 3. CSS Font size: This property is used to increase or decrease the size of the font. 4. CSS Font Style: This property is used to make the font bold, italic or oblique. 5. CSS Font Variant: This property creates a small-cap effect. 6. CSS Font Weight: This property is used to increase or decrease the boldness and lightness of the font. CSS Font Colour CSS font colour is a standalone attribute in CSS although it seems that it is a part of CSS fonts. It is used to change the colour of the text. There are three different formats to define a colour: • By a colour name • By hexadecimal value • By RGB In the above example, we have defined all these formats. <!DOCTYPE html> <html> <head> <style> body { font-size: 100%; }
Oasis Radiant Computer Science, Book 9 154 Approved by Curriculum Development Centre h1 { colour: red; } h2 { colour: #9000A1; } p { colour:rgb(0, 220, 98); } } </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <p>This is a paragraph.</p> </body> </html> Output: This is heading 1 This is heading 2 This is a paragraph. 2. CSS Font Family CSS font family can be divided in two types: • Generic family: It includes Serif, Sans-serif, and Monospace. • Font family: It specifies the font family name like Arial, Times New Roman etc. Serif: Serif fonts include small lines at the end of characters. Example of Serif: Times New Roman, Georgia, etc. Sans-serif: A Sans-serif font doesn’t include the small lines at the end of characters. Example of Sans-serif: Arial, Verdana etc. <html> <head> <style> body { font-size: 100%; } h1 { font-family: sans-serif; } h2 { font-family: serif; } p { font-family: monospace; } } </style> </head>
Approved by Curriculum Development Centre 155 Oasis Radiant Computer Science, Book 9 <body> <h1>This heading is shown in Sans-serif.</h1> <h2>This heading is shown in Serif.</h2> <p>This paragraph is written in Monospace.</p> </body> </html> Output: • This heading is shown in Sans-serif. • This heading is shown in Serif. • This paragraph is written in Monospace. 3) CSS Font Size • CSS font size property is used to change the size of the font. • These are the possible values that can be used to set the font size: Font Size Value Description xx-small used to display the extremely small text size. x-small used to display the extra small text size. small used to display small text size. medium used to display medium text size. large used to display large text size. x-large used to display extra large text size. xx-large used to display extremely large text size. smaller used to display comparatively smaller text size. larger used to display comparatively larger text size. size in pixels or % used to set value in percentage or in pixels. <html> <head> <title>Practice CSS font-size property</title> </head>
Oasis Radiant Computer Science, Book 9 156 Approved by Curriculum Development Centre <body> <p style=”font-size:xx-small;”> This font size is extremely small.</p> <p style=”font-size:x-small;”> This font size is extra small</p> <p style=”fontsize:small;”> This font size is small</p> <p style=”font-size:medium;”> This font size is medium. </p> <p style=”fontsize:large;”> This font size is large. </p> <p style=”font-size:x-large;”> This font size is extra large. </p> <p style=”font-size:xx-large;”> This font size is extremely large. </p> <p style=”font-size:smaller;”> This font size is smaller. </p> <p style=”fontsize:larger;”> This font size is larger. </p> <p style=”font-size:200%;”> This font size is set on 200%. </p> <p style=”font-size:20px;”> This font size is 20 pixels. </p> </body> </html> CSS Margins CSS Margin CSS Margin property is used to define the space around elements. It is completely transparent and doesn’t have any background colour. It clears an area around the element. Top, bottom, left and right margin can be changed independently using separate properties. You can also change all properties at once by using shorthand margin property. There are following CSS margin properties: CSS Margin Properties Property Description margin This property is used to set all the properties in one declaration. margin-left It is used to set left margin of an element. margin-right It is used to set right margin of an element. margin-top It is used to set top margin of an element. margin-bottom It is used to set bottom margin of an element.
Approved by Curriculum Development Centre 157 Oasis Radiant Computer Science, Book 9 CSS Margin Values These are some possible values for margin property. Value Description auto This is used to let the browser calculate a margin. length It is used to specify a margin pt, px, cm, etc. Its default value is 0px. ^It is used to define a margin in percent of the width of containing element. inherit is used to inherit margin from parent element. Note: You can also use negative values to overlap content. CSS Margin Example You can define different margin for different sides for an element. <html> <head> <style> p { background-colour: pink; } p.ex { margin-top: 50px; margin-bottom: 50px; margin-right: 100px; margin-left: 100px; } </style> </head> <body> <p>This paragraph is not displayed with specified margin. </p> <p class=”ex”>This paragraph is displayed with specified margin.</p> </body> </html>
Oasis Radiant Computer Science, Book 9 158 Approved by Curriculum Development Centre Points to remember • CSS is a style sheet language which is used to describe the look and formatting of a document written in a markup language. • CSS selectors are used to select the content you want to style. • There are three ways to insert CSS in HTML documents. – Inline CSS – Internal CSS – External CSS • Inline CSS is used to apply CSS on a single line or element. • Internal CSS is used to apply CSS on a single document or page. It can affects all the elements of the page. • External CSS is used to apply CSS on multiple pages or all pages. • Here, we write all the CSS code in a css file. • CSS comments are generally written to explain your code. Exercises 1. Multiple choice questions: i) CSS stands for - a. Cascade style sheets b. Colour and style sheets c. Cascading style sheets d. None of the above ii) Which of the following is the correct syntax for referring the external style sheet? a. <style src = example.css> b. <style src = “example.css” > c. <stylesheet> example.css </stylesheet> d. <link rel=”stylesheet” type=”text/css” href=”example.css”> iii) The property in CSS used to change the background colour of an element is - a. bgcolour b. colour c. background-colour d. All of the above
Approved by Curriculum Development Centre 159 Oasis Radiant Computer Science, Book 9 iv) The property in CSS used to change the text colour of an element is - a. bgcolour b. colour c. background-colour d. All of the above v) The CSS property used to control the element’s font-size is - a. text-style b. text-size c. font-size d. None of the above vi) The HTML attribute used to define the internal stylesheet is - a. <style> b. style c. <link> d. <script> 2. Answer the following questions: 1. What is CSS? 2. How can you integrate CSS on a web page? 3. What are the advantages of CSS? 4. What are the limitations of CSS? 5. What is Embedded Style Sheet? 6. What are the advantages of Embedded Style Sheets? 7. What is a CSS selector? 8. What is the difference between class selectors and id selectors? 9. What are the advantages of External Style Sheets?
Oasis Radiant Computer Science, Book 9 160 Approved by Curriculum Development Centre Chapter 10 General Concept Of Programming ffl The concept of programming ffl The concept of algorithm and its application ffl The concept of flowchart and its application ffl The types of program logic THIS CHAPTER COVERS :
Approved by Curriculum Development Centre 161 Oasis Radiant Computer Science, Book 9 General Concept of 10 Programming Chapter Introduction Programming is the art of developing a programme. People who develop a computer programme are programmers. Programmers select a suitable programming language for software development. They develop the framework of the software before developing it with the help of programming language. Tools and techniques that represent the software framework which makes it easy to develop the software programmes are called programme development tools and techniques. All the activities of computer system are controlled and processed by data and instructions. These instructions are called commands. Collection of such instructions and the data in a systematic form to solve a problem is a software programme. To solve the problem using a software system, we have to make some systematic plan of action for software development. Such plan of action and the programme structure can be represented in a systematic order using different tools and techniques which are called programme presentation tools and techniques. Popular tools and techniques used to represent the programmes are flowchart and algorithm. Programming Language Language is a medium of communication. There are several languages developed in separate environments and geographical conditions. Nepali, Newari, Hindi, Maithili, Bhojpuri, Urdu, and English are examples of natural languages whereas BASIC, FORTRAN, COBOL, LISP, C, C++, C#, JAVA, etc. are artificial or fabricated languages. A computer language is a type of artificial language used to communicate with the computer. We know that a computer is an electronic device that works on ON and OFF (1 and 0) basis to read and write data. We use several languages to communicate with others, just similarly, we use many computer languages to communicate with computers e.g. BASIC, COBOL, PASCAL, FORTRAN, etc. A
Oasis Radiant Computer Science, Book 9 162 Approved by Curriculum Development Centre computer understands only the computer language written in binary or simple English followed by special syntax. Translators Translators are the programmes that translates a programme written in high level language or assembly level language into machine level language which can be understood by the computer. Some of the commonly used Translators are: 1. Assembler 2. Interpreter 3. Compiler 1. Assembler: An assembler is a system software that translates a programming language written in Assembly language into machine language and viceversa. It converts the whole programme at once. If some errors occur, they have to be corrected, debugged and assembled again. 2. Interpreter: Interpreter is a programme that translates a programme written in high level language into machine level language line by line. As it translates only one line at a time, it takes more time than a compiler does. 3. Compiler: It is also a language processor that translates a programme written in high level language into machine level language as a whole at once. It reads the whole programme at once, if it is bug free, it converts it into machine code at once. Compiler Interpreter i. It translates the entire programme at once. i. It translates one statement at a time. ii. It reports all errors at once. ii. It reports errors line by line. iii. It does not create object code. iii. It creates object code. iv. Debugging is complex. iv. Debugging is easier. v. Translation is not preserved. So, execution is fast. v. Translation is preserved. So, execution is slow. Types of Computer Languages The computer languages are classified into following types: • Machine level language
Approved by Curriculum Development Centre 163 Oasis Radiant Computer Science, Book 9 • Low level language • High level language • Advanced high level language Computer Language Machine Level Machine code Problem oriented 4 GL Procedure oriented 5 GL Assembly language Low Level High Level Advanced High Level Machine Level Language A computer dependent language written in a binary terms is called Machine Language. Machine code requires a high level of programming skill along with sound knowledge of computer architecture and functions. Instructions written in binary terms are immediately executable without the help of any language translators because computer understands the machine codes. Advantages • It is written on machine code so no needs to translate it into machine code for further execution. • It has faster execution than other languages. • Other translator programmes are not required. Disadvantage • It is difficult to understand and develop a programme using machine language. • It is a machine-oriented language. • The knowledge of computer internal architectures is essential for programme coding. • Coding in it is time consuming.
Oasis Radiant Computer Science, Book 9 164 Approved by Curriculum Development Centre Low Level Language: Assembly Language It is a low-level language in which some high level words are used with machine codes. These words are called mnemonics. Some mnemonics are given below: • ADD for Addition • SUB for Subtraction Assembly language requires ASSEMBLER to translate Assembly language code into Machine code. So, it is not immediately executable. It is difficult to develop the programme using this language. Advantages • Coding is faster than machine code language because mnemonics are used for programme coding. • Less time is consumed compared to machine code languages. • Debugging is easy. Disadvantages • It is a machine oriented language. • Good knowledge of machine architectures is required. • It is time consuming • Translator is used to translate programme into machine code. • It is not as fast as machine code language in case of execution. High Level Language High level language contains a set of instructions written in simple English. It is a set of some symbols, words and rules to instruct the machine. The language is oriented towards the problem and procedures. Programme written in high level language is called source programme. A language translator is used to translate programme coding into object code (Machine code). Advantages • Simple English is used for programme coding. • It is machine independent. • It is problem and procedure oriented. • The knowledge of computer architectures is not necessary. • It requires less time for programme coding.
Approved by Curriculum Development Centre 165 Oasis Radiant Computer Science, Book 9 • Programme can be debugged easily and programme maintenance is also easy. Disadvantages • Since it is written in user’s language it should be converted into machine code. So it requires a translator. • The execution of the programme is slower than machine code. • The conversion time is slower than Assembly language because it is entirely written in human language. Advanced High Level It is a high level language in which fewer instruction codes are used to accomplish a particular task. It is a non-procedural language. Major 4th generation languages are used to get information from files and databases. These are database oriented programming languages. Example: DBASE, FOXPRO, SQL, VB, etc. Advantages • Software developed using 4GL (Forth Generation Language) are user friendly. • It is very easy to develop a programme because there is no need to remember the commands and instructions for programme development and we can use drag and drop type command buttons to develop the programme. These buttons are available on the system that helps to develop the programme. Disadvantages • Programme runs slower because it is heavy in size and design. • It is required to devote longer time to convert the programme into machine code. • Huge sized powerful translators are required to convert the language. Algorithm It is the most popular programme presentation technique. Programmers use the natural language like English and write the whole procedures and steps to develop the software which is called algorithm development. Algorithm is defined as a set of rules that define how a particular problem can be solved in a finite number of steps. In other words, an algorithm is a step by step representation of instructions
Oasis Radiant Computer Science, Book 9 166 Approved by Curriculum Development Centre to solve the problem. An algorithm must be composed of a finite set of steps, each of which may require one or more operations. Flowchart A diagrammatic tool used to present the software system using standard symbols is called Flowchart. A flowchart is a pictorial representation of step by step solution of a problem. It is one of the oldest techniques to depict an algorithm and is an important tool in programming. A flowchart is independent of the programming language like the algorithm. A flowchart facilitates the computer programmer in the following ways: • Helps to understand the programme and logic flow more easily. • Gives a clear knowledge about the input, process, logic and the output of the programmes. • Helps to analyse and correct the errors of the programme. • Represents the entire parts of the programme so it is easy to standardise the software. The basic flowchart symbols are: Start / End Terminal Symbol Input/Output Data Input/Output symbol Process Processing Symbol Decision Decision Symbol
Approved by Curriculum Development Centre 167 Oasis Radiant Computer Science, Book 9 Connect Connector Symbol Data Flow Line Types of Flowchart According to use and application, flowcharts are of two types: system flowchart and programme flowchart. 1. Program Flowchart: To represent the programme, we have to use a standard diagram called the programme flowchart. It shows the flow of instructions to solve the problem. It is used to make a logical structure diagram of the programme. 2. System Flowchart: It represents the system and its components, information from one component to the other component. It describes the sequence of processes to perform specific work. It explains the total flow of data inside the system. Rules of developing a flowchart • Analyse the input, process, storage and output of the programme. • Use standard symbols and arrowheads in the direction of flow of data and instructions. • We should use unambiguous symbols which can be easily understood by other programmers. • There should be a list of activities inside each symbol. • It should express all sections like input, process, logic and output. • It should not be any programming language oriented. Advantages • It is an easy method of communication to represent the programme. • It serves as a guide for programme coding.
Oasis Radiant Computer Science, Book 9 168 Approved by Curriculum Development Centre • It is easier to understand than programme and algorithm. • It is programme independent so applicable for all types of programming languages. • It represents all the components of a programme like input, process, logic and output which makes it easy to develop the programme. Disadvantages • It cannot replace all types of logic used in the computer programme. • It takes a long time to represent all modules of computer programme. • It is not suitable and is complex for big programme. Programme Logic Development of a programme is the art of arranging the commands and statements to solve a problem. According to the use of the logic and the structure we have three types of logical constructs which are called the programme logic or structure. They are Sequence, Selection and Iteration. Sequence A sequence is a set of computer instructions which follow one another and are executed unconditionally. In sequencing, instructions are put in a predefined order and computer executes the instructions only after the completion of previous one. Statement 1 Statement 2 Statement 3 For example, a programme to generate sum and product of two numbers might be as follows: • Read the values for the variables, A and B. • Calculate, sum = A + B • Calculate, product = A * B • Display the sum and product.
Approved by Curriculum Development Centre 169 Oasis Radiant Computer Science, Book 9 Selection Selection is a set of computer instructions which are executed conditionally that is, they are executed based on the conditions that can be true or false when the programme is being executed and can do alternative things based on the truth of the “if” conditions as follows. It has a part of decision making statements. 1. if <condition> then execute the statement A (no else) 2. if <condition> then execute statement A else execute statement B 3. if <condition> then statement A else if <condition> then statement B else if <condition> then statement C ……. else statement N If condition? If condition Statement sequence 1 Statement sequence 2 Fig. Selection Iteration (Loop) Iteration is one in which a set of computer instructions are executed repeatedly and conditionally that is, the loop statements are driven by the loop condition. Iteration is also called looping. Some examples of iteration (looping) in programming are as follows: Condition Statement 1 Fig. While Statement True False
Oasis Radiant Computer Science, Book 9 170 Approved by Curriculum Development Centre Statement 1 Condition Fig. Do While Statement True False Initialisation Condition Statement 1 Increment/ Decrement Fig. For Statement True False Procedures and Programme Procedure is a mechanism of doing or performing a job or task in a well suited manner or in a correct way. So, in computer programming also, a procedure is a way of defining, calling and handling various functions in an effective manner to make a programme short and efficient. A programme is a set of computer instructions executed in such a manner that after the complete execution of instructions it results in completion of a task or job. A programme may consist of a number of procedures to complete the task or solve the problem. For example, if a calculator is a programme then addition is one of its functions and the way of defining and calling this function is a procedure. Some Examples of Algorithm and Flowchart 1. Draw flow chart to find the area of a rectangle. Algorithm Step 1: Start programme Step 2: Read L and B Step 3: Calculate Area = L × B L, B Start End A A = L*B Flowchart
Approved by Curriculum Development Centre 171 Oasis Radiant Computer Science, Book 9 Step 4: Prime result area Step 5: End the programme 2. Draw a flowchart to find simple interest and total amount. Algorithm Step 1: Start programme Step 2: Read P, T, R Step 3: Calculate Interest I = P*T*R/100 Step 4: Calculate amount A = P+I Step 5: Print I, A Step 6: End the programme 3. An algorithm and a flowchart to generate sum of first N natural numbers. Algorithm Step 1: Start. Step 2: Read an integer value. i.e N Step 3: Assign, I = 1. Step 4: Calculate, Sum = Sum + I. Increase I by 1. Step 5: If I <= N then go to Step 4. Else go to Step 6. Step 6: Display Sum. Step 7: End. P,T,R Start End I, A A = P + I I = P*T*R/100 Flowchart Read the value for N Start Display Sum Yes End No Sum = Sum + i is i<=N i = 0 Flowchart
Oasis Radiant Computer Science, Book 9 172 Approved by Curriculum Development Centre 4. An algorithm and a flowchart to determine whether a given number is odd or even Algorithm Step 1: Start. Step 2: Read a number n. Step 3: Calculate, the remainder of n divided by 2 (i.e. n/2) that is rem = n%2. Step 4: If the rem = 0 then display n is even number and go to end. Else go to Step 5. Step 5: Display n is odd number. Step 6: End. Flowchart Read the value for n Start Display ‘n is odd number’ Display ‘n is even number Yes End No is rem =0 Calculate, rem = n%2 Flowchart 5. Algorithm and a flowchart to determine the largest of three given numbers Algorithm Step 1: Start.
Approved by Curriculum Development Centre 173 Oasis Radiant Computer Science, Book 9 Step 2: Read three numbers and store them as x, y, z. Step 3: Compare x and y. If x is greater than y, then go to step 6 else go to step 4. Step 4: Compare y and z. If y is greater than z, then print “y is the largest” and go to step 8, otherwise go to step 5. Step 5: Print “z is the largest” and go to step 8. Step 6: Compare x and z. If x is greater than z, then print “x is the largest” and go to step 7. Step 7: Print “z is the largest” and go to step 8. Step 8: End Flowchart Start End Yes Yes Yes No No No is x>y is x>z is y>z Read x, y, z Display ‘x is the largest number’ Display ‘y is the largest number’ Display ‘z is the largest number’
Oasis Radiant Computer Science, Book 9 174 Approved by Curriculum Development Centre Points to remember • A computer system needs instructions to perform different tasks. These instructions are called commands. • A programme is a collection of commands given in a systematic order to perform some work. • Programming is the technique or art to develop a programme. • Programmers are people who develop programmes using standard programming languages. • An algorithm is a step by step description of a programme in a systematic order. • A flowchart is a diagrammatic representation of a programme using standard symbols. • There are two types of flowchart: system flowchart and program flowchart. • The modules of programme are the functional procedures. • Programme design has three structures called sequence, selection and iteration (Loop). Exercises 1. Answer the following questions. a. What is a command? b. What is a programme? c. What is an algorithm? d. Write the characteristics of a good algorithm. e. What is a flowchart? f. Write some advantages of a flowchart. g. Explain the rules for designing a flowchart. h. What are the limitations of the flowchart? i. Draw the symbols of a flowchart.
Approved by Curriculum Development Centre 175 Oasis Radiant Computer Science, Book 9 2. Draw a flowchart and write the algorithm. a. Calculate simple interest. b. To find the smaller of two given numbers. c. To prepare a cup of tea. d. To cross a road. 3. Differentiate between: a. Programme and Command. b. System flowchart and program flowchart. c. Algorithm and flowchart. d. Loop and ordinary programme. e. Input and output of programme. 4. Fill in the blanks a. A computer needs ................................. to do some task. b. An ............................... is step by step approach of programme. c. Flowchart is ..................... representation of programme. d. Decision making symbol ...................... is used to process expressions. e. Symbol defines the process .................... used in programme. 5. State True and False a. Flowchart represents programme using language. b. Algorithm is similar to steps written in English to develop programme. c. Looping structure repeats the logic according to criteria given in programme. d. Processing symbol accepts input. e. Input and output symbol is same in flowchart. 6. Class Activity and Project Work a. Draw a flowchart to explain how you reach school from your house. b. Write an algorithm to explain how your teacher instructs you in class.
Oasis Radiant Computer Science, Book 9 176 Approved by Curriculum Development Centre Chapter 11 Introduction to QBASIC Programming ffl The programming basic ffl Programme development tools and methods in QBASIC ffl Basic programme codes and statements ffl The logic with various examples THIS CHAPTER COVERS :
Approved by Curriculum Development Centre 177 Oasis Radiant Computer Science, Book 9 Introduction to QBASIC 11 Programming Chapter Introduction QBASIC is the most popular high level programming language. Various versions of BASIC have been developed by Microsoft Company. This language is quite simple to understand and has been adopted by most of the microcomputers. It is suitable for both mathematical and business problems. It is compatible with MSDOS environment and it has two basic files QBASIC.EXE and QBASIC.HLP. We can edit, debug and execute the programme using these two files. The advantages of QBASIC 1. QBASIC is easy to learn and fun to practise. It may be called a `People’s language’. 2. It is available in almost every computer from micro to mainframe. Therefore, a programme developed in a micro computer can run on bigger systems with minor modifications. 3. It is suitable for mathematical and business applications. 4. Programme development cycle is quick and debugging is simple 5. Modification of programmes is quite easy. Starting QBASIC It occupies very low memory so it is possible to run the system using floppy disk also. It is compatible with MSDOS and has only two files QBASIC.EXE and QBASIC. HLP. Steps 6. Get MSDOS System and MSDOS Prompt C:\> 7. Change the Directory C:\> CD QBASIC C:\QBASIC> 8. Type QBASIC and press Enter C:\QBASIC>QBASIC enter
Oasis Radiant Computer Science, Book 9 178 Approved by Curriculum Development Centre When you are working with Windows system, Open QBASIC Folder on Windows. Double click over the QBASIC.EXE icon. You will get the QBASIC System as below. Press ESC key and get the programming window of the QBASIC and type the programme on the working window of QBASIC. Using the QBASIC Editor Menu and Commands. To activate QBASIC menus and command, simply press the highlighted letter on keyboard after pressing ALT key or click a menu or command with the left mouse button. To cancel command, simply press ESC key or click the mouse any where outside the menu. QBASIC menus display the sub-menus. Running the Program In order to see the output after writing a program, we should run a program. To run a program, we should follow the following steps. 1. Click on Run menu or press ALT+ R. 2. Select START option. OR Press Shift + F5 key from the keyboard.
Approved by Curriculum Development Centre 179 Oasis Radiant Computer Science, Book 9 Saving Program. 1. Click on File menu or press ALT+F. 2. Select Save or Save as option. 3. Type filename. 4. Click on Ok. Opening Program. 1. Click on File menu or press ALT+F. 2. Select Open option. 3. Type or select filename. 4. Click on Open. QBasic Interface (Screen, menus, shortcut commands) i) Screen:-QBASIC screen is divided into two windows, they are 1. View Window. 2. Immediate Window 1. View Window The largest portion of the QBASIC editor screen is called view window or program window. Statements of program are stored in memory andthen executed (run) by pressing Shif+F5 key. Programs are usually written and stored in this mode. 2. Immediate Window The wide rectangular area just above the reference bar is called immediate window. In this mode, lines of statements are written and run (executed) directly by pressing Enter key. it is useful to find out the instant result of QBASIC statements or commands. ii) Menus :- The Menu Bar consists of list of commands like File, View, Search, Run, Debug, Options and Help. These menus again have some sub commands such as FILE = New, Open, Save, Save As, Print, Exit. EDIT = Cut, Copy, Paste, Clear, New Sub, New Function. VIEW = Subs, Spilt, Output Screen.
Oasis Radiant Computer Science, Book 9 180 Approved by Curriculum Development Centre SEARCH = Find, Repeat Last Find, Change. RUN = Start, Restart, Continue. DEBUG = Step, Procedure Step, Trace On, Toggle Breakpoint, Clear All Breakpoints, Set Next Statement. OPTIONS= Display, Help Path, Syntax Checking. Structure of Basic To construct the QBASIC programme we have to arrange some standard elements. The elements are • Character set • Variables & constants • Arithmetic expressions • Relational or logical expressions • Keywords • Syntax • Library functions The Character Set QBASIC has the character set consisting of the following elements: a. Alphabet: A, B, C, ----, Z b. Digits: 0,1,2, ----, 9 and c. Special characters: + - * / ( ) . , $ ; : = > < “ ^ The symbol ^ (caret) is used to denote exponentiation operator, the symbol * (asterisk) is used to denote multiplication and the other symbols have their usual meanings. Constants and Variables A quantity in a computer programme which does not change its value during the execution of the programme is called a constant and the quantity which may change its values during the execution of the programme is called a variable. QBASIC allows the following constants: • Numeric constant • String constant
Approved by Curriculum Development Centre 181 Oasis Radiant Computer Science, Book 9 Numeric constant is one that is formed by a sequence of digits 0,1,2, ---, 9 and may include a decimal point. A numeric constant known as number may be an integer or a real number. For example, 383, + 57, 0, - 6.2 and 6.15E4 are valid numeric constants. The number 6.15E4, in fact, represents 6.15 ´ 104. The notation E is used to represent exponential form. The number after E is the exponent which can be positive or negative. However, its length cannot exceed two digits. It is also important to keep in mind that QBASIC does not distinguish between an integer and a fraction. Commands are not allowed in a numeric constant. The limit on the number of digits that can be used varies from computer to computer. Normally, a numeric constant can have up to a maximum of eight digits. A string constant consists of a sequence of characters. It must be enclosed by a quotation mark. This may contain a blank space as a character but it should not include the quotation mark. String constants are used to represent non-numeric quantities such as names, addresses, etc. For example, “SUYASH ADHIKARY”, “SUM = Rs 75”, “162” are a few valid string constants. In QBASIC, variables are also of two types. They are: • Numeric variable • String variable Numeric variables can assume numeric values and are represented by an alphabet or an alphabet followed by another alphabet or a digit. For example, A, C, A2, ABC, A6, etc. represent numeric variables. A string variable is represented by an alphabet followed by dollar ($) sign. It should be kept in mind that while constructing the string variable, dollar ($) should be the last character. For example, B1$, NAME$, BOOK1$, etc. are valid string variables. Arithmetic Expressions A QBASIC system can handle arithmetic expressions involving the five arithmetic operators + (addition), - (subtraction), *(multiplication), /(division) and ^ (exponentiation). The hierarchy of operations is as follows: • Exponentiation
Oasis Radiant Computer Science, Book 9 182 Approved by Curriculum Development Centre • Multiplication and division • Addition and subtraction Thus, in a particular arithmetic expression, the order of execution is as per this hierarchy, i.e. all exponentiation operations are performed first, and then multiplication/division and the addition/subtraction operations are the last to be carried out. Note that within a particular hierarchical group, the operations are executed form left to right. Normal hierarchy of operations can be altered by use of parentheses. The operations within the innermost parentheses are performed first and then the second innermost and so on. In addition to this hierarchy of operations, the following rules must be kept in mind in an arithmetic expression: • Two operations must not appear together. For example, C+-D, A/-C, etc are not permitted. • String constants and string variables should not be used in arithmetic expressions. For example, P+P$ is wrong. • When brackets are used, they must be used in pairs, i.e., every left bracket must be matched with a right bracket. • Denominator of an expression should not be zero. • Within a given pair of parentheses, the natural hierarchy of operations will apply. Let us take an example where we give QBASIC equivalents of a few algebraic expressions: Algebraic Expression QBASIC Equivalent 2A+B 2*A+B A + BC + D (A+B)/ (C-D) A(B+C) A*(B+C) B2 -4AC B^2-4*A*C
Approved by Curriculum Development Centre 183 Oasis Radiant Computer Science, Book 9 Relational or Logical Expressions A relational expression is formed by using any of the following relational operators: Relational Operator Meaning = Equal to > Greater than < Less than < = Less than or equal to > = Greater than or equal to < > Not equal to In the execution of programmes, it is sometimes desired to compare two numerical quantities (or sometimes string quantities) and take decisions on achieving certain conditions. For example, we may be interested to check the number of repetitive calculations performed or to find out whether the denominator of an arithmetic expression has become zero or if a particular quantity is negative, and so on. Expressions written to compare two quantities using certain relational operators are known as relational expressions. These expressions take only one of the two values, namely, TRUE or FALSE For instance, the relational expression A > B will be true if A is greater than B, otherwise FALSE. This test result is used to change the sequence of execution of statements in a programme. The general form of a relational expression is as follows: Constant or Variable Constant or Variable Relational operator When expressions are used on either side of the relational operators, the expressions will be evaluated first and then the results of expressions compared. This means that relational operators come last in the hierarchy of operators. Logical expressions are used in IF---THEN statements to determine the course of action of a running programme.
Oasis Radiant Computer Science, Book 9 184 Approved by Curriculum Development Centre Operators Operators are symbols or signs used to perform different operations between two or more operands and give results. There are four types of operators: 1. Arithmetical operator 2. Relational operator 3. Logical operator 4. String operator 1. Arithmetical operators These operators are used to perform arithmetic calculations. Arithmetical operators are given below: Symbols Meaning Example Output + Addition 9+6 15 - Subtraction 20-6 14 * Multiplication 5*5 25 / Division 45/9 5 ^ Exponential 3^3 27 MOD Modulus division (gives remainder) 9MOD4 1 2. Relational operators Relational operators are used to compare two data values. Relational operators are given below: Symbols Meaning Example = Equal to 5=5 > Greater than 6>4 < Less than 10<6 >= Greater than or equal to 7>=5 <= Less than or equal to 6<=6 <> Not equal to 7<>9 3. Logical operators Logical operators are used to combine two or more expressions containing relational operators. Logical operators are given below: a) AND operator.
Approved by Curriculum Development Centre 185 Oasis Radiant Computer Science, Book 9 b) OR operator. c) NOT operator. a) AND operator : AND operator gives true result when all the given conditions are true. The truth table for AND operator is given below: Condition 1 Condition 2 Result T T T T F F F T F F F F b) OR operator : OR operator will give true result even if any one of the given conditions is true. The truth table for OR operator is given below: Condition 1 Condition 2 Result T T T T F T F T T F F F c) NOT operator: NOT operator gives the reverse result of the given condition. The truth table for NOT operator is given below: Condition Result T F F T d) String Operator: An operator which is used to join or concatenate two or more strings is called a string operator. In QBASIC “+” operator is used to concatenate strings. Operator Meaning Example Result + Concatenate PRINT "POK" + "HARA" POKHARA Operator and their precedence When we use more than one operator in an expression the order of precedence to evaluate the operations is called hierarchy of operation. When the operators are having the same priority, they are evaluated from left to right in the order
Oasis Radiant Computer Science, Book 9 186 Approved by Curriculum Development Centre they appear in the expression. The order followed by QBASIC while evaluating an expression is given below: 1. Arithmetic operation a. Exponentiation b. Multiplication and division c. Integer division d. Modulo arithmetic e. Addition and subtraction 2. Relational operations a. = b. > c. < d. <> e. <= f. >= 3. Logical operation a. AND b. OR c. NOT Shortcut commands (Immediate mode commands) FILES - this command displays the list of files and directories. Syntax: FILES<path specification> e.g. FILES =List all the files and directories of default directory. FILES “*.BAS”: It lists all the files having .BAS extensions. FILES “D:\*.*”: It lists all the files and directories of D drive. CHDIR: This command changes the default directory with specified directory. Syntax: CHDIR<Directory name> e.g. CHDIR “C:\RAM”: It changes the default directory by RAM directory. MKDIR: This command creates a new directory in the specified location. Syntax: MKDIR<Directory name> e.g. RMDIR “D:\ABC” : It creates a new directory ABC in D: drive. RMDIR: This command removes or deletes the empty directory. Syntax: RMDIR<Directory name>
Approved by Curriculum Development Centre 187 Oasis Radiant Computer Science, Book 9 e.g. RMDI “C:\RAM” It deletes the directory RAM from C: drive NAME: This command changes the name of file with a new name. Syntax: NAME, old file name>AS <new file or files. e.g. NAME “TEMP.DAT’ AS “RECORD.DAT”: It renames the file “TEMP.DAT” to ‘RECORD.DAT”. KILL: This command is used to delete a file or files. Syntax: KILL<file name> e.g. KILL “record.dat” : It deletes or removes a file named “record.dat”. SHELL: This command changes the QBASIC mode to DOS mode. Syntax: SHELL e.g. SHELL: It changes QBASIC mode to DOS mode. Expression An expression can be a variable or constant or combination of constants, variables with operators. On the basis of result returned by an expression, there are three types of expressions. They are: i) Arithmetic or Numeric expression. ii) Logical or Boolean expression. iii) String expression. i) Arithmetic or Numeric expression: The expression which returns a numeric value as an output is called numeric or arithmetic expression. Arithmetic operators are symbols used in arithmetic expression. The example of arithmetic expressions are given below: SI = P * T * R/ 100 A = (I ^ 2) + Z ii) Logical or Boolean expression: The expression which returns one of two logical values either true or false is called logical expression. This type of expression also called Boolean
Oasis Radiant Computer Science, Book 9 188 Approved by Curriculum Development Centre expression. It gives - 1 if the result is true and 0 if the result is false. For example: PRINT 10>5 returns -1. PRINT 5<3 returns 0 iii) String expression : The expression which returns the string value after processing string with the help of string operators is called string expression. For example: CLS A$= “Computer” B$= “Science” PRINT A$ + B$ END Conversion of Algebraic expression into QBASIC expression. While using expression in QBASIC, we should use QBASIC expression in programming. The following table shows the conversion of Algebraic expression into QBASIC expressing. Mathematical Expression QBASIC Expression AB + CD A * B + C *D P × T × R 100 I= (P * T * R) / 100 A = l × b A = l * b P = 2 ( l + b) P = 2 * (l + b) 3C3 + 2A2 3* C ^ 3 + 2 * A ^ 2 AB NM (A * B) / (N * M) (a+b) (a + b) ^ (1/2) A2 + B2 (A ^ 2) + (B ^ 2)
Approved by Curriculum Development Centre 189 Oasis Radiant Computer Science, Book 9 Points to remember BASIC is one of the most popular programming languages. QBASIC is a high level programming language developed by Microsoft Corporation, USA. The largest portion of the QBASIC editor screen is called view window or programme window. The wide rectangular bar is called immediate window. Numeric data consists of all numeric values (0 to 9) that can be used for mathematical calculations. String data consists of all the alphanumeric values. A variable is a name or reference for any value. There are two types of variables used in QBASIC. They are: Numeric variable and string variable. The data item whose value remains the same or unchanged during the execution of programme is called a constant. There are two types of constants used in QBASIC. They are: Numeric constant and string constant. Keywords are words which have special meanings in QBASIC. They are the commands that instruct the computer to carry out an action. Operations are symbols or signs used to perform different operations between operands and give results. Relations operators are used to compare two values. Logical operators are used to combine two or more expressions containing relational operators. An expression can be a variable or constant or combination of constants and variables with operators.
Oasis Radiant Computer Science, Book 9 190 Approved by Curriculum Development Centre Exercises 1. Fill in the blanks. a. …………………is the high level programming language. b. The two windows of QBASIC screen are called………………….and………………. c. In QBASIC, we can use mouse as well as…………………… d. Two data types used in QBASIC are………………….and…………… 2. Write whether the following statements are True or False. a. QBASIC was developed by Microsoft Corporation. b. Logical expression returns three logical values. c. NOT operator gives the reverse result of the given conditions. d. Operators are symbols or sings that are used to perform different operations. 3. Answer the following questions. a. What is a variable? b. What are relational operators? c. Make a truth table of AND, OR and NOT operators. d. Differentiate between view window and immediate window. e. Differentiate between variable and constant. f. What is an operator? List different types of operator used in QBASIC. 4. Write down the syntax and meaning of the following commands. i. NAME ii. FILES iii. KILL vi. MKDIR
Approved by Curriculum Development Centre 191 Oasis Radiant Computer Science, Book 9 Chapter 12 Programme Statements ffl The programming statements ffl Controls statements ffl Functions in QBASIC THIS CHAPTER COVERS : QBASIC
Oasis Radiant Computer Science, Book 9 192 Approved by Curriculum Development Centre Programme Statements 12 Chapter The statement is a collection of commands used in a programme. CLS, INPUT, PRINT, REM and END are some examples of QBASIC statements. a. Declaration statements b. Assignment statements c. Input/output statements a) Declaration statements (CONST, DIM, REM) The statements which are used to declare variables in the programme are called declaration statements. CONST, DIM and REM are the examples of declaration statements. CONST statement: This statement holds the constant value for the variable and makes it fixed throughout the programme execution. Here, CONST stands for constant. According to its name, this statement is used to store only one data in a particular variable through the programme execution. Syntax: CONST PI = 22/7 DIM statement: DIM statement is especially used to declare an array variables. It can also be used to declare an ordinary variable (explicit declaration) Syntax: DIM variable AS data type ----------> Simple/explicit variable declaration. DIM variable(subscript) ------------- > Array variable declaration. Example: DIM NAME AS STRING DIM AGE AS INTEGER ---------------- > Simple Variable declaration DIM NAME$(5) DIM AGE (5) ----------------- > Array variable declaration
Approved by Curriculum Development Centre 193 Oasis Radiant Computer Science, Book 9 REM statement: This statement is used to give remarks or comments to the programme or statement. We can use this statement any number of times as possible, but it does not increase the size of the programme. We can also use single quote (‘) in place of REM statement Syntax: REM comment for the program or statement‘ comment for the program or statement Example: REM program for finding simple interest.‘ this statement will store value for rate of interest. b) Assignment statements (LET/SWAP) The statements which are used to assign a numeric or string value to a variable are called assignment statements. LET and SWAP are examples of assignment statement. LET statement: It is used to assign a numeric value or a string expression to a variable. But, it is an optional statement. Syntax: [LET]<variable>=<string expression/ value> Example: LETI=25 LET C$ = “COMPUTER” SWAP statement: This statement is used to exchange the values of two similar types of variables. Syntax: SWAP < variable1, variable2> Example CLS A= 5 B= 10 C= 15
Oasis Radiant Computer Science, Book 9 194 Approved by Curriculum Development Centre D= 20 PRINT “Before swapping” PRINT A, B, C, D SWAP A, B SWAP C, D PRINT “After swapping” PRINT A, B, C, D END c) Input/Output Statement Input/ Output statements are those statements that allow the user to input data to the computer and pint the data. They are used to perform input/ output operation for the computer. INPUT, PRINT and LPRINT are examples of Input/ Output statements. CLS statement: It clears the previous output from the display screen and makes the screen blank. Syntax: CLS Example: CLS INPUT Statement: INPUT statement is used to read input from the keyboard during programme execution. Syntax: INPUT [“Prompt” {; l ,}] var1, var2, var3,……….var n. Example: INPUT “Enter your name”; n$ INPUT “Enter your roll no. and section”; r, sec$ READ…DATA statement.
Approved by Curriculum Development Centre 195 Oasis Radiant Computer Science, Book 9 READ…DATA statement is used as complimentary to each other. READ statement reads multiple values by multiple variables from the data listed in the DATA statement. The number of data listed in DATA statement must be equal or more than the variables in READ statement. The sequence of type of variable used in READ statement and the types of data in the DATA statements must match. Syntax: READ variable1, variable2, …….. DATA value1, value2, ………… Example: CLS READ SNO, NAME$, CL PRINT SNO, NAME$, CL DATA 5, Ram,9 END INPUT$ Statement This statement will show a string of character read from a specified file. Syntax: INPUT$ (n[,[#]file number%]) Example: OPEN “TEST.DAT” FOR OUTPUT AS #1 PRINT #1, “OSIS” CLOSE OPEN “TEST.DAT” FOR INPUT AS #1 PRINT INPUT$(3,1) ‘Print first 3 characters. CLOSE INKEY$ Statement This statement reads a character from the keyboard.
Oasis Radiant Computer Science, Book 9 196 Approved by Curriculum Development Centre Syntax: INKEY$ Example: PRINT “Press Esc to exit….” DO PRINT “NEW NEPAL.” LOOP UNTIL INKEY$ = CHAR$(27) ‘27 is the ASCII code for Esc. PRINT (?) statement PRINT statement is used to display string expression and/ or the value of variable(s) stated, on the screen. PRINT statement without any string expression or variable is used for leaving a blank line on the output screen. In place of PRINT statement we can use? (question mark). Syntax: PRINT [“String Expression” {, l ;}] var1, var2…….var n. Example: PRINT “My first program in QBASIC” PRINT PRINT “Value of variable TAB Statement: This statement is used to insert the given the numbers of spaces before printing. It is used with PRINT or LPRINT statements. Syntax: TAB (column%) Example: PRINT “KATHMANDU”; TAB(25); “NEPAL” A$ = “SWARNIM” B$ = “COMPUTER” PRINT A$; TAB (10); B$
Approved by Curriculum Development Centre 197 Oasis Radiant Computer Science, Book 9 SPC Statement: The SPC statement is used in PRINT and LPRINT statements to print or output a number of space characters. Syntax : SPC(count%) Example : PRINT “ABC”; SPC(3); “123” LOCATE Statement: LOCATE statement moves the cursor to a specified position on the screen. Syntax: LOCATE [row%] [,[column%] Example: CLS LOCATE 5,5 PRINT “COMPUTER SCIENCE” DATE$ Statement The DATE$ statement sets the current system date on your computer. Syntax: DATE$ = string expression$ Example: DATE$ = “03-01-19” PRINT DATE$ TIME$ Statement The TIME$ statement sets the current system time on your computer. Syntax: TIME$ = string expression$ Example: TIME$ = “10:55:38” PRINT TIME$
Oasis Radiant Computer Science, Book 9 198 Approved by Curriculum Development Centre PROGRAMME FLOW AND CONTROL STATEMENTS Control structure is the statement of the structure that is used to handle different conditions and iterations. It is used to control the flow of the programme. They are Sequential Structure, Selection Structure and Loop Structure. a) Sequential Structure: It is a structure in which the statements are executed sequentially one after another without changing the flow of the programme. Example: CLS INPUT A INPUT B LET S=A+B PRINT “Sum of Numbers”; S END b) Selection Structure: It is also known as a branching structure that allows to transfer the programme control from one part to another on the basis of a specified condition or without condition. Types of Selection Structure: i) If Statement ii) Select Case Statement i) IF statement As we know, QBASIC executes the instructions of programme sequentially one after another. But, sometimes problems cannot be solved sequentially. So, we need to branch or jump from one line to another line. IF…THEN statement is the conditional branching (jumping) statement. This statement is used to make decision. It executes a statement or a block of statements depending upon the given condition. The condition is given between IF… THEN. If the condition is true, it executes the statements and if the condition is false the execution continues with the next executable statement. It branches the control flow of programme instructions according to the given condition.
Approved by Curriculum Development Centre 199 Oasis Radiant Computer Science, Book 9 This statement can be used in the given condition -- IF…THEN statement. (single line IF statement) Syntax: IF<condition> THEN<statement> Example: CLS INPUT “Enter first number”; A INPUT “Enter second number”; B IF A>B THEN PRINT A END IF…THEN…ELSE statement. (Multi Line IF statement) Syntax: IF<condition>THEN <statement1> ELSE <statement2> END IF Example: CLS INPUT “Enter any number”; N IFNMOD2=0THEN PRINT “Even Number” ELSE PRINT “Odd Number” END IF END IF…THEN…ELSE IF statement. (Nested IF statement) Syntax: IF<condition 1> THEN <statement1> ELSE IF<condition 2> THEN <statement 2> …………………… ……………………
Oasis Radiant Computer Science, Book 9 200 Approved by Curriculum Development Centre ELSE <statement n> END IF Example : CLS INPUT “Enter a number”; N IF N< 100 THEN PRINT “It is lesser than 100” ELSE IF N= 100 THEN PRINT “ It is equal to 100 ELSE PRINT “It greater than 100” END IF END ii) SELECT CASE statement Executes one of several statement blocks depending on the value of an expression. Syntax: SELECT CASE testexpression CASE expressionlist1 [statementblock1] CASE expressionlist2 [statementblock2] CASE ELSE [statementblock-n] END SELECT Example: