Monday, November 25, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 8A


8. Panel Str + Complicated program
8.1 Do what you did, only different
“Nancy, how come the Princess killed the Dragon AND the Prince?” asked Linda. “Shouldn't the Princess end up marrying the Prince?” Linda was seated next to Nancy, and was trying out her text adventure program.
“That's a bad ending. I need to put in a bad ending in there, right? So, you need to bring the flower to the Prince, and kiss him, and hug him. Only then the Prince won't be drinking poisoned wine, becomes insane and tried to kill you!” Nancy currently holds the distinction of “having her head stamped with 'FAIL' the most.” The next closest student isn't even close.
“But, Nancy, I did all that! Something must be wrong with your program!”
“But I tried it and it works!” said Nancy.
“Let's see.” said Linda. She stopped the program and looked through the source code.
“Eh, you're not supposed to do that! You're looking through the source code! That's cheating!”
“I just want to make sure that I do it right.” said Linda. “Let's see. I was in the top of tower right after I kissed him. See here? There's only bad ending possible from here! You have a mistake in your program!”
“What?” asked Nancy. “Let me see that! Oh, that's because you haven't hugged the prince!”
“But I did!” said Linda.
“See, you kiss the Prince, and hug him!”
“No way! Hugs happens way before kissing!”
“Really? But it's the Prince! You don't hug him before kissing!”
“That's just in the movies! That's not what I do.”
“Well,” said Nancy. “That's why the Prince went insane and kill you!”
“That's not a very good story then!” said Linda.
“What's that?” asked Peter, who was just sitting down. “Your program is buggy?”
“There's no bug. Just a bone-headed story!” said Linda.
“There's nothing wrong with my program, and it's certainly not buggy!” insisted Nancy.
The bell rang, and the class settled down. The teacher walked in. “Bunch of failures like usual! There's only one passing score. Peter's. Everybody else can't even score a decent grade. Any question?”
“If the program doesn't work as it should, does that qualify as 'buggy'?” asked Peter.
“No.” said Nancy the ever optimist.
“Of course. Any deviation from planned performance can be considered a bug. Why do you ask?”
“Nancy's program is buggy.” said Peter. Linda nodded her head enthusiastically.
“But the program ran fine? There should be no bug.” said the teacher.
“Hugging before kissing is punished with death!” said Linda. The class was filled with laughter. The teacher thought for a while, trying to remember which story it was. “The dragon killing princess, right? That's not a bug.” Nancy stuck her tongue at Linda. “That's poor game design, with the usual outcome having the game dumped in discount bin.” Linda stuck her tongue at Nancy.
“By the way,” the teacher asked. “Does anybody know what's the first bug?” Nobody knows. “A moth.” said the teacher, smiling.

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

Simpleton Geek Teaches Computer Programming Book 2 Ch 7C


