Chapter 4
Making Decisions

So far, the programs you’ve worked on have been somewhat simple. But sometimes you need to make a decision based on input from a user. And that’s where programming starts to get more challenging. Programs get longer and more complex, and testing them becomes more difficult. This is where test plans become even more important; to ensure correctness, you have to test all the possible ways the input can be interpreted.

So how do you make decisions in your programs? Most programming languages have an if statement where you compare a value to another value. In JavaScript, an if statement looks like this:

 
if​ (userInput === ​'Hello'​) {
 
// do something
 
}

If the input is Hello, then the code between the curly braces runs. This is a simple if statement. If the provided input was anything else, absolutely nothing would happen. Sometimes that’s what you want. Other times you may want to do something else, and so you can add an else statement:

 
if​ (userInput === ​'Hello'​) {
 
// do something.
 
} ​else​ {
 
// do something else.
 
}

And sometimes you may have more than an either-or situation:

 
if​ (userInput === ​'Hello'​) {
 
// do something.
 
} ​else​ ​if​ (userInput === ​'Goodbye'​) {
 
// do something different than the first thing.
 
} ​else​ {
 
// do something else.
 
}

If you have a lot of possible outcomes, then that might be a great time to use a switch statement:

 
switch​(userInput) {
 
case​: ​"Hello"
 
// do something.
 
break​;
 
case​: ​"Goodbye"
 
// do something different than the first thing
 
break​;
 
case​: ​"How was your day?"
 
// do something different than the other two things.
 
break​;
 
default​:
 
// do something else.
 
}

In a larger program, you may have to do different calculations in each part, or multiple steps. It’s possible to nest if statements inside other if statements, too, so you may have to do that from time to time. However, overuse of nested if statements can lead to code that’s hard to read and even harder to maintain over time. So as you get more comfortable, you’ll want to explore different solutions for decision processing.

Writing the code is only a small part of the problem. Figuring out what code to write is more difficult. Flowcharts can help you visualize the problem you’re solving, and they come in handy when wrapping your mind around decision logic.

For example, if you had to write a program that prompted the user for a number greater than 100, and you needed to display “Thank you” when the number is greater than 100 or “That’s not correct” if it’s 100 or less, you could create a flowchart like this:

images/controlflow/flowchart.png

And that flowchart would then be pretty easy to turn into pseudocode:

 
Initialize output to ""
 
Initialize userInput to ""
 
Prompt for userInput with "Enter a number greater than 100"
 
 
IF userInput is greater than 100 THEN
 
output = "Thank you."
 
ELSE
 
output = "That's not correct."
 
END
 
 
Display output

This approach can help you understand the problem better and will also help you catch things you missed. Look at the algorithm here. Do you see anything important that I left out?

I forgot to convert the user’s input from a string to a number before I compared the values. Some languages would catch that by erroring out, but other languages would keep on going, resulting in a logic error. By writing up a flowchart and pseudocode, I was able to communicate my intention to you quickly so you could see if there were any missing steps or flaws in my logic before I spent any time writing code.

As you work through the exercises in this chapter, try to use flowcharts and pseudocode to determine the algorithm for the program, and then turn it into code.

You’ll start out solving problems by making simple decisions, such as “if this happens, do this.” Next you’ll look at how to handle either-or situations. Then you’ll have to solve problems that get more complex, where the result of one decision raises another decision. That’s where these planning tools will come in handy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.210