Lecture 19: If-Then-Else Solution
if-then-else coding exercise
Implement 'if-then-else' with testScore >= 60
if-then-else solution
if (testScore >= 60)
if-then-else output
pass
Hi there, I am Marius from AlefTav Coding with blog post, Lecture 19 of my course, Java for Beginners. In this lecture I will show you the solution to the if-then else coding exercise.
public class Lecture19 {
public static void main(String[] args) {
int testScore = 74;
String result = "undefined";
// TODO: implement 'if-then-else with testScore >= 60'
{
result = "pass";
} else {
result = "fail";
}
System.out.print(result);
}
}
You were asked to Implement 'if-then-else with testScore >= 60'. In my I use an if statement with only one condition. If the value of testScore is larger than or equal to 60.
To complete my program, I have a class, Lecture19 with one method, main(). Then I have declared and initialized an int named testScore as the value, 74 and a String named, result as undefined. Looking further, my program has two control flows.
Firstly, assign the String named, result to be the value, pass if the value of testScore is greater than or equal to 60. The second control flow is to assign the String named result to fail if the value of testScore is less than 60. The value of testScore is initialized here to 74 which is greater than 60. Therefore result will be assigned to the String value, pass. So, I expect to see a printout of the String pass.
I can now type the expected solution by removing the TODO comment with the code, if (testScore >= 60)
When I run my program, Java is looking for the method, main(), the starting point of the program. So, to run my program I need to make sure Java can find main() to execute this code. The outcome is that I will see the value pass printed.
In this lecture you learned the solution to the if-then-else coding exercise.
I am Marius, from AlefTav Coding. Till the next time, KEEP CODING.
Please feel free to upvote, subscribe, like and share. You can find me at the following links.
https://busy.org/@mariusclaassen
https://steemit.com/@mariusclaassen
https://d.tube/#!/c/mariusclaassen
https://bit.tube/mariusclaassen