7.3 Dpad Menu + Button Presses
“Today,” the teacher said gravely, “We are going to learn what many other people have failed to learn properly. A lot of people have tried to do what we will do today, and failed utterly, never to return. In fact, it is no exaggeration that this is The River Test. Beyond the river lies richness beyond your dreams. All you have to do is cross the river. And yet, the river is strong and deadly. Can you cross the river successfully where many have tried and failed? Will you try?”
The students gulped. This is going to be very challenging, indeed. And from the sound of it, extremely dangerous. “What happens if we failed?” asked Nancy.
“The usual fate of failure is death. Many who have failed went on to other ventures, never to return.”
“Then maybe we should skip this and learn something else? Something easier?”
The teacher whacked Nancy's head. “Of course, we'll learn it. What are you talking about? Gya ha ha ha!”
All the other student wisely kept silent. The teacher surveyed the class. They are properly impressed.
Peter raised his hand. “What are we supposed to learn, anyway?”
The teacher smiled. “You're going to learn what to do when the user presses the 'A' button.”
“Whaaat? Is that all? How can something so easy be so difficult?” the students were shocked!
“Simple. Not easy. Two different things.”
“What can be so difficult?”
“If it's that easy to explain, then it wouldn't be difficult, now, is it?” The teacher smiled mysteriously.
The teacher went to the board and wrote this:
@INIT
@MAIN
@END
“Learn this carefully! This is the template in which all your programs will follow from now on. Deviate from this template at your own risk!”
“What happens if we do?” asked Nancy the clueless.
“You'll probably end up with spaghetti code and fail!” came the easy answer.
The teacher points to the board. “There are four main stages. Start of the program, Initialization phase, the Main Loop, and the Ending.
“Start of the program begins from the first line to @INIT. It shows the name of the program, name of the programmer, and date that it is written. Also, variable declarations are there.
“Initialization is where you initialize the variables, read in static data, load graphics, fonts, if need be.
“Main loop is where the main process begins. You will learn to separate different processes and mode. There are many methods possible here, and you will learn them later. We'll take it slow. The most important thing here is to understand how to read inputs. That means, button presses, and Touch Screen.
“Ending segment is easy because you really don't need to do anything here. Exception if you have some background music you need to stop, or clear the graphics you loaded before hand.
“We will learn all of these later. For now, we will learn how input works. That means keyboard, buttons, and touch screen. You have all learned how do read data using INPUT and DATA. The problem is INPUT stops the execution. You need real time input!”
REM INPUT DEMO

@INIT
'READING KEYBOARD
K$=””:KEYBOARD READ
S=0:KEYBOARD SCAN CODE
'READING BUTTONS
BT=0:'BUTTON READ
U=FALSE:'UP DPAD
D=FALSE:'DOWN DPAD
L=FALSE:'LEFT DPAD
R=FALSE:'RIGHT DPAD
A=FALSE:'A BUTTON
B=FALSE:'B BUTTON
X=FALSE:'X BUTTON
Y=FALSE:'Y BUTTON
'READING TOUCHSCREEN
TX=0:'TOUCH SCREEN X
TY=0:'TOUCH SCREEN Y
TS=0:'TOUCH SCREEN STATUS
TT=0:'TOUCH SCREEN TIME

@MAIN
VSYNC 1
K$=INKEY$:S=KEYBOARD
BT=BTRIG()
TX=TCHX:TY=TCHY:TS=TCHST:TT=TCHTIME

IF BT AND 1 THEN U=TRUE ELSE U=FALSE
IF BT AND 2 THEN D=TRUE ELSE D=FALSE
IF BT AND 4 THEN L=TRUE ELSE L=FALSE
IF BT AND 8 THEN R=TRUE ELSE R=FALSE
IF BT AND 16 THEN A=TRUE ELSE A=FALSE
IF BT AND 32 THEN B=TRUE ELSE B=FALSE
IF BT AND 64 THEN X=TRUE ELSE X=FALSE
IF BT AND 128 THEN Y=TRUE ELSE Y=FALSE

CLS
?”INKEY:    “;K$
?”SCANCODE: “;S
?
?”U=”;U
?”D=”;D
?”L=”;L
?”R=”;R
?”A=”;A
?”B=”;B
?”X=”;X
?”Y=”;Y
?
?”TX=”;TX
?”TY=”;TY
?”TS=”;TS
?”TT=”;TT

IF X==FALSE GOTO @MAIN

@END
END

