Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.
Lesson 7: Control Structures in Java – IF statement
Since we started we’ve been writing programs that run in a linear manner. That is, when we run the program, each line of code will be executed, starting with the first line all the way to the last line.
In most cases we would like our codes to be executed only when certain conditions are met. For example, in a payroll, we would like to add an entertainment allowance to an employee salary only if that employee is a manager and not add for every employee. So we’ll have to think along the following lines:
IF employee status = “manager” then add allowance
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. Later on we shall see how we can handle this situation where the condition may be FALSE.
The structure of the IF Statement in Java is shown below:
You type if (notice it is in lowercase), then inside the brackets you specify the condition. After that, inside the braces you specify the codes to be executed if the condition is TRUE.
Let us try a working example. You’ll need to create another class, call it Mycondition.
What we’ll do is that we’ll let the user to enter a number in a text box and then use if statement to check whether that number is a negative number or a positive number.
How? We know that a positive number is greater than zero and a negative number is less than zero, so what we shall check is whether the number entered by the user is greater than zero or less than zero. For each case we shall output the appropriate text message. We’ll also use comparison and logical operators and here they are;
Operators in Java
1. Comparison Operators
Is Greater Than
Is Less Than
Is Greater Than or Equal To
Is Less Than or Equal To
2. Logical operators
HAS A VALUE OF
Create a string variable, call it num, to hold the input entered by the user and another, call it num2, to store the converted int value. We shall then do the comparison. Your code should look like shown below:
Java program to determine positive and negative numbers
From the above code you can see we have used two IF statements to check whether the number is less than zero (i.e Negative) or greater that zero (i.e Positive). Notice that this time we have used comments to document what is happening in the IF statements.
Remember: Every opened curly brace must match with a closed one to make a pair.
Run the program and you should be able to get an appropriate output in each case. Try with positive and negative numbers.
Note that it is not a must you use message boxes for inputs and outputs, you can also use the output window as we saw earlier. In the next lesson we shall look at IF..ELSE statement.
- 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.
Want to make money online?
edwin on August 25, 2012:
Hey,this far enlightening and encouraging to beginners. Thanx for this research and hopping much of it will give better glimpse.