Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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?
Posted
Updated 21-Aug-13 7:01am
v2

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:
C++
#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.
 
Share this answer
 
Comments
pasztorpisti 21-Aug-13 12:58pm    
+5
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').
 
Share this answer
 
Comments
pasztorpisti 21-Aug-13 12:58pm    
+5
CPallini 21-Aug-13 13:19pm    
Thank you.
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.
 
Share this answer
 
v2
Comments
CPallini 21-Aug-13 13:19pm    
My 5.
pasztorpisti 21-Aug-13 13:20pm    
Thank you!
ALIWAZ,

Hi,

In fact your program knows how many times for loop will be executed befor control hits for() .. statement. The only thing is that number is not hard-coded rather its dynamic but it has been well before. That goes well with definition of for loop.

If you think of alternatives then you have while() or do while(). But it good to use while or do-while when number of times a loop will be executed is not yet decided before start of loop.

For example consider a case:

while(m_bLive==TRUE)
{
sleep(100);
}

In this above case the number of times this loop will be executed will be depend on fact that when variable m_bLive is set as FALSE.

In that way your use of for loop is appropriate since you know the count before starting the loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900