Okay. I assume you've read and understand the topic i posted about what you need to do before you even attempt any of my tutorials.
If you havent, Please read it
here so i dont have to worry about useless errors -_-
______________________________________________________________________
In this tutorial, We will be studying the if-then statement and if-then-else statement.
They sound A LOT harder than they actually are.
Basically, what it means is it will test the data using whats called a logical operator, and if it comes back false, it moves to the next test(the next if statement). And if it comes back true, it goes to the then statement.
This tutorial's scenario is a school test grade. We really need to figure out what letter grade we have, But for some reason we just cant remember what letters go with what numbers. (LOL...). I'm not sure what kind of grading system anyone has where they live so i will just use the system we have here.
We have the letter grades, A, B, C, and F
The numerical grade that corresponds with them, is a=90+, b=80+, c=70+, f=69-.
So, We start off naming our class.
I'm calling mine IfElseTut for short and simplicity.
So, Here we go~
- Code:
-
class IfElseTut{
There goes the easy part~
Now, We do that seemingly pointless line,
- Code:
-
public static void main(String[] args){
So far we should have
- Code:
-
class IfElseTut{
public static void main(String[] args) {
Now, We start the fun part
But, before we start this, I think this is a good time for a lesson on our operators:
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
!= NOT equal to
== Equal to.
The only thing i think i need to stress here is that "=" is different than "==". "=" is used when assigning values. "==" is comparing those values.
SO
Now that that is over, We should start out by declaring our test score as an integer.
Lets say we got an 83 on the test.
- Code:
-
int testgrade = 83;
But, we've come across a problem...
We cant set a letter as an integer o__O
To fix that, we have to create another type of variable. A "char", stands for character.
SO,...
- Code:
-
char grade;
This snippet will work.
Now, heres what you should have:
- Code:
-
class IfElseTut {
public static void main(String[] args) {
int testgrade = 83;
char grade;
Now, heres where we start the if-else statements.
- Code:
-
if (testgrade >=90){
grade = 'A';
}
As you can see, its kind of simple, its comparing the value of testgrade to see if its at least 90. If it is above 89, you have an "A". :}
I"m just going to finish the rest myself. Its basically the same as that. just put else each time after,...Before "if" that way, it will keep trying to find a match and if it doesnt, it goes to the last else statement and sets the grade as an F.
- Code:
-
if (testgrade >= 90) {
grade = 'A';
} else if (testgrade >= 80) {
grade = 'B';
} else if (testgrade >= 70) {
grade = 'C';
} else {
grade = 'F';
}
I feel like everyone is going to be lost by now...So i'll post what you should have.
- Code:
-
class IfElseTut {
public static void main(String[] args) {
int testgrade = 82;
char grade;
if (testgrade >= 90) {
grade = 'A';
} else if (testgrade >= 80) {
grade = 'B';
} else if (testgrade >= 70) {
grade = 'C';
} else {
grade = 'F';
}
Okay. NOW, Now that it has found a match, we need to tell the program what to do with the match. We'll simply tell it to type something
- Code:
-
System.out.println("Grade = " + grade);
As you can see, This prints out Grade =, Then it adds the value of the char grade.
So, put that in your code, and you should have
- Code:
-
class IfElseTut {
public static void main(String[] args) {
int testgrade = 82;
char grade;
if (testgrade >= 90) {
grade = 'A';
} else if (testgrade >= 80) {
grade = 'B';
} else if (testgrade >= 70) {
grade = 'C';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
THATS IT!
Just close your brackets and you're done!
Heres a finished copy:
- Code:
-
class IfElseTut {
public static void main(String[] args) {
int testgrade = 82;
char grade;
if (testgrade >= 90) {
grade = 'A';
} else if (testgrade >= 80) {
grade = 'B';
} else if (testgrade >= 70) {
grade = 'C';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
~Zachy