这个程序如何最终获得5分?

Jad*_*den 0 c while-loop

谁能解释这个程序?末尾如何打印“ 5”?提前致谢。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 1;
    while (a++ <= 1)
    while (a++ <= 2);
    printf("%d",a);  //5
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ris*_*aje 7

请注意,;在第一个while语句之后没有after。这意味着有嵌套的while循环。

while (a++ <= 1)
    while (a++ <= 2);
Run Code Online (Sandbox Code Playgroud)

让我们一一检查行。

a = 1; // initialize 
while (a++ <= 1)  // condition is TRUE, after this statement a === 2
   while (a++ <= 2); // condition is TRUE, after this a == 3
   while (a++ <= 2); // condition is FALSE, 
                     // but `a++` is still evaluated. So after this statement a == 4. 
                     // Inner loop exits
while (a++ <= 1)  // condition is FALSE, 
                  // `a++` is evaluated and after this a==5
                  // Outer Loop exits
printf("%d",a);   // print value of a i.e. print 5.
Run Code Online (Sandbox Code Playgroud)