- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 11
Table of Contents
<< Lesson 10 | Lesson 12 >>
Lesson 11: Control Structures in Java – The WHILE Loop
Welcome to Lesson 11 of Programming in Java. We’ll continue looking at more loops control structures in Java and for this particular lesson, we shall look at the WHILE loop.
The WHILE loop execute a statement or a group of statements so long as the specified condition remains TRUE.
The WHILE loop in Java has the following structure:
while( condition ) {
}
The loop starts with the keyword “while”. Notice the keyword is in lowercase. Just after the keyword “while” you open the round brackets and specify the condition to be tested and then you close the round brackets.
The code statements to be executed if the condition turns out to be TRUE goes inside the curly braces.
Let us write a program to output the text “This is a while loop” for ten times on the console window. Go to the NetBeans code window, create a new class, call it whileloop. Type the code as shown and run.
In the above code, we’ve created an int variable called count that will loop from 1 to 10. As it does that, we’ll keep on checking if count is less or equal to 10 and if TRUE, we output out text output. The output will be done ten times whereby after that the condition turns out FALSE and the loop stop.
Now, let us work with a more practical example. Suppose we want to check whether a user has entered the correct password? We are going to set our own password and then let the user enters his/her password.
If the password does not match the one already set, we output an appropriate message using Java Option Pane, and if he/she enters the correct password we also output the appropriate message.
It will be a good programming practice if we can let the user attempt several times to enter the password. We’ll set attempts to three times after which we’ll let the user know that our program has been locked. To do this, we’ll need a WHILE loop, an IF ..ELSE statement and the method called equals(). By now, you should be able to combine several control structures in Java.
Create another Java class, call it Password and type the following code. Pay attention to curly braces
Java program using WHILE loop and IF..ELSE statement
package myfirstprogram; import javax.swing.JOptionPane; public class Password{ public static void main(String[] args) { int count; count = 1; String mypass,yourpass; mypass = "user"; while(count <=3){ yourpass = JOptionPane.showInputDialog("Enter password: "); if(yourpass.equals(mypass)){ JOptionPane.showMessageDialog(null,"Correct password!"); count = 3; }else{ JOptionPane.showMessageDialog(null,"Attempt:"+count+" Incorrect password!"); if(count == 3){ JOptionPane.showMessageDialog(null,"System locked!"); } } count++; } } }
In the above code, we have created one int variable called count and two string variables; mypass and yourpass. We’ve set our password to be “user” and stored that in mypass variable.
Using the WHILE loop, we let the user attempts three times to enter the password and each time checking whether the password entered matches the set password. To do this, we are using IF .. ELSE statement inside the WHILE loop. In each case we output the appropriate message. If the user enters the wrong password for the third time, we lock (end) the program and output the appropriate message.
Run the program, attempt to enter wrong password three times and see what will happen.
Okey, that’s all for the WHILE loop. In the next lesson, we shall look at the DO ..WHILE loop in Java.
<< Lesson 10 | Lesson 12 >>
Related hubs...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 1
Java is increasingly becoming the world’s programming language of choice. It has been used in developing desktop applications, client-server applications and even mobile phone applications including games. - 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 6
In this lesson we’ll learn about another useful class for accepting user inputs and displaying results. This class is called JOptionPane class and is located in the javax.swing library. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 5
One of the useful classes that handle inputs from a user in Java is the Scanner class. The Scanner class is located in the util (utility) package of the Java library.