What is wrong with the following loops (if anything), and how do you fix them?
1.
for (int
count=1;
count != 6; count++)
{
System.out.println(count);
count++;
}
2.
for (int
count=0;
count != 6; count++)
{
System.out.println(count);
count++;
}
3.
int
count = 0;
int
i = 1;
for
( count = 1;
i< 10; count++)
{
System.out.println("hello");
}
Tracing questions:
5. What will this print out? How many times will
it loop
for
( int count = 10; count < 15; count++)
{
System.out.println(count);
}
System.out.println(“end");
6. What will this
print out? How many times will
it loop
int numb = 1;
int sum = 0;
for
( int count = 3; count< 6; count++)
{
System.out.println(count);
numb = numb*count;
sum = sum + numb;
}
System.out.println(“at the end, sum is ”+ sum + " and numb is "
+ numb);
7. What will this
print out? How many times will
it loop
for
( int count = 0; count< 6; count++)
{
System.out.println(count);
}
System.out.println(“end");
8. What will this print out:
int bigSum=0;
int smallSum;
for (int count =3; count <=4; count++)
{
System.out.println(“Pass
#:”+ count);
smallSum
= 0;
int x;
for
(x = 1; x
< count; x++)
{
System.out.println(“number to add is “ + x);
smallSum =
smallSum+x;
}
System.out.println(“sum of pass “ + count + “ is “ + smallSum);
bigSum =
bigSum+smallSum;
// can I have System.out.println(x)
here?
}
System.out.println(“sum of all passes is “ +
bigSum);
9. What will this print
out: & What will total equal on the second pass?