Question

Here is my code :

private String SerialNo;
private String FirmVersion;


public String  GetSerial(int[] Data){

    System.out.println("GetSerial Debug : Data => "+Data);

    for (int i = 2;i==13;i++){
        System.out.println("In the FOR => ok ");
        if (i != 9){
            SerialNo = SerialNo + Data[i];  
        }
        if (i == 9){
            SerialNo = SerialNo + ".";
        }
    }

    System.out.println("SerialNo => "+ SerialNo);
    return SerialNo;
}

My problem : I can't "enter" in the FOR

So my sysout of "In the FOR => ok", never shows and all the "actions" aren't done.

What am I doing wrong ?

ps : I'm sure that I'm compiling the right file.

Was it helpful?

Solution 2

Examine your for statement:

for (int i = 2; i==13; i++)

This actually means the following:

  1. assign 2 to i
  2. Check whether i equal to 13. If yes, continue loop, exit otherwise.

Since i is not 13 in the first iteration of loop you never enter it. I believe that you wanted to write

for (int i = 2; i <= 13; i++)

In this case you will iterate from 2 to 13 inclusively. The condition of for loop means "do I have to remain iterating?" and not "do I have to escape?"

OTHER TIPS

The loop condition is never satisfied; i = 2 in the begin, the first check would fail, so all the loop would fail. Maybe it should be changed for:

for (int i = 2; i <= 13; ++i)

Change for (int i = 2; i == 13; i++) to for (int i = 2; i <= 13; i++).

The second argument is the loop condition which has to be true to run the loop.

Your condition became false at first iteration so control never goes to loop body.

for loop syntax:

for(initialization; condition; increment/ decrement){
  //your code
}

So here you will have to use some appropriate condition to enter into the loop.

So for example :

for (int i = 0; i <= 13; i++) // for 0 to 13 increment

or

for (int i = 10; i >= 0; i--) // for 10 to 0 decrement

for (int i = 2;i==13;i++){}

It enters but failed at first condition check and for-loop exits.

It should be -

for (int i = 2;i<=13;i++)

You have initialized i=2

 for (int i = 2;i==13;i++)

And condition is i==13 which will become false ultimately flow never enter into for loop

try to change the code like this

for (int i = 2;i<=13;i++)

Statement is not good should be like the one below:

for (int i = 2; i<13; i++) or for (int i = 2; i<=13; i++)

See compared simple while loop in the case of your for loop.

Think about int i =2; value set and i == 13 condition

  1. Do you think it will work?

    for (int i = 2;i==13;i++){
      //do something
    }
    
    Same to below *while loop* explanation
    
    int i = 2;
    while (i == 13) {
        //do something
        i++;
    }
    
  2. I am sure it will work

    for (int i = 2;i < 13;i++){
       //do something
    }
    
    Same to below **while loop**
    
    int i = 2;
    while (i < 13) {
        //do something
        i++;
    }
    

The flow of the for loop is: init statement-> condition check-> goes inside loop or outside depending on the condition outcome.

Here, since you've said i=2 ,then i==13 is false; it'll never go inside the loop. You could use the ?: operator in the for loop and then modify your if statements a bit I guess..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top