Unit 6 - Software Design and Development LESSON 3 KEY FEATURES
Last session 1. Language generations. 2. Reasons why languages are used by organisations. 1. Proprietary or open source. 2. Features and tools. 3. Availability of trained staff. 4. Reliability. 5. Development and maintenance costs. 6. Expandability
What is covered in this session Key features of programming languages
Features of programming Assignment 3 requires you to write a computer program. In order to write this program we need to understand key features of programming. These key features apply to all programming languages.
sequence; selection eg Features of programming case, if then else; iteration eg repeat until, while.. do; variables eg naming conventions, local and global variables, logical operators; assignment statements; input statements; output statements
Code Structures It is important to have a logical structure to programs. Programs should be organized so developers and maintainers can easily determine what modules and routines perform what function. In procedural paradigms the logical structure follows a top down approach.
Code sequences In all programming languages, irrespective of programming paradigms, code must be executed in sequence. One line at a time. This is the order of execution (the order in which the code runs).
Code sequences Normal code will run line by line. In VB.NET, the program starts with the public class. In Scratch, the program starts at the top and works downwards.
Task 10 Use Visual Studio to Create a new (Visual Basic) VB.net Windows forms application name this U06Exercise1. You should have a blank form in the design window. Now do Lab04 part a (only part a). What result did you get?
Selection statements Selection is about decision making in your code. break the line-by-line reading tell the program to jump to another section. An example of this would be an IF statement. In VB.NET, you can make use of for loops and if statements. In Scratch, you can also use loops if a criteria is/isn t true.
Selection statements If statements check for a true condition and then execute the dependant statements. If a = b then.. End if Is this true? If so, do this!
Selection statements What do these statements do? 1. If x > 0 and x < 5 then Console.writeline(x.tostring) End if 2. If x > 0 then if x < 5 then End if End if Console.writeline(x.tostring)
Selection statements Add a button to your form named btnselection, the text should say Selection Add a numericupdown control named numnumber. Change its max value property to 100. Now do Lab04 Part b. Make sure you add comments to the code.
Case statements An alternative way to code multiple If/elseif Statements.
Case statements. Example of If/elseif If x = 0 then console.writeline(x.tostring()) Elseif x = 1 then console.writeline(x.tostring()) Elseif x = 2 then console.writeline(x.tostring()) etc
Example of If/elseif replaced with case. Select Case x Case 0 console.writeline(x.tostring()) Case 1 console.writeline(x.tostring()) Case 2 console.writeline(x.tostring()) End Select This is easier to read and maintain.
Case statements Add a button to your form named btncase, the text on the button should read Case. Do Lab04 Part c. Comment the code. Is this code easier to read than that in part b?
Iteration statements. (Loops) Iteration in computing is the repetition of a block of statements within a computer program. Two types of loop. Fixed loops a sequence of statements is executed a fixed number of times. Dim ant as int32 = 0 For ant = 0 to 5.. Do something Loop An assignment statement
Iteration statements. (Loops) The second type is a loop which continues until a condition is met or continues while a condition is true. Do while x > 0 and x < 5. Loop Example of a pre-check loop condition
Loops Add a button to your form named btnloop, the text on the button should read Loop. Do Lab04 Part d. Comment the code.
Increment/decrement statements Consider these statements Dim x as int32 = 0 x = x + 1 what is the value of x? As an alternative we could do this x++ this produces the same result as the line above. However, it only increments by 1.
Increment/decrement statements Consider these statements Dim x as int32 = 0 x = x - 1 what is the value of x? As an alternative we could do this x-- this produces the same result as the line above. However, it only decrements by 1.
Increment/decrement statements Consider these statements Dim x as int32 = 12 x = x + 1 what is the value of x? As an alternative we could do this x++ this produces the same result as the line above. It still only increments by 1.
Assignment statements Assign values to variables use = or := E.g. myvariable = 20 Can be combined with mathematical operator E.g. myvariable = 20 + subtotal
Mathematical Operators myvariable = 20 + 2 * 3 What answer will this produce? Why? BIDMAS Operator Meaning Example Precedence ^ Exponentiation 2^3=8 1 / Division 10/2=5 2 * Multiplication 2*3=6 3 Mod Modular arithmetic (remainders) 5(mod)2=1 4 + Addition 4+5=9 5 - subtraction 7-4=3 6
Input and output statements In visual programming input and output usually accomplished by controls. List boxes; text boxes; buttons Procedures collect values entered into boxes E.g. MyText = txtmyinput.text variable textbox
Input and output statements Similarly for output Procedures enter values into boxes E.g. lblmylabel.text = mytext label variable String value could also be used.
Variables Data input into a program must be stored Stored in defined memory areas variables Temporary storage for data used in processing Source of data for data output Temporary lost when program ends If needed as permanent data must be saved as output to files.
Variables Name allocated by programmer to give identity within the program Naming conventions must be adhered to Data type defines sort of data to be stored Normally declared (created) at beginning of program E.g. in Vb.net Dim variable_name As data-type Dim student_name As string
Variables Naming conventions start with a letter (not a number) Can contain any combination of letters and numbers No spaces Can be upto 255 characters in length Cannot use any symbols (except underscore_) Cannot use any reserved words (e.g. PRINT) Should be meaningful to aid editing
Variables When declaring variables you cannot use reserved words. These reserved words are elements of the language syntax and therefore would confuse the compiler if variables were allowed to be named the same as syntax keywords
Local and Global Variables Most programs split into sections Procedures; sub-routines; modules Variables normally declared within a section Can only be accessed and used in that section. These are local variables.
Local and Global Variables Can declare variables to be accessed and used across entire program Global variables. Vb.net declared at top of program before any subroutine, use Public Shared instead of Dim e.g. Public Shared Grand_Total as Integer Not considered good practice but sometimes can t be avoided Can lead to problems due to confusion and misunderstanding especially in large projects with teams of programmers
Variables Task 11 Refer to the programme created for lab04 Create a short presentation to explain some Key features of programming languages Collect code snippets to show: Sequence Selection Iteration Use these code snippets to illustrate what each of these terms means.
Arrays Used to store a series of related values First value is numbered zero Dim age_0 as integer Dim age_1 as integer Dim age_2 as integer Dim age_3 as integer Dim age_4 as integer Dim age_n as integer Dim age(n-1) as integer A six-element array from age(0) to age(5) We could be here sometime writing this lot out
Array declarations VB.net Dim intcalendardays() As Int32 = {31, 28, 31, 30} ' etc Python calendardays = [31,28,31,30,..] Php $calendardays = array(31,28,31,30,.);
Logical operators Used by selection statements Boolean logic AND both conditions must be met OR only one condition needs to be met NOT reverses the input, often used in emergency stop routines.