In this tutorial I show you the if statement, relational operators, logical operators, ternary operator and the switch statement.
If you haven’t watched part 1 watch it first here Java Video Tutorial.
As you watch this video, you will be able to follow it easier if you have the commented code in front of you. It follows the video below.
While this tutorial has been slow it will get much more complicated quickly. So, make sure you understand everything I cover in the beginning.
If you like videos like this share it
Code From the Video
public class LessonThree { public static void main(String[] args) { // Creates a random number between 0 and 50 int randomNumber = (int) (Math.random() * 50); /* Relational Operators: * Java has 6 relational operators * > : Greater Than * < : Less Than * == : Equal To * != : Not Equal To * >= : Greater Than Or Equal To * <= : Less Than Or Equal To */ // If randomNumber is less than 25, execute the code between {} and then stop checking if (randomNumber < 25) { System.out.println("The random number is less than 25"); } // If randomNumber wasn't less than 25, then check if it's greater than it. If so, execute the code between {} and then stop checking else if (randomNumber > 25) { System.out.println("The random number is greater than 25"); } // Checks if randomNumber equals 25 else if (randomNumber == 25) { System.out.println("The random number is equal to 25"); } // Checks if randomNumber is not equal to 25 else if (randomNumber != 15) { System.out.println("The random number is not equal to 15"); } // Checks if randomNumber is less than or equal to 25 else if (randomNumber <= 25) { System.out.println("The random number is less than or equal to 25"); } // Checks if randomNumber is greater than or equal to 25 else if (randomNumber >= 25) { System.out.println("The random number is greater than or equal to 25"); } // If none of the above were correct print out the random number else { System.out.println("The random number is " + randomNumber); } // Prints out the random number System.out.println("The random number is " + randomNumber); /* Logical Operators: * Java has 6 logical operators * ! : Converts the boolean value to its right to its opposite form ie. true to false * & : Returns true if boolean value on the right and left are both true (Always evaluates both boolean values) * && : Returns true if boolean value on the right and left are both true (Stops evaluating after first false) * | : Returns true if either boolean value on the right or left are true (Always evaluates both boolean values) * || : Returns true if either boolean value on the right or left are true (Stops evaluating after first true) * ^ : Returns true if there is 1 true and 1 false boolean value on the right or left */ if (!(false)) { System.out.println("I turned false into true"); } if ((false) && (true)) { System.out.println("\nBoth are true"); } // There is also a & logical operator it checks the second boolean result even if the first comes back false if ((true) || (true)) { System.out.println("\nAt least 1 are true"); } // There is also a | logical operator it checks the second boolean result even if the first comes back true if ((true) ^ (false)) { System.out.println("\n1 is true and the other false"); } int valueOne = 1; int valueTwo = 2; // The Conditional or Ternary Operator assigns one or another value based on a condition // If true valueOne is assigned to biggestValue. If not valueTwo is assigned int biggestValue = (valueOne > valueTwo) ? valueOne : valueTwo; System.out.println(biggestValue + " is the biggest\n"); char theGrade = 'B'; /* When you have a limited number of possible values a switch statement makes sense * The switch statement checks the value of theGrade to the values that follow case * If it matches it executes the code between {} and then break ends the switch statement * default code is executed if there are no matches * You are not required to use the break or default statements * The expression must be an int, short, byte, or char */ switch (theGrade) { case 'A': System.out.println("Great Job"); break; // Ends the switch statement case 'b': // You can use multiple case statements in a row case 'B': System.out.println("Good Job, get an A next time"); break; case 'C': System.out.println("OK, but you can do better"); break; case 'D': System.out.println("You must work harder"); break; default: System.out.println("You failed"); break; } } }
nice tutorial, keep it up…
Another one will be done tomorrow
if possible, please add some videos for java frameworks(i.e Struct’s, spring etc.) in this java video tutorial series..
Thank you
System.out.println(‘Yeah’);
//doesnt work with single quotes. Why so?
When you use single quotes you can only use one character. That’s just the way the language works. I don’t particularly like it either
In java once and for all a character is delimited by 2 quotes (”) and a string of characters is delimited by 2 double quotes (“”)… I know related to other languages it can be confusing…
I have to just drop a comment to say thanks for these videos. They have taught me more in a month than I have learned from any book with the last year. I really appreciate that you go beyond the basics and cover enough material to help both a beginning programmer and an experienced programmer.
You’re very welcome 🙂 It makes me very happy to hear that I’m helping so many people. Many more videos are in the works. I won’t be done with Java for at least another year
Love the videos … Learning as we speak. I did notice that this will not call unless you use an if statement instead of else if inside the the original if loop
// Checks if randomNumber equals 25
031
else if (randomNumber == 25)
032
{
033
System.out.println(“The random number is equal to 25”);
034
}
I just made this stream of code to talk about what each of the conditionals do. I guess it just comes naturally to talk while writing the code?
Talk it out … 🙂 works for me !
Great vid! I have more questions! Is it necessary to have a break; in the default case? Or any of the other cases for that matter? I’d assume it would continue to check the other cases and nothing would happen. Would having the switch do those unnecessary checks be something that would affect the performance of the program overall?
Are Conditional operators better for a program’s performance? If I was programming I would have been thinking to write
If (valueOne > valueTwo)
{
int biggestValue = valueOne;
}
else {
int biggestValue = valueTwo;
}
rather than
int biggestValue = (valueOne > valueTwo) ? valueOne : valueTwo;
Yes you don’t need the break and you are correct in regards to what would happen without it. The compiler is normally smart enough to handle any performance issues that come up depending on which conditional tools you use.
Great video! I have question, a lot of people said that in order to become master in programming you have to practice coding a lot, and make a program. and I want to practice this, but I dont have any idea what to program, or what to build with the basic java that I have gained from your tutorial. I really want to improve my programming skill. Do you have any suggestion ? thank you!
Thank you 🙂 You may benefit from watching my object oriented design tutorial. It covers how to turn a problem into finished code. You don’t have to completely understand the code. What is more important is understanding the mindset of programming. Feel free to ask more questions.
int randomNumber = (int) (Math.random() * 50);
Can you explain how does this represent numbers 1-49? I thought it would take a random number and multiply it by 50.
Math.random returns a number between 0.0 and less than 1.0. Then I force it into being an int which gives me a number between 0 and 50
Hi, really nice tutorials, still learning.
I noticed that on this website code is a little bit weird, comments really don’t work the best, almost all code i green. Is it the website or is it a mistake? But still great work. Keep it up.
Thank you 🙂 I’m not sure what you mean by the code is green. I’ll do my best to help.
So, would I need papers, a calculator, and patience when coding? Java acts like a pet dog to you, and to me it is like deciphering binary 1’s and 0’s. Also, what can I do to practice? Any questions or practice problems somewhere?
It will come with practice. It is best to print out the code and take notes in your own words. Write down questions you have and I’ll answer them. It may help you dramatically to learn UML. I have a UML tutorial. Many years ago after learning UML everything started to make sense. Also in the beginning you’ll copy and paste code and heavily use code completion. You’ll get it 🙂