“Okay. Type that program and run it. What do you see?” asked the teacher.
After the students run the program, several of them got confused. “Teacher, it doesn't seem to be working!”
“Oh, really? Is there an error somewhere or you're just not getting any response?”
“No response.”
“No response at all or is there something on the screen?”
“TX and TY has some numbers on them.”
“Then the program works as expected. Try moving your stylus around and see what happens to the Touchscreen variables.” said the teacher.
The students tried it, and it works fine. They can see the numbers working as they should. “Now try holding down the key on the keyboard.”
“Hey! The inkey and scancode are flashing! It's showing some number.”
“The inkey should show the character pressed. The scancode is showing the code associated with that keypress. Useful if you want to capture special keys such as backspace, shift, and tab.”
“But teacher! The buttons don't work!”
“The buttons works fine. Push X button and see if the program ends.” Sure enough, the program ends when the student pressed the X button.
The teacher pointed to the screen. “See this X variable turned to 1? This proves that the program works just fine. Now run the program again and while you're pressing this button or that button, press X. Also try it using the DPAD and see if all eight direction is accounted for.”
The students tried it. “No, teacher. It doesn't work!”
The teacher laughed. “See what I mean about how difficult it is? Now do you understand the problem? Even when the program works just fine, it still doesn't show that it works!”
“But it doesn't work!” said Nancy.
The teacher stamped her forehead with so much force, her head flipped backward quite a ways. “Owwww!” said Nancy. “Why did you hit me for?”
“I just said it works, how come you contradict me so easily?” said the teacher.
“But I don't see it!”
“And that's the trouble with you. You see that the X button work. Logically, there's nothing to prevent other buttons from working. So, they work. You just don't see it!”
“Then how can we tell it works?”
“The usual advice is to replace BT=BTRIG() with BT=BUTTON(). In a moment, I will let you do that, but first tell me. What will make you believe that it works without seeing it?
Nobody make any suggestion. Then Peter said, “How about doing some animation or text?”
“In other words, some long delayed process. You may add the code to see that if BT is not zero, then wait a while. Add this code just before GOTO.”
IF BT>0 THEN WAIT 60
The students tried it. “Oh, we see it now! It works after all!”
“See what I mean about how this simple process is very difficult to get right? You were misled into the wrong thing. Now, delete that WAIT line, and replace BT=BTRIG() with BT=BUTTON().”
The student dutifully tried it. “It works, teacher! BT=BUTTON() really works!”
“Fail! You've just did the wrong thing! See how difficult it is to get it right?”
“But, teacher! It really works! I can see it working!”
“First of all BUTTON() is the wrong way to do it. The correct syntax is BUTTON(0). But that's not the point. The point is, BTRIG() is not the same as BUTTON(0)! BTRIG is the same as BUTTON(1). Therefore, the alternative way to do it, isn't BUTTON(), but BUTTON(1)!”
“But what's the difference?”
“BUTTON(0) will keep set the button states for as long as the button is held down. Button(1) will set the states only for 1/60th of a second. That's why you cannot see it! It's too fast for your eyes to follow!
“Not too fast,” the teacher smiled, “for the computer to follow. So, set a counter at the @INIT, and add this line on the main just below the Touchscreen variables printing.
@INIT
C=0

'PRINT COUNTER
IF A THEN C=C+1
?”COUNTER=”;C
“Now run the program. It will count the number of button presses every time you press A button. See what happens.”
“Teacher! The program counts my presses multiple times! I only press it 3 times, and it counted 26 already!”
“Good. Now try it again with BT=BUTTON(1) and see what happens.”
After the students made the changes, the difference is clear. The program counted the number of presses correctly. A wave of understanding went over the students.
“And that is why reading button presses is so difficult to understand. Not because it is complex, but because it is so easy to go the wrong way. And remember, unless you have the ability to unlearn what you've learned incorrectly, there's no way back. Once you think that BUTTON() holds all the key to understanding how button works, it is very hard to unlearn that knowledge and accept that you misunderstood the process.
“The end result is a lot of frustration and unnecessary grief. This causes the mythical 'Programming is incredible difficult' train of thought that is very hard to get rid of.
“I say this before, and I say this again and again: Computer Programming is very easy to do IF you know what you're doing. So many people don't know what they're doing. So, they give up and never return. Instead of admitting that they're doing it wrong, they rather blame the tools.
“If computer programming can be equated to making cabinets, then these bad workmen blame their tools. That's just silly! If you don't know how a hammer work, it's stupid to blame the hammer. Then why would we accept that the computer is hard to program? It's not hard to program. It's your knowledge that is faulty. BASIC does not cause brain damage. The damage is already there!
“Now that you understand how this work, will you use BUTTON(0) in your program?” asked the teacher.
“No, teacher, we won't” said the students.
“That's too bad, because there are instances where BUTTON(0) will work better. When VSYNC is greater than 1 for example. In fact, why don't you try it now and see if the program still works fine when you do VSYNC 15?”
The students were stunned! The teacher got them yet again!


