Presentation is loading. Please wait.

Presentation is loading. Please wait.

Playing computer with logic problems Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Playing computer with logic problems Please use speaker notes for additional information!"— Presentation transcript:

1 Playing computer with logic problems Please use speaker notes for additional information!

2 start firstNum = 10 secondNum = 5 thirdNum = 1 theAns = 0 do while thirdNum < 10 if firstNum > 10 firstNum = firstNum + 5 secondNum = secondNum + 5 thirdNum = thirdNum + 5 else if secondNum > 10 firstNum = firstNum + 3 secondNum = secondNum + 3 thirdNum = thirdNum + 3 else secondNum = secondNum + 4 end if end while loop theAns = firstNum +secondNum + thirdNum display theAns end To start the program: firstNum secondNum thirdNum theAns thirdNum = 1 so thirdNum is less then 10 so I move on to the first if statement firstNum = 10 so firstNum is NOT greater than 10 so I go to the else. secondNum = 5 so secondNum is NOT greater than 10 so I go to the else I add 4 to secondNum. Since secondNum started at 5, it is now =9 NOW: firstNum secondNum thirdNum theAns Now I drop through the end if, end if and end while loop and return to the do while statement to check and see if the loop should be done again.

3 start firstNum = 10 secondNum = 5 thirdNum = 1 theAns = 0 do while thirdNum < 10 if firstNum > 10 firstNum = firstNum + 5 secondNum = secondNum + 5 thirdNum = thirdNum + 5 else if secondNum > 10 firstNum = firstNum + 3 secondNum = secondNum + 3 thirdNum = thirdNum + 3 else secondNum = secondNum + 4 end if end while loop theAns = firstNum +secondNum + thirdNum display theAns end NOW: firstNum secondNum thirdNum theAns thirdNumis 1 so it is still < 10 so I enter the loop firstNum is 10 so is NOT greater than 10 so I go to the else secondNum is 9 so is NOT greater than 10 so I go to the else I add 4 to second number which is currently equals 13 NOW: firstNum secondNum thirdNum theAns Now I drop through the end if, end if and end while loop and return to the do while statement to check and see if the loop should be done again.

4 start firstNum = 10 secondNum = 5 thirdNum = 1 theAns = 0 do while thirdNum < 10 if firstNum > 10 firstNum = firstNum + 5 secondNum = secondNum + 5 thirdNum = thirdNum + 5 else if secondNum > 10 firstNum = firstNum secondNum = secondNum + 3 thirdNum = thirdNum + 3 else secondNum = secondNum + 4 end if end while loop theAns = firstNum +secondNum + thirdNum display theAns end NOW: firstNum secondNum thirdNum theAns thirdNum is still 1 and that means it is less than 10, so I enter the loop. firstNum is 10 so it is still not greater than 10 so I go to the else. secondNum is 13 so secondNum is greater than 10 so I execute the three statements under that it. NOW: firstNum secondNum thirdNum theAns Now I drop through the end if, end if and end while loop and return to the do while statement to check and see if the loop should be done again.

5 start firstNum = 10 secondNum = 5 thirdNum = 1 theAns = 0 do while thirdNum < 10 if firstNum > 10 firstNum = firstNum + 5 secondNum = secondNum + 5 thirdNum = thirdNum + 5 else if secondNum > 10 firstNum = firstNum secondNum = secondNum + 3 thirdNum = thirdNum + 3 else secondNum = secondNum + 4 end if end while loop theAns = firstNum +secondNum + thirdNum display theAns NOW: firstNum secondNum thirdNum theAns thirdNum is 4 and that means it is still less than 10, so I enter the loop. firstNum is 13 so it is greater than 10 so I execute the statements in the if by adding 5 to the numbers NOW: firstNum secondNum thirdNum theAns Now I drop through the end if and end while loop and return to the do while statement to check and see if the loop should be done again. Note that I do not enter the else on the first if so I do not ask the second question.

6 start firstNum = 10 secondNum = 5 thirdNum = 1 theAns = 0 do while thirdNum < 10 if firstNum > 10 firstNum = firstNum + 5 secondNum = secondNum + 5 thirdNum = thirdNum + 5 else if secondNum > 10 firstNum = firstNum secondNum = secondNum + 3 thirdNum = thirdNum + 3 else secondNum = secondNum + 4 end if end while loop theAns = firstNum +secondNum + thirdNum display theAns NOW: firstNum secondNum thirdNum theAns thirdNum is 9. The do while says to enter the loop if thirdNum is less then 10, so I enter the loop. firstNum is 18 so the answer to the firstNum > 10 question is yes and I execute the three commands. NOW: firstNum secondNum thirdNum theAns Now I drop through the end if and end while loop and return to the do while statement to check and see if the loop should be done again. Note that I do not enter the else on the first if so I do not ask the second question.

7 start firstNum = 10 secondNum = 5 thirdNum = 1 theAns = 0 do while thirdNum < 10 if firstNum > 10 firstNum = firstNum + 5 secondNum = secondNum + 5 thirdNum = thirdNum + 5 else if secondNum > 10 firstNum = firstNum secondNum = secondNum + 3 thirdNum = thirdNum + 3 else secondNum = secondNum + 4 end if end while loop theAns = firstNum +secondNum + thirdNum display theAns NOW: firstNum secondNum thirdNum theAns thirdNum is 14 so it is no longer less than 10. This means that I do not enter the loop. When I drop out of the loop, I execute the sentence immediately after the end while loop. theAns = theAns = will be displayed on the screen


Download ppt "Playing computer with logic problems Please use speaker notes for additional information!"

Similar presentations


Ads by Google