简体   繁体   中英

Using a greater-than sign in a for loop

I'm working on an encryption assignment for my first CS class, and I'm trying to make this code work... here's what I've got.

    public static void findE(int phiPQ) {

        int e = 0;

        for(e > 2; e < (phiPQ - 1);) {
            int larger = phiPQ;
            int smaller = e;
            int r = 1;
            r = larger / smaller;
            larger = smaller;
            smaller = r;

            if(larger  == 1) {
            break;
        }
        return; 
     }

Now the issue I'm having is that when I try to do "e > 2" in the for loop it does not work, and says that the assignment operator is invalid. The assignment says that I'm supposed to use a for loop from e > 2 to e < (phiPQ - 1). How is this possible without something like a while loop? I tried that as well and couldn't get the return statement to work. Please help!

Someone has "fixed" some of the issues discussed below. YMMV.


There are several critical problems with the code. The combination of syntax and/or semantic errors may compound upon each other. First start by fixing the blatant syntax errors.

1. The for syntax is wrong.

The first "part" of a for loop statement must be an initialization statement (read: "assignment"). It cannot be an arbitrary expression. The syntax is for(initialization; termination; increment) statement - see the link for more details.

I expect the code should be:

for(/* At the start, LET e = 2 */
    e = 2 ;
    /* Terminate when this is false. */
    e < (phiPQ - 1) ;
    /* no increment */) ..

Or:

for(e = 2; e < (phiPQ - 1) ;) ..

(Or, assign 2 to e above the for-loop and provide an empty initialization statement.)

2. As identified by a comment is the code is equivalent to:

// This is a loop with AN EMPTY STATEMENT FOR A BODY due to the
// immediately trailing semicolon.
// Even when this loop is corrected per above, the condition will never
// change state due to lack of increment and it will "loop forever".
for(e > 2; e < (phiPQ - 1);) ;

// This is a new block, that does NOT BELONG to the loop above.
{
    int larger = phiPQ;
    int smaller = e;
    int r = 1;
    r = larger / smaller;
    larger = smaller;
    smaller = r;
}

To fix this, remove the ; immediately following the for statement.

3. The loop never changes state.

The termination condition is dependent upon e and phiPQ . However, these variables are never changed and so the loop will either never loop or never terminate normally.

Review the loop termination conditions and state advancement.

4. break is used outside of the for statement.

While #3 asserts that the code never terminates, it may be the case that the if statement containing the break (which is incorrectly located outside of a looping construct and will result in a syntax error) is meant to terminate the loop. If this is the case, then it should be inside the loop body.

How about:

for (int e = 0; e > 2 && e < (phiPQ - 1);)
{
    // your code
}

For loop :

for(int e = 3; e < (phiPQ - 1); e++){

}

You are receiving this error because the first expression in a for-loop is used for initialisation. The second expression is then your termination condition; when the termination condition evaluates to false , the loop will end. eg

for (int i = 0; i < numIterations; i++) {}

This loop will iterate through the body numIterations times.

You will therefore want to more fully define the iteration space of your loop in the second expression, or perform assignment of e through the first expression.

For more information, refer to the Javadoc for for-loop s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM