Download presentation
Presentation is loading. Please wait.
Published byPhoebe Ford Modified over 9 years ago
1
B065: PROGRAMMING OPERATORS AND SELECTION 1
2
Starter Have a go at the All Logic Signs activity. COMP1 Programming 8 Selection All Logic Signs.xls
3
Objectives Understand what is meant by selection in programming. Explore selection programming commands: IF Understand the use of logical operators.
4
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
5
Logical Operators in Programming EvaluationSymbolExample Equal to=Number = 1 Less Than<Age < 21 Less Than or Equal To<=Age <=17 Greater Than>Debt > 25 Greater Than or Equal To>=Debt > = 2000 Not Equal To<>Number <> 1 AND Number = 1 AND Age < 12 OR Number = 1 OR Number = 2 NOT NOT Age < 17
6
IF Logic Recap Which goes where to make a logical sentence? Let’s do one at a time. CLICK on a question mark to reveal.
7
What you believe in… QuestionWhat to do if TRUEWhat to do if FALSE IF 12 > 25Clap twiceClap once IF you are at CryptStand up, then sit downSpin your head around IF 30 < 42Wink twiceShake your hands IF 12 > 6*1Put your hands upClick twice IF you are in ICTStand up, turn around, sit back down Yawn IF 4*2 >= 12Roar like a lionMoo like a cow 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.
8
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
9
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 Then End If
10
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.
11
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 Note that each If requires a corresponding End If. See how we INDENT to make the code easier to understand.
12
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?
13
Multiple Conditions? ELSEIF What if you have multiple conditions (or rules) which you want to check?
14
Summary IF…END IF Basic Checks. IF…ELSE…END IFBasic 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.
15
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.
16
Objectives Review Understand what is meant by selection in programming. Explore selection programming commands: IF Understand the use of logical operators.
17
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
18
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.