1 / 13

Conditions & Branching

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?

abel
Download Presentation

Conditions & Branching

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. IP 10 Mr. Mellesmoen Conditions & Branching

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

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

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

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

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

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

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

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

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

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

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

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

More Related