What would happen if the braces showing the loop body were removed?
The loop would become an infinite loop. Those two characters make a big difference!
Here is a fragment of the program, with those two braces removed:
if ( N >= 0 )
{
while ( N > 1 )
fact = fact * N;
N = N - 1;
System.out.println( "factorial is " + fact );
}
else
{
System.out.println("N must be zero or greater");
}
|
This fragment looks OK, but remember that blank lines and indenting are for people, not for the compiler. What the compiler sees is the same as:
if ( N >= 0 )
{
while ( N > 1 )
fact = fact * N;
N = N - 1;
System.out.println( "factorial is " + fact );
}
else
{
System.out.println("N must be zero or greater");
}
|
In this version, it is clear that the value of N is never changed inside of the loop, so the condition N > 1 is always true, and the loop continues forever.