thephfactor
thephfactor

Reputation: 181

Java for loop won't run...?

I am working on a homework assignment, and the crucial loop of the program is giving me trouble. My teacher has informed me that she will take off points if I use a while loop for a counter-controlled variable, so I'm anxious to get this right.

Here's what I would like to work, and what I feel in my heart should work:

for ( int check = 0; check == value; check++ ) {
    int octal = getOctal();
    int decimal = convertOctal( octal );
    System.out.printf( "%d:%d", octal, decimal );
}

However, this loop does not run. I tried doing it with a while loop, and it worked perfectly!

int check = 0;
while ( check < value )
{
    int octal = getOctal();
    int decimal = convertOctal( octal );
    System.out.printf( "%d:%d", octal, decimal );
    check++;
}

Here is the rest of the main method:

public static void main ( String args[] )
{
    int value = getCount();

    while ( value < 0 )
    {
        System.out.print( "\nYou must enter a positive number" );
        value = getCount();
    }

    if ( value == 0 )
    {
        System.out.print( "\n\nNo numbers to convert.\n\n" );
    }
    else
    {   
        int check = 0;
        while ( check < value )
        {
            int octal = getOctal();
            int decimal = convertOctal( octal );
            System.out.printf( "%d:%d", octal, decimal );
            check++;
        }
    }
}

Yes, this is an octal-to-decimal converter. I wrote the converter method myself from scratch and am ridiculously proud of it.

EDIT: MY QUESTION is, what's wrong here? EDIT part deux: Thanks all for your help in clearing up my misunderstanding. Onward to method documentation!

Upvotes: 2

Views: 6156

Answers (4)

Nathan Power
Nathan Power

Reputation: 650

From the Oracle website (my emphasis):

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
statement(s) 
} 

When using this version of the for statement, keep in mind that:

The initialization expression initializes the loop; it's executed once, as the loop begins.

When the termination expression evaluates to false, the loop terminates.

The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Upvotes: 1

user2030471
user2030471

Reputation:

To get the same effect as:

int check = 0;
while (check < value) {
  // something
}

your for should look like this:

for (int check = 0; check < value; check++) {
  // something
}

Upvotes: 0

Jean Waghetti
Jean Waghetti

Reputation: 4727

for ( int check = 0; check == value; check++ )

This will only run if check == value. Modify to:

for ( int check = 0; check < value; check++ )

Upvotes: 12

gefei
gefei

Reputation: 19856

try for ( int check = 0; check <= value; check++ ) instead of for ( int check = 0; check == value; check++ )

Upvotes: 3

Related Questions