This is in no way the official description of If Statments, this is my description based on personal experience. If statments are used to check things, then react on the outcome. With If statements you can use booleans which we covered in the last tutorial. When programing, you make the code react to the person using the program.
How To Setup/Use:
When your coding Java, you will always use If Statements.
This is how a If statement looks:
Now, lets make it where a boolean controls the if statement
This is saying: If name is true, then I will perform the things between the brackets. That's how I look at this, makes things a lot easier.
Now if I wanted to a string say "Im on" when the boolean is true then you do this:
So right now, I have the boolean automaticly true, so when the program is ran it will make the string name2 equal 'Im on'. Now what happends if we set the boolean false. Nothing! Nothing will happen because its not checking to see if the boolean is false. It only cares if it is on as of right now. Say we want to check to see if the program is off. All we need to do is add a '!' before it. When you add '!' it tells it to check for the opposite.
Now the if statement is saying: if name is false, then I will do the following code in the brackets. Only thing now is its not checking to see if its on. We could make two if statments one with a '!' and one without it, but thats too much work.
That right there checks to see if name is on, if it is then it will make the String name2 equal 'Im on'. If the name isn't on, then it will do what is in the else statement.
This is how I look at it. If name is on we will do this, anything else we will do this. 'This' meaning inside the brackets.
There is a lot you can do with if statements, but that's all you need to know for now. You need to learn other basic stuff before we go into making a program together. From there I can go more into depth with booleans and if statements. Be sure to check my other tutorials!
How To Setup/Use:
When your coding Java, you will always use If Statements.
This is how a If statement looks:
PHP:
if( Statement ){
}
PHP:
public boolean name = true; //You have to declare it first before using it.
if(name){
}
Now if I wanted to a string say "Im on" when the boolean is true then you do this:
PHP:
public boolean name = true;
public String name2;
if(name){
name2 = "Im on";
}
System.out.println(name2);
PHP:
public boolean name = true;
public String name2;
if(!name){
name2 = "Im off";
}
System.out.println(name2);
PHP:
public boolean name = true;
public String name2;
if(name){
name2 = "Im on";
}else{
name2 = "Im off";
}
System.out.println(name2);
This is how I look at it. If name is on we will do this, anything else we will do this. 'This' meaning inside the brackets.
There is a lot you can do with if statements, but that's all you need to know for now. You need to learn other basic stuff before we go into making a program together. From there I can go more into depth with booleans and if statements. Be sure to check my other tutorials!