Reputation: 37
I am working on a question for my Computer Programming class in high school and the question is as follows.
Using a conditional statement, generate a program in any coding language and explain the working of it: “If the temperature of your room is more than 80° or less than or equal to 60°, then the program should branch; otherwise, the execution should continue without branching.”
I have managed to create a conditional statement on my own, but I do not understand the branching part. I have Googled already and have found nothing useful. Can someone explain what I am supposed to do?
**Also, I chose JavaScript for my coding language.
function temperature(z) {
if(z > 80 || z <= 60) {
/*branch program?*/;
}
else {
/*do not branch program?*/;
}
}
console.log(temperature(81)); /*evaluates to branching*/
That is all I have so far. Also, is there a simpler way to write that code? I would love some opinions!
Upvotes: 2
Views: 1010
Reputation: 471
The if statement itself is a branching instruction to your CPU where in assembly language CPU would’ve taken that instruction as a possibility to skip set of instructions inside the if statement and continue after it (branching) or execute that piece of code within the normal flow. I believe that the statement you wrote is already a perfect example of the question, you should just be able to change some variable inside that if where that exact variable would’ve taken different value if the program were to take different execution path (aka branch).
Edit:
You could end up with code looking something like this:
function getTemperatureFeel(t) {
var feel = "Perfect"; // this is straight execution
if (t > 80 || t <= 60) {
feel = "Either warm or cold"; // this is branch execution
}
return feel;
}
console.log(getTemperatureFeel(81));
Upvotes: 1