Do/while does not work properly on Galaxy S5 and Android 5.1.1

Asked 2 years ago, Updated 2 years ago, 82 views

It's an ordinary code, but for the Galaxy S5 and Android 5.1.1 combination
Only, do/while statements move mysteriously.No one knows the cause
Is that so?

inti=0;
int j = 0;

do{
    Log.d(TAG, "[test do-while1]i="+i+", j="+j);
    i = 0;
    Log.d(TAG, "[test do-while2]i="+i+", j="+j);
    i++;
    Log.d(TAG, "[test do-while3]i="+i+", j="+j);
    i++;
    Log.d(TAG, "[test do-while4]i="+i+", j="+j);
    j++;
    Log.d(TAG, "[test do-while5]i="+i+", j="+j);
} while(j<5);

The following is the log output from Galaxy S5 (Android 5.1.1).

D/MainActivity (9856): [test do-while1] i=2,j=0
D/MainActivity (9856): [test do-while2] i=2,j=0
D/MainActivity (9856): [test do-while3] i=2,j=0
D/MainActivity (9856): [test do-while4] i=2,j=0
D/MainActivity (9856): [test do-while5] i=2,j=1
D/MainActivity (9856): [test do-while1] i=2,j=1
D/MainActivity (9856): [test do-while2] i=2,j=1
D/MainActivity (9856): [test do-while3] i=2,j=1
D/MainActivity (9856): [test do-while4] i=2,j=1
D/MainActivity (9856): [test do-while5] i=2,j=2
D/MainActivity (9856): [test do-while1] i=2,j=2
D/MainActivity (9856): [test do-while2] i=2,j=2
D/MainActivity (9856): [test do-while3] i=2,j=2
D/MainActivity (9856): [test do-while4] i=2,j=2
D/MainActivity (9856): [test do-while5] i=2,j=3
D/MainActivity (9856): [test do-while1] i=2,j=3
D/MainActivity (9856): [test do-while2] i=2,j=3
D/MainActivity (9856): [test do-while3] i=2,j=3
D/MainActivity (9856): [test do-while4] i=2,j=3
D/MainActivity (9856): [test do-while5] i=2,j=4
D/MainActivity (9856): [test do-while1] i=2,j=4
D/MainActivity (9856): [test do-while2] i=2,j=4
D/MainActivity (9856): [test do-while3] i=2,j=4
D/MainActivity (9856): [test do-while4] i=2,j=4
D/MainActivity (9856): [test do-while5] i=2,j=5

If you try other combinations, the following behavior will be as expected.

D/MainActivity (9515): [test do-while1]i=0,j=0
D/MainActivity (9515): [test do-while2] i = 0, j = 0
D/MainActivity (9515): [test do-while3] i=1,j=0
D/MainActivity (9515): [test do-while4] i=2,j=0
D/MainActivity (9515): [test do-while5] i=2,j=1
D/MainActivity (9515): [test do-while1] i=2,j=1
D/MainActivity (9515): [test do-while2]i=0,j=1
D/MainActivity (9515): [test do-while3]i=1,j=1
D/MainActivity (9515): [test do-while4] i=2,j=1
D/MainActivity (9515): [test do-while5] i=2,j=2
D/MainActivity (9515): [test do-while1] i=2,j=2
D/MainActivity (9515): [test do-while2] i=0,j=2
D/MainActivity (9515): [test do-while3] i=1,j=2
D/MainActivity (9515): [test do-while4] i=2,j=2
D/MainActivity (9515): [test do-while5] i=2,j=3
D/MainActivity (9515): [test do-while1] i=2,j=3
D/MainActivity (9515): [test do-while2]i=0,j=3
D/MainActivity (9515): [test do-while3]i=1,j=3
D/MainActivity (9515): [test do-while4] i=2,j=3
D/MainActivity (9515): [test do-while5] i=2,j=4
D/MainActivity (9515): [test do-while1] i=2,j=4
D/MainActivity (9515): [test do-while2]i=0,j=4
D/MainActivity (9515): [test do-while3]i=1,j=4
D/MainActivity (9515): [test do-while4] i=2,j=4
D/MainActivity (9515): [test do-while5] i=2,j=5

Other facts confirmed are as follows:

·If you rewrite it to while or for statements, there will be no problem.
·Even if you doubt the log and replace it with a self-made log, the event will not change.
·It does not occur in Galaxy S4 and S6, nor does it occur in Android 5.0.2.

android java

2022-09-30 20:52

1 Answers

Is there any possibility that some optimization is being done?

For example, suppose Log.d() was removed when the app was released.

inti=0;
int j = 0;

do{
    i = 0;
    i++;
    i++;
    j++;
} while(j<5);

This is essentially what it looks like, and optimization may result in three lines from i=0; to i++; being summarized as i=2.

inti=0;
int j = 0;

do{
    i = 2;
    j++;
} while(j<5);

After that, it seems that Log.d() has been relocated again.

If this conjecture (which means the effect of optimization) is correct, the results should be different if the optimization is prevented.

inti=0;
int j = 0;
intk = 0;

do{
    Log.d(TAG, "[test do-while1]i="+i+", j="+j);
    i = 0;
    Log.d(TAG, "[test do-while2]i="+i+", j="+j);
    i++;
    k+=i;
    Log.d(TAG, "[test do-while3]i="+i+", j="+j);
    i++;
    k+=i;
    Log.d(TAG, "[test do-while4]i="+i+", j="+j);
    j++;
    Log.d(TAG, "[test do-while5]i="+i+", j="+j);
} while(j<5);

Log.d(TAG, "k="+k);

For example, by doing this, k operations interrupt, we cannot optimize the three lines for i together with i=2, so wouldn't it be as good as we expected?


2022-09-30 20:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.