18
B065: PROGRAMMING OPERATORS AND SELECTION 1

B065: PROGRAMMING

  • Upload
    aine

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: B065: PROGRAMMING

B065: PROGRAMMINGOPERATORS AND SELECTION 1

Page 2: B065: PROGRAMMING

Starter Have a go at the All Logic Signs activity.

COMP1 Programming 8 Selection All Logic Signs.xls

Page 3: B065: PROGRAMMING

Objectives• Understand what is meant by selection in programming.

• Explore selection programming commands: IF

• Understand the use of logical operators.

Page 4: B065: PROGRAMMING

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

Page 5: B065: PROGRAMMING

Logical Operators in Programming

Evaluation Symbol Example

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 AND Number = 1 AND Age < 12

OR OR Number = 1 OR Number = 2

NOT NOT NOT Age < 17

Page 6: B065: PROGRAMMING

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.

Page 7: B065: PROGRAMMING

What you believe in…

Question What to do if TRUE What to do if FALSE

IF 12 > 25 Clap twice Clap once

IF you are at Crypt Stand up, then sit down Spin your head around

IF 30 < 42 Wink twice Shake your hands

IF 12 > 6*1 Put your hands up Click twice

IF you are in ICT Stand up, turn around, sit back down

Yawn

IF 4*2 >= 12 Roar like a lion Moo 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.

Page 8: B065: PROGRAMMING

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

Page 9: B065: PROGRAMMING

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

Page 10: B065: PROGRAMMING

ExamplesIf score = 100 Then Console.WriteLine("Full Marks!")End If

If pay > tax Then pay = pay - taxEnd 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.

Page 11: B065: PROGRAMMING

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 IfEnd If

• Note that each If requires a corresponding End If. See how we INDENT to make the code easier to understand.

Page 12: B065: PROGRAMMING

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 IfConsole.WriteLine(result)

• You can use ELSE. What happens in the code above?

Page 13: B065: PROGRAMMING

Multiple Conditions? ELSEIF• What if you have multiple conditions (or rules) which you

want to check?

Page 14: B065: PROGRAMMING

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.

Page 15: B065: PROGRAMMING

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.

Page 16: B065: PROGRAMMING

Objectives Review• Understand what is meant by selection in programming.

• Explore selection programming commands: IF

• Understand the use of logical operators.

Page 17: B065: PROGRAMMING

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

Page 18: B065: PROGRAMMING

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?