The structure of computer programs The Basic building
The structure of computer programs The Basic building blocks of computer programs • All programs can be built from three logical building blocks • Selection/ifs • If the traffic in the high street is heavy Sequence • Take the first left • Take the second right • Take third exit at the roundabout 10/17/2021 • take the first left • take the first right • take the first left • else • follow the high street to the end Arab. OU - M 150 - Abu. Nawaf Repetition/loops • while you have not yet reached the turnoff for Little Whamping • go straight ahead at each roundabout 1
Selection • A selection instruction allows you to write programs (and remember programs are basically just plans) that will operate in different ways depending on circumstances. If I win the lottery this Saturday I'll tell my boss what I think of her on Monday otherwise I'll start an OU degree and get a better job in a couple of years 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 2
True or false • Selection always includes a condition that can be evaluated when the program runs (i. e. the plan is executed) to be true or false (this is called a Boolean expression). • The truth or falsity of the condition determines a "flow chart" which path is taken. The condition is like a fork in the road determining which path you take True I win the lottery this Saturday tell my boss what I think of her on Monday 10/17/2021 Arab. OU - M 150 - Abu. Nawaf False start an OU degree 3
Repetition • A repetition instruction allows one or more instructions to be repeated a number of times. While I've got a chance of winning I'll keep putting one foot in front of the other I've got a chance of winning False True Sorry Paula! 10/17/2021 put one foot in front of the other Arab. OU - M 150 - Abu. Nawaf 4
An Introduction to programming using Java. Script Ø What is Java. Script (JS)? Ø A Sample Program Ø Java. Script Variables Ø Data types Ø Operators Ø Prompt box Ø Programming for selection (the if statement) Ø Programming for repetition (the while statement) Ø Programming for repetition (the for statement) 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 5
What is the Java. Script? Ø Java. Script was designed to add interactivity to HTML pages. Ø Java. Script is a scripting language Ø A scripting language is a lightweight programming language ØJava. Script is usually embedded directly into HTML pages Ø Java. Script is an interpreted language (means that scripts execute without preliminary compilation) Ø Everyone can use Java. Script without purchasing a license Ø Java. Script is used to improve the design, validate forms, detect browsers, create cookies, and much more. Ø Java. Script is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Firefox, and Opera 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 6
First program. <script LANGUAGE=‘Java. Script’> tag: Every thing between <script> and </script> is written in a scripting language. § § The LANGUAGE attribute defines the scripting language, in our case ‘Java. Script’. § Java. Script is the default language, so that if no specific language is specified, HTML will assume that the code is Java. Script. § </Script> ends the <script> tag. 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 7
Programming for repetition: the “while” statement Introducing the “while” statement q Loops are ways that allow computer to do repetitive calculations or instructions for a certain program. q The while statement creates a loop that repeats until the test expression becomes false. q The syntax of the while statement is: while (Boolean condition/expression) { one or more statements } ü The statement part can be a simple statement terminated by a semicolon, or it can be a compound statement enclosed in braces. ü The expression is mostly a comparison of values, you can use any expression ü If the Boolean expression is true, the statement part is executed. 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 8
Introducing the “for” statement (loop) The general form of the for loop is: for (declare and initialize; test; update) statement q The for statement uses three control expressions, separated by semicolons. q it’s not mandatory to declare values inside the for statement. q First, The initialize expression is executed once, before any of the loop statements are executed. q Then, the test expression is evaluated and if it is true, the loop is cycled through once. q Then, the update expression is evaluated and it is time to check the test expression again. q The statement part of the form can be a simple statement or a compound statement. 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 9
Programming for repetition: the “for” statement Introducing the “for” statement (loop) q The for loop enables you to repeat the execution of a block of statements a predetermined number of times. q The for loop uses a variable as a counter. q To use a JS for loop, you need to know: ü the starting value of the counter; ü the final value of the counter for which the loop body is executed; ü the fixed number by which the counter is increased or decreased after each repetition; ü the statement(s) which make up the body of the loop (as for a while loop). 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 10
Arrays Structured data ØThere are many situations where we want to deal with collections of data values that are related to each other in some way. ØIn processing such collections of data values, we will often want to treat each value in a collection in a similar way. ØIn such situations we talk about structured data. ØIn most programming languages the name given to a collection of data values, that are all of the same type, e. g. numeric values or all string values, is an array. 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 11
What is an array? Ø An array is a data structure that consists of a list of data values of the same type; for example, numbers or Strings. Ø An array should have a name. Ø Each data value is called an element. Ø The position of the data element in the array is called the index or subscript. Ø Index of an array starts at 0. 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 12
Declaration and initialisation of arrays . . cont Ø var rain. Array = [111, 115, 200, 95, 23, 59, 144, 66, 37] Ø Java. Script interpreter recognizes this is an array because of the square brackets. Ø var rain. Array = new Array(12) Ø Reserved word new is used to create an Array object. Ø The number 12 will provide enough memory for the 12 elements that are going to be stored in it. Ø Initial value for the elements of the array is undefined. (See Fig 2. 3) 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 13
Functions Dealing with subtasks in programs Ø In real life, programs are often very large and have separate subtasks to perform. Ø We need to organize these subtasks. Ø Function is a way to handle and organize these subtasks. 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 14
Using functions in Java. Script Ø One big task may be broken into simpler subtasks. Ø A program is a main task that can be broken down into a collection of subtasks. Ø Java. Script provides functions as a way for handling subtasks. i. e. parse. Float(). 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 15
Functions with arguments General form of a function: function FUNCTION_NAME(PARAMETER_1, PARAMETER_2, . . . , PARAMETER_n) // Assumes: DESCRIPTION OF ASSUMPTIONS MADE ABOUT PARAMETERS // Returns: DESCRIPTION OF VALUE RETURNED BY FUNCTION { STATEMENTS_TO_PERFORM_(AND_RETURN)_THE DESIRED_COMPUTATION; } 10/17/2021 Arab. OU - M 150 - Abu. Nawaf 16
- Slides: 16