Java programming for beginners - Lesson 3 - IF Statements
Hello everyone, welcome to the third lesson in my Java programming for beginners tutorials series. The fourth lesson(lesson - 3) - Operators can be found at - https://steemit.com/programming/@robertlyon/java-programming-for-beginners-lesson-2-operators
In this lesson, I am going to cover IF statements, where I will show you how your programs can make decisions based on the conditional statements that I introduced in the previous tutorial.
IF STATEMENTS
What are they?
So, an IF statement is a way for you to make a decision in your code. For example, if you had a character in a video game and you wanted to make that character move left or right, your program will need to make some kind of decision, based on the input for the player. An example of this would be.
IF player presses right
move the character right
IF player presses left
move the character left
What I have written above is what is called Sudo Code. This is code logic that is written in English but is structured in a way similar to code.
As you can see above there are two options for the program to follow, in reality, the code to perform this action would be more complicated but it is good enough for this example.
The first line of the sudo code checks to see if the player has pressed the right key on the keyboard, if they have pressed it then the indented code below will execute and the character will move to the right which is the code shown on the second line.
The third line checks to see if the player has pressed the left key on the keyboard, if they have pressed it then the indented code on line 4 executes and the character will move to the left.
Structure of an IF statement.
we will now look at how we would code an if statement, have a look at the code below and try and work out for yourself what the answer is. The answer will be included in the picture below the code snippet.
if(6 > 7)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
As you can see above the answer to this problem is true. If you didn't get the answer, don't worry we will now dissect the program line by line.
if(6 > 7)
so line on starts off with the keyword if, this is the keyword that lets the java compiler know that you are using an IF statement. The next item of interest on the list is the parentheses(), these are a mandatory part of the if statement and are used to hole the conditional statement. This brings us to the last part of the line which is the conditional statement itself. If you think back to the previous lesson then you will know that 6>7 is in fact false.
The next line is the {. This curly bracket indicates the start of a block of code. Each opening curly brace has a corresponding closing brace. Both of these braces create the block of code for each specific statement so the braces on lines 2 and 4 create the block for the IF part of the statement and the braces on lines 6 and 8 create the block for the ELSE part of the statement.
Line 3 is a statement. This statement causes the word "true" to be printed to the console. This is where the IF statement gets interesting. This statement only executes if the conditional statement in the parentheses is true(6 > 7). As 6 is never greater than 7 this statement never gets executed.
This brings us on to the else part, the else keyword is used here. Notice that the else part of the statement does not have any condition that needs to be met. This is because the else statement can be thought of as a safety net. If the IF statement is not true then the else statement will get executed as it catches any other conditions.
An important point to note on this is that the else statement does not execute if the IF statement is true.
Nested IF Statements
Now the logic of the IF statement allows us to make a number of decisions in our code by using what is known as nested if statements. This is simply just an IF statement within an IF Statement. Have a look at the code below and try to work out for yourself what the answer could be.
if(6 == 6)
{
if(6 > 5)
{
System.out.println("true");
}
else
{
System.out.println("false2");
}
}
else
{
System.out.println("false1");
}
As you can see this is similar to the example above. We start with a comparison which checks to see if the number 6 is equal to the number 6. Because this is equal to true the if statement moves into it's code block.
The first line of this code block is another IF Statement. In this IF statement we check to see if 6 is greater than 5. Because this is true the answer "true" is printed to the console. Had this comparison been false then the if statement inside this code block would have been executed and "false2" would have been printed to the console.
If the condition in the first IF Statement had failed then the program would have jumped to the else statement related to this IF statement and "false1" would have been printed to the screen.
IF, ELSE IF, ELSE Statements
So as you can see nested if statements are a way of making multiple decisions in your code, but there is a better way to do this. Have a look at the code below to see how this works.
if(6 == 7)
{
System.out.println("1");
}
else if (6 > 4)
{
System.out.println("2");
}
else
{
System.out.println("3");
}
As can be seen above the first IF statement checks to see whether or not 6 is equal to 7. As this is false the program moves on to the ELSE IF statement. This checks to see whether or not 6 is greater than 4. This, of course, is true
Conclusion
So in this lesson i have covered the logic of IF Statements. You can now take this basic understanding to the next level by practacing on your own and experimenting with the statements and the logic to see what you can produce.
Next Lesson
The next lesson in this series will be focusing on Switch statement. These are similar to IF Statements in the way that they are used for making decisions in programs.
If there is anything in this lesson that confuses you or there is anything programming related that you need help with then please comment below and I will try my best to help you.
As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.
Thank you for reading and I hope that someone will get some use out of these tutorials.
Message to readers
Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.
This post received a 41% upvote from @morwhale team thanks to @robertlyon! For more information, click here! , TeamMorocco! .
Resteemed by @resteembot! Good Luck!
Curious?
The @resteembot's introduction post
The @reblogger's introduction post
Get more from @resteembot with the #resteembotsentme initiative
Check out the great posts I already resteemed.
nice post
thanks :)
nice education i like it
Thanks :)
This post has received a 4.32 % upvote from @buildawhale thanks to: @robertlyon. Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.
To support our daily curation initiative, please vote on my owner, @themarkymark, as a Steem Witness