c - 'while' 在这个程序中做什么?

标签 c while-loop

这是一个 C 程序,用于计算离散频率分布的算术平均值。 该程序按预期运行,但无法理解 main() 函数中“While”在该程序中的作用。

void main()
{
  int choice;
  while(1)
  {

  printf("\n\tThe Mean for Discrete Distribution ");
  printf("\n\n1.Direct Method \n2.Shortcut Method \n0.Exit ");
  printf("\nEnter your choice: ");
  scanf("%d",&choice);

  switch(choice)
  {
    case 1:
      getdata();
      direct();
      break;

    case 2:
      getdata();
      shortcut();
      break;

    case 0:
      exit(0);
      break;

    default:
      printf("\nEnter valid choice!");
      break;
   }

  }
}

程序运行得很好,但我只是不明白“while”在这里做什么。程序很大,所以我没有写函数。

最佳答案

由于 while(1) 总是评估为 true,该程序不断提示用户“输入您的选择”,直到用户点击 0。当输入 0 时,整个程序停止,因为将调用函数 exit(0)

关于c - 'while' 在这个程序中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681759/

相关文章:

c - Linux read()最小数据量

c - 如何接收 (void *)0x12345678 参数?

c - 如何重置 while 循环?

java - 当没有输入任何内容时解析生成我生成的错误消息的字符串

java - While with Buffered reader 中的两个条件

c - tx_buf 期望 spi_transfer 中的输入内容

java - DGT 电子板的 RabbitPlugin 未显示完整的 FEN 符号

c - 在c中制作我自己的数组:

php - 从查询结果打印多维数组

java - Java 上 0.1 到 2 的奇怪循环