180 likes | 279 Views
B065: PROGRAMMING. OPERATORS AND SELECTION 1. Starter. Have a go at the All Logic Signs activity. COMP1 Programming 8 Selection All Logic Signs.xls. Objectives. Understand what is meant by selection in programming. Explore selection programming commands: IF
E N D
B065: PROGRAMMING OPERATORS AND SELECTION 1
Starter • Have a go at the All Logic Signs activity. • COMP1 Programming 8 Selection All Logic Signs.xls
Objectives • Understand what is meant by selection in programming. • Explore selection programming commands: IF • Understand the use of logical operators.
Operators Recap • Answer these questions with a TRUE or FALSE • 6 > 3 • 72 < 75 • 115 >= 115 • 92 > 42+2 • 12 = 6+6 • 32 <= 16*2 > MORE than < LESS than <= LESS than or EQUAL to >= MORE than or EQUL to
IF Logic Recap IF THEN ? ? OTHERWISE ? THEN OTHERWISE ? IF ? IF THEN OTHERWISE ? Which goes where to make a logical sentence? Let’s do one at a time. CLICK on a question mark to reveal.
What you believe in… You will be given a logical question. If you think it is TRUE, do what is says under TRUE. If you think it is FALSE, do the FALSE action.
A Separate Example • Let’s look at a spreadsheet version (From GCSE) which you should be familiar with. • It nicely sets the scene for selection in programming. • IF Example
IF…THEN…END IF • Sometimes in programming, you may only want to execute lines of code if certain conditions are met. • For example, you would not send out a bill to a customer if they did not owe anything. • The way to write an IF statement is simple: If <condition> Then <statements> End If
Examples If score = 100 Then Console.WriteLine("Full Marks!") End If If pay > tax Then pay = pay - tax End If If mark < 40 Then result = "Fail" console.writeline(result) End If The semantics are simple: if the condition is True, the following statement(s) will be executed; otherwise they will be omitted. If, Then, and End If are all keywords - the code editor will capitalise them and colour them blue when you finish typing any of them, as it does with all keywords.
What can you use? • The statements can be any legal VB statements - and that includes If … Then … End If statements now! So we can quite easily have a nested structure, like the following example: If age > 65 Then If theDay = "Thursday" Then 'Senior Citizens get a 10% discount! cost = 0.9 * cost End If End If • Note that each If requires a corresponding End If. See how we INDENT to make the code easier to understand.
IF…THEN…ELSE…END IF • What if you want some code to be executed if the condition IS NOT met? If mark < 40 Then result = "Fail" Else result = "Pass" End If Console.WriteLine(result) • You can use ELSE. What happens in the code above?
Multiple Conditions? ELSEIF • What if you have multiple conditions (or rules) which you want to check?
Summary • IF…END IF Basic Checks. • IF…ELSE…END IF Basic Checks with a catch situation. • IF…ELSEIF…ELSE…END IF Multiple conditions. • You can also use logical operators ( >, >=, <, <=, AND, OR, NOT) in your selection statements.
TASK • Complete the questions in your guide. • Attempt Tasks 10, 11 and 12. • We will go through each task throughout the lesson. • Complete for homework.
Objectives Review • Understand what is meant by selection in programming. • Explore selection programming commands: IF • Understand the use of logical operators.
Required Reading • Each week you will be given required reading. • If you fail to do this, you will 100% find the lessons which follow it EXTREMELY difficult. • Before next lesson you should have read: • Read Chapter 4 • Homework – Finish exercises 1-10 Chapter 4
Plenary • If the day is Monday and a person is 18 or over (but younger than 21), they qualify for a discount on drinks over £2. They receive 35p off the price of the drink. • What would be the IF block which would express this in code?