- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 8
Table of Contents
<< Lesson 7 | Lesson 9 >>
_______________________________________
Lesson 8: Control Structures in Java – IF ... ELSE statement
Welcome to Lesson 8 of Programming in Java. In Lesson 7, we learnt about the IF statement and we also did one practical example to compare whether a number is positive or negative.
We also learnt about various Java programming operators, how we use them in a condition and we actually used some of these operators in our examples. Remember the code we had in Lesson 7? Below, have a look at it again.
Java program to determine positive and negative numbers
As you can see, we had to use two IF statement to get our output. Another better option is to use IF .. ELSE statement. The structure of IF .. ELSE statement look as shown below:
if (condition) {
}
else {
}
The first part of IF .. ELSE statement has the condition you want to test for. Inside the curly braces you put the code to be executed if the condition is TRUE. In the second part, the else part, you put the code to be executed inside the curly braces if the condition is FALSE.
So, IF .. ELSE statement has two parts, one to cater for when condition is TRUE and another for when condition is FALSE. Modify your code to look like shown below and run the program.
Java program to determine positive and negative numbers
package myfirstprogram; import javax.swing.JOptionPane; class MyCondition { public static void main(String[] args) { String num; int num2; num = JOptionPane.showInputDialog("Enter a number: "); num2 = Integer.parseInt(num); if(num2 < 0){ //Check if number is negative JOptionPane.showMessageDialog( null,"The number is Negative"); System.exit(0); } else{ //If it wasn't negative, then it is positive JOptionPane.showMessageDialog( null,"The number is Positive"); System.exit(0); } } }
IF .. ELSE IF Statement
There may be situations where we would like to test many conditions at the same time. Take for example the grading of students marks. Suppose we want to grade students according to the following grading system:
Student Marks Grading System
Marks
| Grade
|
---|---|
80 - 100
| A
|
60 - 79
| B
|
50 - 59
| C
|
30 - 49
| D
|
0 - 29
| FAIL
|
To grade the marks according to the above given grading system, you’ll need several IF .. ELSE IF statements. You’ll also need to use a logical operator. Let us create another class, call it Grading.
We are going to let the user enter the marks in a text box. We’ll then convert the user input from string to int and use the int value to find the grade using IF .. ELSE IF statement.
Type the following code carefully and let’s see what it will do. You can create your own variables or just use the one I have used. Pay attention to curly braces.
Java program to grade students' marks
package myfirstprogram; import javax.swing.JOptionPane; public class Grading { public static void main(String[] args) { String user_marks; int marks; //Get marks from the user and convert from string to int user_marks = JOptionPane.showInputDialog("Enter marks: "); marks = Integer.parseInt(user_marks); //Compute the grade and ouput the grade if(marks >= 80 && marks <= 100){ JOptionPane.showMessageDialog( null,"Grade = A"); } else if(marks >= 60 && marks <= 79){ JOptionPane.showMessageDialog( null,"Grade = B"); } else if(marks >= 50 && marks <= 59){ JOptionPane.showMessageDialog( null,"Grade = C"); } else if(marks >= 30 && marks <= 49){ JOptionPane.showMessageDialog( null,"Grade = D"); } else if(marks >= 0 && marks <= 29){ JOptionPane.showMessageDialog( null,"Grade = FAIL"); } } }
In the above code, we are getting the input from the user as user_marks. We are then converting the user_marks from string to int and assigning it to another int variable called marks.
It is the value in the marks variable that we are using in the IF .. ELSE IF statement to find if it is within our grading system, and if it is, we output the correct grading statement in a message box.
We are also using the && (AND) logical operator to factor in the range of our value. Now, run the program and see if it will output the correct grade. Try with different values of inputs.
Notice that you won’t get any message if you enter a value greater than 100 or less than zero. This is because those values are not within our grading system and our program does not recognize them.
Exercise:
Modify the code so that when you enter a value greater than100 or less than zero the program output an appropriate message.
Nested IF statement
Nested IF just means an IF statement inside another IF statement. Suppose we want to admit a movie goer into the movie theatre only if he/she is at least 18 years of age and only if he/she has paid the entrance fee. We can use the following Nested IF statement:
If(age >= 18){
If(entrance fee == “Paid”){
JOptionPane.showMessageDialog( null,"Allow into movie theatre");
}
}
Nesting can also be applied in IF .. ELSE and IF ... ELSE IF statements.
Exercise: Write a program of your choice that will implement Nested IF statement.
Boolean values in Java
A Boolean value is one with two choices: true or false, yes or no, 1 or 0. In Java, there is a variable type for Boolean values and that is boolean type. So if we want to specify that a variable paid is Boolean and assign a value of true into it, we’ll write like follow:
Boolean paid = true;
If we need to use the variable paid, we can write a code like shown below:
if (paid == true) {
System.out.println("it's true");
}
else {
System.out.println("it's false");
}
Notice that the assignment operator is a single equals sign ( = ), but to check if a variable "has a value of" something, you need two equal signs ( = =).
In our next lesson, we shall see what is a switch statement and how we can use switch statement in Java.
<< Lesson 7 | Lesson 9 >>
Related hubs...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. Switch statement can also be used instead of IF .. ELSE ... - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 3
In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 7
IF statement executes the code only if the condition specified is met i.e. is TRUE, otherwise if the condition is not met, i.e. it is FALSE, the code will not be executed. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 4
In Lesson 3, we learnt how to write a Java program in the Java code window and how to Run a Java program. In this lesson, we shall learn how to work with variables in Java and how to manipulate computer memory using variables.