控制语句结构

来源:互联网 发布:软件系统测试手册 编辑:程序博客网 时间:2024/06/11 05:56

if

if (x == 100)  cout << "x is 100";

if (x == 100){   cout << "x is ";   cout << x;}

if (x == 100)  cout << "x is 100";else  cout << "x is not 100";

if (x > 0)  cout << "x is positive";else if (x < 0)  cout << "x is negative";else  cout << "x is 0";

while

  while (n>0) {    cout << n << ", ";    --n;  }

do while

  do {    cout << "Enter number (0 to end): ";    cin >> n;    cout << "You entered: " << n << "\n";  } while (n != 0);

  for (int n=10; n>0; n--) {    cout << n << ", ";  }

for ( n=0, i=100 ; n!=i ; n++, i-- ){   // whatever here...}

break

  for (n=10; n>0; n--)  {    cout << n << ", ";    if (n==3)    {      cout << "countdown aborted!";      break;    }  }

continue

  for (int n=10; n>0; n--) {    if (n==5) continue;    cout << n << ", ";  }

goto

int main (){  int n=10;  loop:  cout << n << ", ";  n--;  if (n>0) goto loop;  cout << "FIRE!\n";  return 0;}

switch (x) {  case 1:    cout << "x is 1";    break;  case 2:    cout << "x is 2";    break;  default:    cout << "value of x unknown";  }

switch (x) {  case 1:  case 2:  case 3:    cout << "x is 1, 2 or 3";    break;  default:    cout << "x is not 1, 2 nor 3";  }

int x = 10;
int y=0;
switch (x) {  case 10:
     y=30;  default:     y+=20;  }
//y == 50