簡體   English   中英

如何跳出循環而不執行該循環中的其他語句?

[英]How to break out of a loop and not execute the other statements in that loop?

void process(){
  int ID;
  float hours_employee_worked, hourly_wages_for_employees;
  float tax = 0.4;
  float overtime = 1.5;
  float time_without_overtime, total_overtime;
  float total, average;


  do {
    printf("Enter Employee ID (or -1 to exit): ");
    scanf("%d", &ID);
    printf("Enter hours worked (e.g 8.5 hours): ");
    scanf("%f", &hours_worked);
    printf("Enter hourly wage (e.g 20.25): ");
    scanf("%f", &hourly_wage);
    if (ID == -1)
    {
      puts("All done");
      break;
    }
    else if (hours_employee_worked <=40)
    {
      total = hours_employee_worked* hourly_wages_for_employees;
      average = total * tax;
      average = total - average;
      printf("%.2f", average);
    }
    else if (hours_employee_worked > 40)
    {
      total_overtime = ((hours_employee_worked- 40) * 1.5) * hourly_wage_for_employees;
      total = (40 * hourly_wages_for_employees+ total_overtime) * tax;
      time_without_overtime = 40 * hourly_wages_for_employees;
      average = time_without_overtime + total_overtime - total;
      printf("%f", average);
    }

  }while (ID != -1);

}

基本上,如果員工 ID 等於 -1,我希望我的程序結束執行。 但它仍然繼續並在最后中斷。 在執行完成並且不會跳出循環之前,它不會說“全部完成”。 謝謝。

如果您想在檢查employee_id 后立即將第一個if 語句移動到程序頂部,您的程序運行良好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM