Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 7

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 6 | Lesson 8 >>

_______________________________________

Lesson 7: Control Structures in Java – IF statement

java-programming-a-step-by-step-tutorial-for-beginners-lesson-7

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:

if(Condition) {

}

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.

java-programming-a-step-by-step-tutorial-for-beginners-lesson-7

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

OperatorDescription

>

Is Greater Than

<

Is Less Than

>=

Is Greater Than or Equal To

<=

Is Less Than or Equal To

2. Logical operators

SymbolDescription

&&

AND

||

OR

==

HAS A VALUE OF

!

NOT

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

java-programming-a-step-by-step-tutorial-for-beginners-lesson-7

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.

java-programming-a-step-by-step-tutorial-for-beginners-lesson-7
java-programming-a-step-by-step-tutorial-for-beginners-lesson-7
java-programming-a-step-by-step-tutorial-for-beginners-lesson-7
java-programming-a-step-by-step-tutorial-for-beginners-lesson-7

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.


<< Lesson 6 | Lesson 8 >>

Related hubs...

Want to make money online?

Comments

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.

much appreciation.

Related Articles