1

For the scope of variables, I know that if you initialise a variable INSIDE a pair of curly braces, then you can only use that loop inside those braces, for example if you initialise a variable within a loop, once that loop ends, you can no longer use that variable. BUT, if I declare the variable outside the loop, and then CHANGE IT inside a loop, does the variable still have the value it was changed to AFTER the loop has ended?

For example:

    int pyramid_height = 0;

do
{
    printf("Please enter the half pyramid's height\n");
    pyramid_height = GetInt();
} while (pyramid_height < 0 || pyramid_height > 23);

for (i = 0; i < pyramid_height; i++)

say the user entered pyramid_height as 8 initially, would it still be set to 8 for me to use as the condition for the for loop i'm trying to make?

1

Yes it would. The variable is declared outside the loop. Any alteration you make in the variable value within main, would be stored in it!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .