简体   繁体   English

在C-中实施循环

[英]For Loop in C- implementation

I am confused on how to complete this for loop. 我对如何完成此for循环感到困惑。 The mission is to read input in unix. 任务是读取UNIX中的输入。 For the input if the radius is >0 it should prompt the user each time and then if <=0 it should terminate. 对于输入,如果半径> 0,则每次应提示用户,如果<= 0,则应终止。 I am going from centimeters to square inches. 我要从厘米到平方英寸。 My current configuration requires 2 inputs (1 prompted, 1 not) before giving output to the console. 我的当前配置在将输出提供给控制台之前需要2个输入(提示1个,否1个)。 Cheers. 干杯。

#include <stdio.h>
#define PI 3.14159

main()
{
float r, a;
  int y = 9999999;


  for(int i =0; i <y; i++){
     printf("Enter the circle's radius (in centimeters): ");
     scanf ("%f", &r);

        if(r>0){
           r=r;
       a = PI * r * r *2.54;

       printf("Its area is %3.2f square inches.\n", a);
   }  else {}
     }

  }

Your code flow is the following: 您的代码流如下:

for (infinite condition) {
  scan input
  if (input > 0) {
    do things
  }
  else {
    do nothing
  }
}

So there's no way to exit out of the loop, that's why the break statement exists, to force quitting an iterative block of code: 因此,没有办法退出循环,这就是存在break语句以强制退出迭代代码块的原因:

while (true) {
  scanf ("%f", &r);
  if (r > 0) {
    // do whatever;
  }
  else
    break;
}

The break will stop the cycle when executed, just going out of the loop. break将在执行时停止循环,只是退出循环。

Consider a while loop instead: 考虑使用while loop

#include <stdio.h>
#define PI 3.14159

main(){
    float r, a;

    int continueBool = 1;
    while(continueBool == 1){
         printf("Enter the circle's radius (in centimeters): ");
         scanf ("%f", &r);

         if(r>0){
              a = PI * r * r *2.54;
              //the above formula may be wrong, so consider trying:
              //a = PI * r * r/2.54/2.54;
              printf("Its area is %3.2f square inches.\n", a);
         }
         else{
             continueBool = 0;
         }
     }
}

The break statement can be dangerous if you are new to C programming, so I recommend not using it until you get a better understanding of C and break. 如果您不熟悉C编程,则break语句可能很危险,因此,建议您在对C和break有了更好的了解之前, 不要使用它。 If you do want to use break , then this could be your solution: 如果您确实想使用break ,那么这可能是您的解决方案:

#include <stdio.h>
#define PI 3.14159

main(){
    float r, a;

    while(1){
         printf("Enter the circle's radius (in centimeters): ");
         scanf ("%f", &r);

         if(r<=0){
             break;
         }

         a = PI * r * r *2.54;
         //the above formula may be wrong, so consider trying:
         //a = PI * r * r/2.54/2.54;
         printf("Its area is %3.2f square inches.\n", a);
     }
}
r=1.0f;

// break if no. of cases exhausted or r is negative or zero
for(int i =0; i < y && r > 0; i++) 
{
     printf("Enter the circle's radius (in centimeters): ");

     if( scanf ("%f", &r) == 1) // Always check for successful scanf
     {
          a = PI * r * r/2.54/2.54; //This is correct formula

          printf("Its area is %3.2f square inches.\n", a);
     } 

 }

You may want to try using a while loop instead so that the question is continually prompted until the user inputs a value =>0. 您可能想尝试使用while循环,以便不断提示问题,直到用户输入值=> 0。 see if below helps (also your conversion factor was not quite right); 看看下面是否有帮助(同样您的转换系数不太正确);

#include <stdio.h>
#define PI 3.14159

void main()
{
    float r, a;
    printf("Enter the cirle's radius (in centimeters):");
    scanf("%f",&r); 

    while (r>0)
    {
        a=PI*r*r*0.155; // conversion from sqcm to sqin is ~0.155
        printf("Its area is %3.2f square inches \n", a);
        printf("Enter the cirle's radius (in centimeters):");
        scanf("%f",&r);
    }
}

Use this: 用这个:

for(int i =0; i < y; i++)
{
     printf("Enter the circle's radius (in centimeters): ");
     scanf ("%f", &r);

     if(r > 0)
     {
          a = PI * r * r *2.54;

          printf("Its area is %3.2f square inches.\n", a);
     } 
     else
     {
          break;
     }
 }

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

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