Simpleton Geek Teaches Computer Programming Book 2 Ch 7B


7.2 Number Menu + Wait for INPUT
The teacher was showing some menu on the board. It was simply a screenshot with numbered items.
SAMPLE PROGRAM
1. HELLO
2. CAT
3. DOG
4. END
CHOICE?
“So, this is what you have to do. You have already know how to do 'Press (Enter) to continue. Now you're going to do something about it. It's time for you to learn how to do GOTO! In fact, we're going to do something different. In addition to plain GOTO, we'll also do IF-GOTO, and ON GOTO! Actually, I was going to show you how to print on the lower screen, but since you asked about GOTO, may as well do it now! Gya ha ha ha!” Nancy was very embarrassed about it.
“Here's something you should know.” The teacher continued. “RESTORE can take a string as label! So, you can do this easily.”
CH$[0]=”HELLO”
CH$[1]=”CAT”
CH$[2]=”DOG”
CH$[3]=”END”
INPUT C:C=C-1
PRINT CH$[C]
“Very important to note that we're doing 'C-1' calculation in there. That's because array starts at zero, but menu starts at 1! Off by one is a very common mistake, and you need to ALWAYS watch for it! Depending on the situation, you may want to set 'END' as 0, but let's leave it for now. But we want more than that! So, given the right DATA statements, we can actually do this:
INPUT C:C=C-1
RESTORE “@”+CH$[C]
READ X$
and so on... (Appendix B)
“That will make it very convenient to write the program. There's another way to do it as well.
INPUT C
ON C GOTO @END,@HELLO,@CAT,@DOG,@END
“You see that in this case, I simply repeated the '@END' twice. You will also notice that I did not decrement the value of C at all. This is an example of the proper way to use GOTO can clarify the code. You need to understand when to use GOTO, and when NOT to use goto. In this case, using ON-GOTO clarifies the program tremendously, so you are well advised to use it in this case. What if you're using a computer language that do not have GOTO? You probably have to use IF-LADDER:
IF C==1 GOTO @HELLO
IF C==2 GOTO @CAT
IF C==3 GOTO @DOG
IF C==4 GOTO @END
“As you can see, there's a lot of repetition here. I suppose if you're going to be stubborn about it, you can do so. However, as far as readability goes, putting it into one line would have been preferable than scrolling down the screen too much. Scrolling down take time and energy. Therefore, I say that computer languages that does not allow you to put multiple commands in one line is not a well-designed language. Python, for example, for all their readability concern, doesn't allow you to do that. There are several other languages as well.”
“One more thing. The fact that strings can be label, means that you can do this:”
INPUT C:C=C-1
GOTO CH$[C]
“Which makes it very convenient. You can simply put the choices into DATA statements. Then as you read them into the array, you can display them on screen. Something like this:
RESTORE @MENU:CLS
READ CH$[0]:?”1”,CH$[0]
READ CH$[1]:?”2”,CH$[1]
READ CH$[2]:?”3”,CH$[2]
READ CH$[3]:?”4”,CH$[3]
“That way you need to type things only once. Which makes it very convenient to do. Let's do a simple template. Let's make a simple interactive story where you get to be the hero. Make it so that each step, you can make up to 4 choices to make. It's show and tell time. How are you going to do it?”
“We'll have the hero fight a dragon and rescue the princess!” said a student.
“Oh, yeah? We'll have the hero fight a dragon and rescue the PRINCE!” said a female student.
“Ha! The prince don't need any rescuing! Only the princess!”
“We don't need rescuing, either!”
“Hold it, right there!” said the teacher. “We're not into stories, yet. We're still discussing program structure. What is wrong with you guys? Are you not paying attention to the lesson plans? Either you give me program structure in the next 5 minutes, or I'll start whacking heads until you do!” All the students coiled in terror!
Finally, Peter said, “There is a screen of text at the top, followed by 4 choices at the bottom.”
“Right you are! Is that so hard to do?” The teacher wrote:
- 1 line of title
- 10 lines of text
- 4 lines of menu
“What's next?” asked the teacher.
“Then using ON-GOTO, we display another line of text.”
“ON-GOTO is implementation specific, but I get the idea. Here's the completed pseudocode:”
1. INIT
- TITLE$,TEXT$[10]
- PAGE$[4],MENU$[4]
- Choice (CH), P$
2. RESTORE-DATA
3. CLS:DISPLAY SCREEN
- 1 line of title
- 10 lines of text
- 4 lines of menu
4. INPUT CHoice
5. Set P$ according to CHoice
6. If not done, GOTO 2
“Is that simple enough? Now, it's very important that you follow this structure, so let's learn something else: REMark (REM) command. This command actually doesn't do anything. All it does is to ignore whatever is typed after this command. So, you can give yourself a little note for the program. So put that pseudocode in your code as comment. One more thing: Tick mark is a shorthand for REM, so you can use that as well.
REM This is just a comment.
'So is this. Tick mark is REM.
“Now I want you to write a simple story with branching. Each page will have a title, up to 10 line of text, and up to 4 menu choices. So, I want you to write it like this in your journal:”
Page 1: Page id
Title: Title string
Text: up to 10 lines
Menu0: Page id-Menu string
Menu1: Page id-Menu string
Menu2: Page id-Menu string
Menu3: Page id-Menu string
“Remember that each line is only up to 32 characters, so budget your lines accordingly. Also, you can leave blank lines if you wish. You can also leave blank menu items. Simply leave Menu string blank, and set the Page id to current Page-id. Alright? Let's get to it!”
The students get all worked up. They're busy writing stories in their journal. An example is shown in Appendix (C). After a while, all the students have gotten at least one page filled.
“Good.” The teacher said. “Now, what I want you to do is to write a bunch of DATA statements. Use the Page-id as label for the  DATA segment. Like this:”
@INTRO
REM TITLE
DATA “THE STORY OF WINDELL”
REM TEXT
DATA “ONCE UPON A TIME, THERE WAS A”
DATA “LITTLE RAT BY THE NAME OF”
DATA “WINDELL.”
DATA “ “
DATA “SHE WAS HUNGRY AND IN SEARCH OF”
DATA “CHEESE. WHERE DO YOU THINK”
DATA “SHE SHOULD GO?”
DATA “ “
DATA “ “
DATA “ “
REM MENU: PAGEID - MENU TEXT
DATA “KITCH”,”TO THE KITCHEN”
DATA “BEDRO”,”TO THE BEDROOM”
DATA “BADEND”,”STAY HOME”
DATA “INTRO”,” “
“I want you to write all these for homework, every page that you have, written in this form. And that's it! You already know how to save the program, but I don't want you to save anything. Just the DATA statements. Save it into another file, called 'STORY' or something like that. Tomorrow, we'll be doing the coding. Got it?”
The next day, everybody was ready with their story. However, the teacher simply led them to the implementation of the pseudocode. “Alright, everybody. Set your timer!”
The first thing we want to do is the initialization. Now, we already have the pseudocode, and of course, those are noted in the REM statements already right?” The students gulped. They didn't do it that way. “Never mind! Just do it right now and quick! You're on a timer! Gya ha ha ha!”
REM INIT
CLEAR:TITLE$=””
DIM TEXT$[10]
DIM PAGE$[4]
DIM MENU$[4]
CH=0:P$=”INTRO”
REM RESTORE-DATA
@SHOWPAGE
RESTORE “@”+P$
READ TITLE$
READ TEXT$[0]:READ TEXT$[1]:READ TEXT$[2]
READ TEXT$[3]:READ TEXT$[4]:READ TEXT$[5]
READ TEXT$[6]:READ TEXT$[7]:READ TEXT$[8]
READ TEXT$[9]
READ PAGE$[0]:READ MENU$[0]
READ PAGE$[1]:READ MENU$[1]
READ PAGE$[2]:READ MENU$[2]
READ PAGE$[3]:READ MENU$[3]

