Home > Back-end >  for loop statement initialization
for loop statement initialization

Time:09-15

Is it possible to define '10' in the for loop initialization as a variable where the user is prompted to input the value of i?

int i;
for (i = 10; i > 0; --i)

example:

for (i= x > 0; --1)

where x is defined as an input parameter using the scanf function? I searched online and couldn't find any examples.

CodePudding user response:

int main()
{
    int x;
    scanf("%d",&x);
    
    for (int i=x;i>0;--i)
    {
        printf("doingSomething\n");
    }
    
    return 0;
}

CodePudding user response:

If you sign up for Harvard's CS50, you will download their library of functions for beginners including the chance to:

for( int i = get_int("Enter start value: "); 0 < i && i < MAXIMUM; i   )

You can see how the user input is gathered with a function call as part of the first statement of for()

(Of course, you could write your own version of this function, too.)

  • Related