• Post Reply Bookmark Topic Watch Topic
  • New Topic

why does this only loop 2 times (JAVA)  RSS feed

 
James Jenkins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


for (int i = 2; i < 10; i = i*i) {
System.out.println(i);
}


The answer i was given was 2  but shouldn't it be 3 times?

2
4
8

Doesn't a loop always begin with the first number?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James, welcome to CodeRanch!

Yes, a loop always begins with the initial value, but I think you're confused about the step size. The output you posted shows that i doubles on each step, but actually, i squares on each step.
 
James Jenkins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply sir .

Do we count the first " i " value as a loop?  because if we dont then i can understand why its only 2 loops.

if we look at this one :

for(int x = 1; x <=5; x++) {
  System.out.println(x);
}

Outputs
1
2
3
4
5



From the output , does that " 1"  count as a loop?  do we say that there have only been 4 loops in this one ?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

I suggest you go through those two loops with pencil and paper. There are a couple of examples for a different loop in this thread. Beware: we wrote those as examples and reserve the right to write fictitious values to make you work it out for yourself.
Anyway, work out those two loops on paper like that. Remember that you run the initialisation once, then the continuation test (even if the loop has never been run), then the loop then the increment. So if the i =... part makes the middle part evaluate to true, the loop will run with the first value of i. Work out what i * i evaluates to after your first loop has run once.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Jenkins wrote:. . .  do we say that there have only been 4 loops in this one ?
You mean to say, does the loop run 4×? If you look where the print instruction is, inside the loop, you can work out that the number of times something is printed is the same as how many times the loop runs.
 
Norman Radder
Ranch Hand
Posts: 146
4
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Radder wrote:Also posted at . . .
Please read this; as it says on the other website, people at both ends sho‍uld know about duplicate questions so as to avoid duplicated effort.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Jenkins wrote:Do we count the first " i " value as a loop?  because if we dont then i can understand why its only 2 loops.

Yes, the case where i=2 counts as an iteration. That's not the reason why the loop is said to only have 2 iterations.

You say the output will be:

However, I'm betting the program will only print:
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps if you rewrote the code a little and then compiled and executed it you would see what is happening.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statement on line 2 should remove definition for 1:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!