REM DISPLAY SCREEN
CLS:?TITLE$:?
? TEXT$[0]:? TEXT$[1]:? TEXT$[2]
? TEXT$[3]:? TEXT$[4]:? TEXT$[5]
? TEXT$[6]:? TEXT$[7]:? TEXT$[8]
? TEXT$[9]:?:?
? “1”,MENU$[0]
? “2”,MENU$[1]
? “3”,MENU$[2]
? “4”,MENU$[3]

REM INPUT Choice$
INPUT CH:CH=CH-1
P$=PAGE$[CH]

REM If not done, GOTO 2
GOTO @SHOWPAGE
“Okay! And done! That's less than 10 minutes! That what you get from using Copy and Paste option from the menu. What's that? You don't know where it is? It's on the lower right option. Use the arrow to set it to copy and paste. You didn't know that? What a loser! Can't even read the help menu! Gya ha ha ha!”
Once the students are all finished, the teacher then have them saved the program. “Make sure that you do not overwrite your homework! Save it under a different name! Got it? Good. Now I want you to stay in the RUN mode, and type APPEND storyfile. Hit Enter. Go to the EDIT mode. What do you see?”
The students did just that, and “Hey! I got my data at the end of the program!” “Me, too!”
The teacher laughed. “Now you see why DATA statements are traditionally located at the end of the program. That's because simply by appending a different file, you can have a different data immediately. It makes it very convenient to have a different story with just one program.”
Peter raised his hand, “Is this the way to do it? Normally, we just select the file from the menu. Does that mean the APPEND command works inside the EDITOR?”
“Not in Petit Computer case. That's a good question, by the way. Commands inside the program depends on the fact that you do not change the program while inside it. So, there is that liability involved there. Normally, you do not store the data in DATA statements.”
“You don't? But it's so convenient!”
“Actually, not at all. It's very inconvenient to do so. Much more convenient to just load the data off a file, which Petit Computer do not have. So, an alternative must be sought. There are several different alternatives.
“However, there is another command: EXEC which will execute another program. Now, you understand why you need to clear the memory before you allocate them via DIM. Once the memory has been allocated, it stays in the memory until explicitly cleared.
“Thus, you can simply EXEC a program which contains the DATA statements, and load the data into memory, then EXEC the actual program to process the data. By splitting the process into two different programs, you don't have to do APPEND each time you run it. You simply run the data loading program, which in turn runs the real program later on.”
Peter nodded his understanding.
“So, there you go. By the way, we did our program in about 10 minutes. How long does it take you to enter the data?”
“It took me one hour.”
“Only one hour? It took me three!”
“It only took me 2 hours”
“Me, too!”
The teacher laughed. “That's actually normal.”
“But teacher,” said Nancy, “That's totally backward! Shouldn't writing the computer program the most difficult task of all?”
“No, of course, not. Why do you say that?”
“Because writing computer program is so difficult and you need to debug them and test them for a long time!” Other students nodded their heads.
“Are you kidding me? Have you forgotten what I said in the beginning already?” asked the teacher. Seeing their puzzled facial expressions, it is obvious that they did.
“See, here, computer programming is one of the easiest thing you can do. Of course, the key is that IF you know what you're doing. Since most people don't know what they're doing, of course it's going to be difficult!
“Now look at this pseudocode. Do you see anything difficult about this at all?” All the students shook their heads. “See? Once you know what to do, it becomes the easiest thing to do. A routine matter. No trouble at all.”
The students were unconvinced. All their experience showed that computer programming is mind-numbingly difficult. Yet, they didn't have too much trouble at all.
“Do you wonder why you have no trouble at all? Think of it this way: How long does it take you to translate from English to a foreign language?”
“Which foreign language?” asked a student.
“The one you know well, of course. Whatever it is. So, how long does it take you?”
“Well, usually I need to write it down, then translate it, then read it. So about a minute or two?”
“Do you know that United Nation interpreters do it in real time? As soon as the other person speaks, the interpreter says it in foreign language. Immediately. Without a delay.”
“But how is that possible?”
“From experience. Just as you memorize multiplication table, you memorize grammar and vocabulary. Once you have it, no effort is expended. It's all automatic.
“Guess what? Computer programming is merely the translation from English (Pseudocode) into Computerese (Petit Computer). Once you know what's going on, it should be effortless, and it was.”
The students cannot argue with that. Indeed, the process was quick and effortless.
“Now let's see what we have here. Are you guys up to your stories? Swap them with your neighbor and play!”



