简体   繁体   English

if条件中的i ++和while循环中的if之后的i ++有什么区别?

[英]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: 而且我不是在if条件下而是在一段时间之前:

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; 假设在代码上方定义了其他变量,例如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' . 这两个代码段之间的逻辑差异在于,在第一个代码段中,变量i仅在ptype'B''P'时递增。 However, in the second snippet, i gets incremented with each iteration of the while loop regardless of what the value of ptype is. 然而,在第二个片段, i获取与每次迭代递增while循环,无论什么价值ptype的。

In the second version, the while loop would iterate a maximum of N times, regardless of the input. 在第二个版本中,无论输入如何, while循环最多将迭代N次。 However, the first version would iterate an unlimited amount of times, stopping only after either 'B' or 'P' has been entered N times. 但是,第一个版本将迭代无限次,仅在输入'B''P' N次之后才停止。

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. 在if条件内,如果ptype等于p,则i将增加1,如果ptype也等于B,则i将再次增加1,因此, if and only if both conditions are met. ,i上的总增量将为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. 因此,这意味着如果两个条件都不满足,i不会增加。

Second Case: 第二种情况:

i++ outside if but inside while: so i will get incremented by one on every iteration of while irrespective of if conditions. i ++在if之外但在while里面:因此,while的每次迭代都将使i加一,而不管是否存在条件。 so, total increment on i will be 1 for each while iteration. 因此,每次迭代时i的总增量为1。

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 因此,在第一种情况下,如果同时满足两个“如果”条件,我将增加两次,而在第二种情况下,它将在while循环的每次迭代中增加一

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. 主要区别在于,如果在if状态中增加计数,则意味着如果列出的两个选项中的任何一个出现,计数都会增加,而在if语句中排除计数则意味着计数将始终增加。 This issue at hand is that if ptype !== "P" || 当前的问题是,如果ptype!==“ P” || 'B' then it will not increment. 'B'则不会递增。

i++ in the if condition: i ++在if条件下:

Here i will only be incremented if ptype that is, the scanned character is either P or B . 在这里,仅当ptype即扫描的字符为PB时i才会递增。 In other words, i will be incremented when the control will enter either of the if block. 换句话说,当控件进入任何一个if块时, i都会递增。

i++ after the if inside a while loop i ++在while循环中的if之后

In this case, i will be incremented in every iteration of the while loop. 在这种情况下, i将在while循环的每次迭代中递增。

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. 此外,第一种情况还意味着,如果不扫描所需字符,则while循环将迭代N次以上的迭代。 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. 而在第二种情况下,while循环将仅迭代N次迭代,而与是否找到所需字符无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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