关于循环的问题.......... [英] question about loop..........

查看:106
本文介绍了关于循环的问题..........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream.h  >  
#include < conio.h >
void main()
{
clrscr();
int f = 1 ;
int n;
cout<< 输入no;
cin>> n;

for int c = 1 ; c< = n; c ++)
{
f = f * c;
}
cout<< f;

getch();
}





[edit] pasztorpisti:source indentation [/ edit]



以上代码用于查找数字的阶乘值





i听说for循环通常在知道时使用提前循环执行多少次



我的问题是上面的程序我可以预先知道一个循环执行多少次?

解决方案

你不能 - 这取决于用户。

如果他输入3,它将执行三次。

如果输入8,它将执行8次。



循环的迭代次数在循环运行之前已知,但仅在之后用户做出选择。这是循环限制的运行时选择,而您可能将其视为编译时选择:

  #include   <   iostream.h  < span class =code-keyword>>  
#include < span class =code-keyword>< conio.h >
void main()
{
clrscr();
int f = 1 ;
int n;
/ * cout<<输入否; * /
/ * cin>> n; * /
n = 6 ;
for int c = 1 ; c< = n; c ++)
{
f = f * c;
}
cout<< f;

getch();
}



BTW:缩进你的代码是一个好主意 - 这样可以更容易地看到发生了什么。


Quote:

我听说for循环通常在预先知道循环执行次数时使用



C 编程语言的 语句是非常强大和灵活的:它特意填充程序,是的,当事先知道迭代次数时也会使用它。



As OriginalGriff 已经指出在你的程序中,循环上限取决于用户输入,因此你事先不知道它将执行多少次迭代(除非'用户'只是你,当然:rolleyes :)。





有时候对循环迭代设置一个上限是个好主意(如果它不符合'预期范围',你可以强制它拒绝用户输入)。

很久以前我已经回答了一个for循环相关的问题,我已成功找到它: for c in c编程 [ ^ ]。值得一读。



执行for循环体的次数是多少次?这取决于你放入for循环头部的第二个表达式。如果你在那里写一个总是求值为true的表达式,同时你从不在循环体中使用 break 那么你的循环永远不会停止。



有时循环次数是事先不知道的:循环的终止(for循环标题中的第二个表达式)可能取决于在内部更改的变量的值循环体取决于很多外部因素。


#include <iostream.h>
#include <conio.h>
void main()
{
    clrscr();
    int f=1;
    int n;
    cout<<"Enter no ";
    cin>>n;

    for(int c=1;c<=n;c++)
    {
        f=f*c;
    }
    cout<<f;

    getch();
}



[edit]pasztorpisti: source indentation[/edit]

above code is for finding the factorial of a number


i heard that for loop is usually used when it is known in advance how many times a loop will execute

my question is above program can i known in advance how many times a loop will execute?

解决方案

You can't - it's up to the user.
If he enters a "3", it will execute three times.
If he enters an "8", it will execute eight times.

The number of iterations of the loop is known in advance of the loop running, but only after the user makes his selection. This is a Run Time selection of the loop limit, whereas you are probably thinking of it as a Compile Time selection:

#include <iostream.h>
#include <conio.h>
void main()
    {
    clrscr();
    int f=1;
    int n;
    /* cout<<"Enter no "; */
    /* cin>>n; */
    n = 6;
    for(int c=1;c<=n;c++)
        {
        f=f*c;
        }
    cout<<f;
        
    getch();
    }


BTW: It's a good idea to indent your code - it makes it a lot easier to see what is going on.


Quote:

i heard that for loop is usually used when it is known in advance how many times a loop will execute


The for statement of the C programming language is very powerful and flexible: it endemically populates programs and, yes, it is also used when the number of iterations is known in advance.

As OriginalGriff already pointed out in your program the loop upper limit depends on user input, hence you don't know in advance how many iterations it will perform (unless 'the user' is just you, of course :rolleyes: ).


Sometimes is a good idea to put an upper limit on loop iterations (you may force it rejecting the user input if it does not fit in the 'expected range').


Long ago I've answered a for loop related question and I've successfully found it: for loop in c programming[^]. It is worth reading.

How many times will be the body of your for loop executed? It depends on the second expression your put into the head of your for loop. If you write there an expression that always evaluates to true and at the same time you never use break in the body of your loop then your loop never stops.

Sometimes the number of loops isn't known in advance: the termination of the loop (the second expression in the for loop header) may depend on the value of variables that are changed inside the body of the loop depending on a lot of external factors.


这篇关于关于循环的问题..........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