130 likes | 224 Views
IP 10 Mr. Mellesmoen. Conditions & Branching. Making Our Programs Think. Our first program told the computer to say, Hello World. What if we wanted it to say Good Morning World or Good Afternoon World depending on the time of day?
E N D
IP 10 Mr. Mellesmoen Conditions & Branching
Making Our Programs Think Our first program told the computer to say, Hello World. What if we wanted it to say Good Morning World or Good Afternoon World depending on the time of day? Well, here’s what we need to do, we need to add a conditional statement.
If & Then In your editor, enter the following: If (Clock.Hour < 12) Then TextWindow.WriteLine(“Good Morning World”) EndIf If (Clock.Hour > 12) Then TextWindow.WriteLine(“Good Evening World”) EndIf
Task Using an If Then statement, tell your computer to say where you should be i.e. If (Clock.Hour < 12) Then TextWindow.WriteLine (“Have fun in Art”” EndIf
Else In our original program we really didn’t need the second IF THEN ENDIF statement, would could have used an Else statement. If (Clock.Hour < 12) Then TextWindow.WriteLine("Good Morning World") Else TextWindow.WriteLine("Good Evening World") EndIf
Indentation You likely noticed that the statements between If, Else and Endif are indented. The computer does not require this for the program to work, however it is just good form to do so as it helps us read the program easier.
Even or Odd We know what odd and even numbers are, but could our computer tell us if they are odd or even? Here is the code for that….give it a try. TextWindow.Write("Enter a Number: ") num = TextWindow.ReadNumber() remainder = Math.Remainder(num,2) If (remainder=0)Then TextWindow.WriteLine("The number is even") Else TextWindow.WriteLine("The number is odd") EndIf
Math.Remainder This simple command tells the computer to divide the first number by the second number. In our program we entered the line: Remainder=Math.Remainder(num, 2) num was already defined as the number entered by the user 2 was what we divided the first number by.
Branching So far we have had programs that run one line at a time. There is a special statement that can cause the computer to jump to another statement out of order. Enter this in your editor: i=1 start: TextWindow.WriteLine(i) i=i+1 If (i<25) Then Goto start EndIf
So… What did your computer spit out? Likely a list of numbers from 1 – 24. In our program we added a new statement that ends in a colon (:) start: This is called a label which our computer understands. Using the Goto statement we made our computer repeat an action over and over.
Endless Execution In our program where we determined if a number was odd or even we were only able to check one number. See what happens when you change your original program to this: begin: TextWindow.Write("Enter a Number: ") num = TextWindow.ReadNumber() remainder = Math.Remainder(num,2) If (remainder=0)Then TextWindow.WriteLine("The number is even") Else TextWindow.WriteLine("The number is odd") EndIf Goto begin TextWindow.Write("Enter a Number: ") num = TextWindow.ReadNumber() remainder = Math.Remainder(num,2) If (remainder=0)Then TextWindow.WriteLine("The number is even") Else TextWindow.WriteLine("The number is odd") EndIf
Task: Create a program using an IF THEN ELSE command for me to try. It needs to ask me a question and if it doesn’t receive a specific answer it will give me a response. Example on next slide
TextWindow.Write("What is your name: ") name = TextWindow.Read() TextWindow.WriteLine("Please to meet you " + name) TextWindow.WriteLine("How old are you? ") number = TextWindow.Read() If (number>18) Then TextWindow.WriteLine("Wow, you are old!") Else TextWindow.WriteLine("What grade are you in? ") EndIf number1=textwindow.Read() If (Number1=10)then TextWindow.WriteLine("Hey, I'm in grade 10 too!") Else TextWindow.WriteLine("Good luck with that") EndIf