Monday, November 25, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 7D


7.4 Library File Template
Peter was at the teacher's office per usual. He has a habit of confirming the day's lesson to check his understanding.
“So, have you figured out yet why the button presses doesn't register like normal when VSYNC is greater than 1?”
“The way I see it, it's by chance. If VSYNC is 2, then I have 1 in 2 in registering the button presses. If VSYNC is 10, then I have 1 in 10.”
“Correct. VSYNC is actually a WAIT command. Since the button is set only for 1/60th of a second, chances are, unless it's set right on the clock, it'll go undetected during the WAIT command.”
“What exactly is the difference between VSYNC and WAIT? They're both WAIT command, right?”
“VSYNC is guaranteed to fire every specified interval. Let's say you have a WAIT 60. If the program takes half a second to do its processing, then the whole waiting period will be 1.5 second. However, if you use VSYNC 60, then the whole waiting period will still be 1 second. It substracts the processing time from the waiting period.”
“Then I should use VSYNC most of the time.”
“Yes, you should.”
“The INPUT demo program there, it looks like a good way to process the input. Would that be a good template to use?”
“Yes, it is. In fact, you can just save it somewhere, and use it for future programs. You may want to modify to suit, but it will definitely save you some typing. Once you have learned GOSUB, you will learn to hide some complexity behind it. It'll take slower to process, and you may have to exceed the 1/60th jiffy, but that should be no problem.”
“Jiffy?”
“A technical term. It means 1/60th of a second.”
“Ah, I see.”
“Have you any other question?”
“Regarding the IF command, as I understand it, it requires a comparison. But you're not using a comparison.”
“Well, IF command requires True/False state. As I set the button to True/False, it works just fine. Furthermore, it is commonly accepted that zero equals FALSE, whereas non-zero equals TRUE.”
“So, I can do IF 1 THEN do something?”
“Correct. Also, there is a catch in the IF-THEN-ELSE structure. You may want to play with it. What happens if you see this code?”
IF cond THEN ?”TRUE”:?”FALSE”
Peter thought for awhile. “I expect to see 'FALSE' to be printed regardless of condition, and 'TRUE FALSE' to be printed when condition is true.”
“In fact, that is the behavior of old Basic programming. However, it is not the behavior of this Basic programming. Both statement are printed only if condition is true. That's why you can have this construct:
IF cond THEN stmt1:stmt2 ELSE stmt3:stmt4
“That would have caused an error with old Basic.” the teacher explained. “But it's perfectly legal and desirable here because then you can have multiple statement when the condition is TRUE, and multiple statement when the condition is FALSE.”
“Is that a good thing? To deviate from standard?” asked Peter.
“Normally, I'd say 'no'. However, in this case, a lot of code are written better that way, so I have to say 'yes'. Tradition is fine and all, but not if it impedes progress.”

No comments:

Post a Comment