khuang8493
6/14/2018 - 10:45 PM

Variable Scope for a block of code

Declare "x" inside the while loop, the scope of "int x" only exist within the while loop brackets. Can't use it outside of the while loop brackets.

The correct way of declare "int x" if want to use it later after the while loop, within the entire program, is to declare it outside of while loop brackets, in the beginning of the program, on the very top.

	do
	{
		int x = get_int("Please input an integer that is between 1 and 23: ");
	}
	while ((x<1) || (x>23)); // Declare "x" inside the while loop, the scope of "int x" only exist within the while loop brackets. Can't use it outside of the while loop brackets.

  // The correct way of declare "int x" if want to use it later after the while loop:
  
	int x;

	do
	{
		x = get_int("Please input an integer that is between 1 and 23: ");
	}
	while ((x<1) || (x>23));