Java Question for cycle

Star-Dust

Expert
Licensed User
Longtime User
I found myself such a cycle to translate into B4X. And I can't understand what the final condition is for which the cycle stops. Usually it's a boolean condition now I find the negation of an integer, so what does that mean?

Java:
int i = 0;
for (; !i; i = 0 + i + 1) {
    System.out.println(i); 
    // other
}

PS the problem is that the condition is not a boolean and I don't understand how it is satisfied
 
Last edited:

Lucas Siqueira

Active Member
Licensed User
Longtime User
I found myself such a cycle to translate into B4X. And I can't understand what the final condition is for which the cycle stops. Usually it's a boolean condition now I find the negation of an integer, so what does that mean?

Java:
int i = 0;
for (; !i; i = 0 + i + 1) {
    System.out.println(i);  
}

B4X:
For i = 1 To 10
   Log(i) 'Will print 1 to 10 (inclusive).
Next
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
For i = 1 To 10
   Log(i) 'Will print 1 to 10 (inclusive).
Next
Forgive me, but this java cycle doesn't seem to stop at 10, it would
Java:
int i = 1; for (; i==10; i = 0 + i + 1) { System.out.println(i); }

the condition to be satisfied is !i not i==10
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
!i is equivalent, in some languages, to i==0
In other words, the for loop will only execute while i==0 (or until i<>0)
So if I'm not mistaken, the example code of the first post should only loop once.

In this case, the equivalent code would be
B4X:
for i=0 to 0
...
Next

(although some 'for' loops with more complex statements fit better into a Do..while..loop structure)
 

Star-Dust

Expert
Licensed User
Longtime User
!i is equivalent, in some languages, to i==0
In other words, the for loop will only execute while i==0 (or until i<>0)
So if I'm not mistaken, the example code of the first post should only loop once.

In this case, the equivalent code would be
B4X:
for i=0 to 0
...
Next

(although some 'for' loops with more complex statements fit better into a Do..while..loop structure)
Thanks @JprdiCP,

After researching on the web I suspected exactly what you told me.
I did some experiments but in java but the compiler doesn't accept it and yet it is definitely a working code.

Thanks again
 

JordiCP

Expert
Licensed User
Longtime User
Yes, it's strange 🤔
Perhaps it is an untested sample code written by someone who also programs in C/C++, and mistakenly mixed C and Java syntax
 

Star-Dust

Expert
Licensed User
Longtime User
I have the source and the compiled version. I'm sure it works even with the compiled version.
 

RodM

Member
Licensed User
B4X:
int i = 1; for (; i==10; i = 0 + i + 1) { System.out.println(i); }

The condition specified above seems to be like "Loop WHILE i==10". The second parameter of a for loop must be True for the loop to execute and, in that case, the initial value of i is 1 and the loop should never run this way.

Sorry if I am wrong, I'm not an expert in Java :)
 

LucaMs

Expert
Licensed User
Longtime User
1695490296317.png



Eventualmente la colpa è tutta di Perplexity, non mia 😁
If that's not accurate, it's all Perplexity's fault, not mine
 

Star-Dust

Expert
Licensed User
Longtime User
Solved..thank's
Yes, it's strange 🤔
Perhaps it is an untested sample code written by someone who also programs in C/C++, and mistakenly mixed C and Java syntax
 
Top