Lucky Cat Tutoring Services


CIT 111: While Loops
September 17, 2009, 3:59 pm
Filed under: Uncategorized | Tags: , , , ,

While loops mean “while this statement is true, do this. When it’s false do something else. A loop is a part of a program that repeats until a certain need is met
Every “WHILE” loop that you write has certain parts. If you forget these, you can end up with an infinite loop and you don’t want this. Infinite loop goes on forever (the dreaded hourglass in windows, the spinning pinwheel of death in mac…) and THIS IS BAD! Don’t be like Microsoft, loop well.

With all loops:
Initialize
Test
Update

Make sure not to close your loop prematurely with a “;”

Ex:

while (number MAX_INT) // NO “;” IN A WHILE LOOP; && cannot work for this, instead use OR which is || {
JOptionPane.showMessageDialog(null,”You must not be able to read. Try again, Ceiling Cat is watching! “);
inData = JOptionPane.showInputDialog(” Please enter a number from 1 to 10″);
number =Integer.parseInt(inData);//make string a number datatype
}

Lets look at this little program:

/**
This program demonstrates the while loop.
*/

public class WhileLoop
//while is a KEY WORD and has special meaning. “While” is followed by a boolean expression inside a {}
//while “this bit of bode is doing its thing and this condition is true, then do this command. when it’s FALSE stop doing.

{
public static void main(String [] args)
{
int number = 1; //this INITIALIZES

while (number <= 5) //this TESTS
{
System.out.println("Hello");
number++;//this UPDATES the loop
}

System.out.println("That's all!");
}
}

Keep in mind that {}s are your friends. They link bits of code together like putting beads in different boxes in a kit. Lets do a tracethrough of this little program:

Number is made to be a value of one.
{Test: is NUMBER less than or = to 5?
yes it is…lets do our loop. Say “hello!”
Add 1 to Number. Number is now 2. Lets go again….}

Number is made to be a value of 2.
{Test: is NUMBER less than or = to 5?
Yes it is lets do our loop. Say “hello!”
Add 1 to Number. Number is now 2. Lets go again….}

Number is made to be a value of one.
{Test: is NUMBER less than or = to 5?
yes it is…lets do our loop. Say “hello!”
Add 1 to Number. Number is now 3. Lets go again….}

Number is made to be a value of 3.
{Test: is NUMBER less than or = to 5?
yes it is, lets do our loop. Say “hello!”
Add 1 to Number. Number is now 4. Lets go again….}

Number is made to be a value of 4.
{Test: is NUMBER less than or = to 5?
yes it is, lets do our loop. Say “hello!”
Add 1 to Number. Number is now 5. Lets go again….}

Number is made to be a value of 5.
{Test: is NUMBER less than or = to 5?
yes it is, lets do our loop. Say “hello!”
Add 1 to Number. Number is now 6. Lets go again….}

Number is made to be a value of 6.
{Test: is NUMBER less than or = to 5?
NO! TIme to get out of the loop
Print “that is all” and it’s done }

What if a part is missing. Take out the “UPDATE” method and let it run…you’re going to be there for a while. You’ve made yourself a crash!

PRACTICAL APPLICATIONS OF LOOPS
1) Prompting the user to enter in a value until it’s correct. Insure some data integrity; make sure the data is in range before it does anything with it
2)Use as a log
And there are many other uses as well

PROGRAMING STYLE AND THE WHILE LOOP
Watch “;” in syntax. That means “stop looping and just do the following code”

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: