// Lecture 18: If-Then and If-Then-Else Examples
// EXAMPLE PROBLEM STATEMENT:
// Implement 'if-then-else' where int num1 = 56 and int num2 = 65,
// and print whether they are equal or not
// EXAMPLE SOLUTION:
// if (num1 == num2) {System.out.print("56 is equal to 65");}
// else {System.out.print("56 is not equal to 65"); }
// 56 is not equal to 65
Hi there, I am Marius from AlefTav Coding. In this lecture you will learn how to use the 'if-then' and the 'if-then-else' expressions in Java.
'If-then' is a control flow statement where Java executes code only if a condition is true. Here, these 2 numbers 21, are stored at different locations. Java looks at the locations where these 2 numbers are stored, compares the numbers and prints the statement that they are 'equal'. IF the first location, 'num6' is equal to the second location, 'num7' THEN print that they are equal.
With 'if-then-else', Java executes another piece of code if the first condition is false. Here are another 2 numbers, 56 and 65, and again, they are stored at different locations. Java looks again at the 2 locations and compares the numbers stored there. It then prints the statement that they are 'not equal', and simply ignores the code that reads, 'equal'. IF the first location, 'num1' is equal to the second location, 'num2' THEN print they are equal, ELSE print that they are not equal.
Let's look at our problem statement. 'Implement if-then-else where int num1 is assigned to the value 56, and int num2 is assigned to the value 65 and print whether they are equal or not. Looking at what I expect to see printed, it should be that they are not equal, here, because 56 and 65 are different integer values. My example solution is, IF num1 equals equals num2 THEN print 'equal', ELSE print 'not equal'.
The next part of the requirement is to do a printout and say if they are equal. These 2 values are not equal so my printout should read, as here in the expected solution, that 56 is not equal to 65.
Now, to complete my program, I have a class, 'Lecture18' with a 'main' method. Then, here are declared and initialized 'num1' and 'num2'. And the 2 control flows, the first is to print 'equal' and the second control flow is to print 'not equal'. All that is left is to print the 'if' statement with the one condition, in round brackets num1 equals equals num2. And the reason I am using the equals equals symbols is that I am comparing 2 primitive dataTypes, int. Everything is in order. Now, let me run my program to test my code. I want to see this output displayed here. And there is the expected output.
In this lecture you've learned to use Java's if-then-else control statement.
Now, complete this coding exercise to practice what you've learned.
// IF-THEN-ELSE CODING EXERCISE:
// Implement 'if-then-else' with testScore >= 60
My name is Marius from AlefTav Coding. Till next time, KEEP CODING.
You can find more tutorials at the links that follow:
https://busy.org/@mariusclaassen
https://steemit.com/@mariusclaassen
https://d.tube/#!/c/mariusclaassen
https://bit.tube/mariusclaassen
▶️ DTube
▶️ IPFS