Download presentation
Presentation is loading. Please wait.
1
ICS 3U Tuesday, September 21st
2
Conditional Statements If Statements
If statements are used in code to make decisions ... If a condition is true, then something is done If the condition is false, something else may happen An example would be doing one thing if the user enters an even number, and something else if an odd number is entered
3
Basic Structures Three types of if statements: Simple If
If / Else If / Else If / Else Simple If Something is done if the boolean expression is true If (boolean expression) then Statements that are executed for true
4
Basic Structures (con’t)
If / Else Something is done when true, and something else is done when false If (boolean expression) then - statements evaluated if true Else - statements evaluated if false
5
Basic Structures (con’t)
If / Else If / Else Multiple boolean expressions are included If (boolean expression) then - statements if this boolean expression is true Else If (boolean expression) then - statements if first expression is false, but this one is true Else - statements if all boolean expressions are false
6
Boolean Expressions / Variables
A boolean expression is an expression that evaluates to true or false (a boolean result) They involve relational operators (<, >, !=, ==, <=, >=) The result of a boolean expression can be assigned to a boolean variable. Boolean variables are used to enhance the readability of programs, and to help with debugging When boolean variables are converted to integers, they have a value of 0 or 1 (0 = false, 1 = true)
7
Boolean Expressions / Variables Example
Example: if age > 18 price = Better example (use of boolean variable): adult = age > 18 if adult
8
Logical Operators In addition to the relational operators, we can use logical operators: AND && OR || NOT ! These are used to combine boolean expressions to create new and more complext boolean expressions Remember they have an order of operations: NOT, AND, OR NAO
9
Logical Operator Examples
Example: if adult && passholder then price = if tired || hungry then go home if ! happy then tell a joke
10
Logical Operator Rules
AND && - both expressions have to be true This course is ICS3U and all students are 15 yrs old This course is ISC 3U and the teacher is Miss Nieuwenhuis OR || - only one of the expressions needs to be true It is a day 1 or it is a day 2 This course is ICS 3U or all students are 15 yrs old NOT ! – the opposite ! (age < 15) ! (weather is sunny)
11
ActionScript Syntax
12
Work Time! Programming Exercises – see wiki
NEW Programmers – Stay UP HERE with me for some additional examples
13
Examples Write if statements for the following:
User has entered in a number, output if the number is greater than 100 User has entered in their mark, output if the grade is a pass or a fail User has entered in their age, compare their age to yours output younger, same age or older.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.