Simpleton Geek Teaches Computer Programming Book 2 Ch 7A


7. Idle Loop
7.1 Input-Function-Output
The students were all waiting for their teacher to show up. They were showing each other their ASCII art and MADLIBS program. Everybody was enjoying themselves. Even at this early stage, they could do something fun.
“I wish there's a way to show more than one screen.” Nancy complained. “I can show more pretty things on my screen!”
“I'm sure there is a way to do so. Why not ask the teacher to show you?” said Linda, who was seated next to her.
“I'm afraid the teacher will get upset. You know how he is.” said Nancy. That's right. Terminator on the prowl.
The teacher comes into the classroom. “Okay, guys. Ready to start class?”
“Teacher, can you show us how to do 'Press A to continue?'” asked Peter.
“Hmmm. That will involve something you haven't learned already. A 'Press Enter to continue', however, is perfectly doable already. You mean you haven't tried that?”
“Sorry, teacher. It never occurred to me. Everything I've seen so far is 'Press A to continue'.” said Peter.
“Well, I guess I can't blame you for that. You never grow up in the environment where mouse and joystick are optional. Would you believe that at one time, the backspace key is optional? Gya ha ha ha!”
The teacher thought about it for a while. “Hey, you guys knows INPUT, right? Have you checked LINPUT yet?” Everybody shook their head.
“LINPUT is actually the simpler of the two. The reason why INPUT is taught because that's what was available back then, so it's done by convention. But INPUT actually parse the line so you can enter multiple values at once. You actually don't want it for a simple button press. So, use LINPUT instead. It only takes a single value, and is perfect for your use.”
?”PRESS (ENTER) TO CONTINUE”
LINPUT A$
?”YOU PRESSED (ENTER)!”
If you do that, you'll find that the program paused for your input before printing the second line. That's how you wait for further input. In fact, armed with this knowledge, you can do a mini note program, where you simply put the text on screen, one after the other. So far, you only have knowledge to just go down one route, no looping. That comes after GOTO. For now just do it the hard way. Gya ha ha ha!”
“But teacher, we don't want to learn GOTO!” said Nancy. The teacher's eyes narrowed dangerously. “Because GOTO means spaghetti code!” Nancy stammered her explanantion. The teacher was not convinced. “Because it jumbles the code and makes it hard to read!”
“GOTO doesn't make it hard to read. Code confusion is the fault of the programmer, not GOTO!” said the teacher.
“But it has been proven that GOTO means spaghetti code.” insisted Nancy.
“FAIL!” said the teacher as he stamped her forehead with the dreaded 'FAIL' stamper.
Nancy looked like she's about to cry. “Shouldn't a good teacher be able teach without violence?” she wailed.
“Hmmm. That's true. A good teacher can certainly do that.” the teacher said. “Too bad I'm not a teacher, but a professional! Gya ha ha ha!”