Basic Program Logic: Input / Process / Output, and basic Loops
Previously, we had talked about how some people cannot problem-solve. While others take to it like duck to water. They can't see the process of the programing, esp. when it comes to applying if branches or even loops. So let's discuss that a bit.
A very simple program takes some input, process the input, and generates an output.
- Get input
- Process input
- Display output
Let's Write a Simple Input/Process/Output program
var a = prompt("Enter a",0) // input
var b = prompt ("Enter b",0) // input
var x=parseInt(a)+parseInt(b) // process
alert("Result is "+x) // output
You may have noticed I used parseInt. That's because prompt returns a STRING. And when you add "1" to "2", you get "12", not 3. We want to add 1 and 2, not "1" and "2". And parseInt turns that into an integer.
output=ProcessFunction(input)
Let's write it in JavaScript.. Note this doesn't care about specifics of input or output.
function add2(aa,bb) {
var xx = 0;
xx=aa+bb;
return xx }
So if we want to write the processing bit as a function, the program would look like this...
function add3(aa,bb) {
var xx = 0;
xx=aa+bb
return xx }
var strA = prompt("Enter a",0) // input
var strB = prompt ("Enter b",0") // input
var x = add2(parseInt(strA),parseInt(strB)) // process
alert("Result is "+x) // output
And here we can easily dump the result to the console with console.log...
console.log("Result is ",x)
The idea is... HOW you do the input and/or output should be SEPARATE from how you do processing of the data. So you can switch them around as needed.
Let's Loop the Input/Process/Output Program
With me so far? Good. Now let's do this in a loop. Let's say, instead of making this run only once, I want it to run until I want it to add 0 and 0.
As the condition is "keep running until..." this is a while loop situation. Basically, it's "while NOT exit_condition..."
So what is the exit condition? when both strA and strB = "0" Remember, they are STRINGs.
What are we looping? All three steps: input, processing, and output.
So the while statement has to go outside the input/process/output block. We will create a new variable to track the exit condition.
function add3(aa,bb) {
var xx = 0;
xx=aa+bb
return xx }
var exit_condition = false
while (!exit_condition) {
var strA = prompt("Enter a",0) // input
var strB = prompt ("Enter b",0") // input
if (strA=="0" && strB=="0") break; // bust out of loop
var x = add2(parseInt(strA),parseInt(strB)) // process
alert("Result is "+x) // output
} // end loop
Now this will keep running until you enter 0 for both A and B.
Let's Sum Six Numbers
Okay, we're adding 2 numbers. What if we want to add, say, SIX numbers together?
Why, you'd use a loop.
What type of loop?
Since we know exactly how many times to run this, this would be a for loop.
So what goes inside the for loop?
Let's think about this.
Obviously, the input goes inside the for loop. We need users to enter all six numbers.
But does processing go inside the for loop or not?
Turns out, it can go both ways.
You can definitely declare six variables, a,b,c,d,e,f then sum them. You can even use an array so you don't have to declare six variables.
You can also do a running total. like sum=sum+currentInput. In this case, you only need one variable to hold the input, and one variable to hold the sum.
Which is better? Keep in mind there is no "overall better", only better for the circumstances and requirements. The former uses more memory but separates the input from the processing, so it technically easier to debug, in case you run into processing that's very hard and you're not sure if the data is bad or the processing is bad, and you need to "validate" them separate.
If you choose to combine processing and input inside the same loop, then you need to debug them together. Right now, with just six items and a simple summation, the difficulty is trivial. But when you start to process thousands of items, and processing involves hundreds of lines of code, you may appreciate the separation. On the other hand, this uses practically no extra memory.
Either way would work fine.
So, can you do the problem now? Can you do it with BOTH approaches, and compare the two?
Comments
Post a Comment