Share
Explore BrainMass

Java: flow of control looping

1. What is the output of this code sequence? (The user successively enters 3, 5, and -1.)

System.out.print( "Enter an int > " );
int i = scan.nextInt( );
while ( i !=-1 )
{
System.out.println( "Hello" );
System.out.print( " Enter an int > " );
i = scan.nextint ( );
}

2. what are the values of i and product after this code sequence is executed?

int i=6;
int product =1;
do
{
product *=i;
i++;
}while ( i <9 );

3. What is the output of this code sequence?

for ( int i = 0; i <3; i++ )
System.out.println( "Hello" );
System.out.println( " Done" );

4. What are the values of i and sum after this code sequence is executed?

int i = 0;
int sum = 0;
for ( i =0; i < 5; i++ )
{
sum += i;
}

5. What is the value of sum after this code sequence is executed?

int sum = 0;
for ( int i = 1; i < 10; i++ )
{
i++;
sum += i;
}

Solution Preview

Thank you for asking BrainMass!

1. The output of given code sequence would be below 5 lines. First line would be printed due to code before the loop. Loop will execute 2 times, one for user input 3, and second time for user input 5.

Enter an int >
Hello
Enter an int >
Hello
Enter an int >

2. Loop will execute for the values ...

Solution Summary

The solution not only provides the output of code execution, but it also explains why that output will come.

$2.19