1 / 18

B065: PROGRAMMING

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

aine
Download Presentation

B065: PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  6. 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.

  7. 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.

  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 <condition> Then <statements> 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 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 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.

  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?

More Related