简体   繁体   中英

What is the difference between i++ in the if condition and i++ after the if inside a while loop?

int i = 0 ;
        while(i < N)
        {
            char ptype ;
            scanf("%c" , &ptype);
            //getchar();
            if(ptype == 'P'){
                scanf("%d" , &passto);
                //printf("\n");
                preplayer = arr[top];
                top++;
                arr[top] = passto;
                printf("%d\n", i); 
                i++;               
            }
            if(ptype == 'B'){
                int tempplayer = arr[top];
                top++;
                arr[top] = preplayer ;
                preplayer = tempplayer;
                i++;
            }
            //++i;
        }

And i++ not in if condition but before while:

int i = 0 ;
    while(i < N)
    {
        char ptype ;
        scanf("%c" , &ptype);
        if(ptype == 'P'){
            scanf("%d" , &passto);
            //printf("\n");
            preplayer = arr[top];
            top++;
            arr[top] = passto;
            printf("%d\n", i); 
            //i++;               
        }
        if(ptype == 'B'){
            int tempplayer = arr[top];
            top++;
            arr[top] = preplayer ;
            preplayer = tempplayer;
            //i++;
        }
        i++;
    }

Both of them gives the different results. Assume other variables are defined above the code like N = 10; and other integers and characters are also defined.

The above two codes give different results in the case of below input:

1
10 23
P 86
P 63
P 60
B
P 47
B
P 99
P 9
B
B

The logical difference between the two code snippets is that in the first one, the variable i only gets incremented if the ptype is 'B' or 'P' . However, in the second snippet, i gets incremented with each iteration of the while loop regardless of what the value of ptype is.

In the second version, the while loop would iterate a maximum of N times, regardless of the input. However, the first version would iterate an unlimited amount of times, stopping only after either 'B' or 'P' has been entered N times.

First case:

inside if condition, i will get increment by one if ptype is equal to p and again it will be incremented by one if ptype is also equal to B so, total increment on i will be 2 (1+1) if and only if both conditions are met. so, it means no increment will happen on i if both conditions aren't met.

Second Case:

i++ outside if but inside while: so i will get incremented by one on every iteration of while irrespective of if conditions. so, total increment on i will be 1 for each while iteration.

So, in first case i will get incremented two times if both 'if' conditions are met, while it will get incremented by one on every iteration of while loop in second case

The main difference is that having the count increments within the if satements means that it will increment if either the two listed options occur and having it out of the if statements means that it will always increment. This issue at hand is that if ptype !== "P" || 'B' then it will not increment.

i++ in the if condition:

Here i will only be incremented if ptype that is, the scanned character is either P or B . In other words, i will be incremented when the control will enter either of the if block.

i++ after the if inside a while loop

In this case, i will be incremented in every iteration of the while loop.

Moreover, the first case also implies that the while loop will iterate for more than N number of iterations if the desired characters are not scanned. Whereas, in the second case while loop will only iterate for N iterations irrespective of the fact that the desired character(s) is found or not.

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