Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The variables type of for loop in the initialization block, on page 82 (Java OCA 8 Programmer I)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 82, the second sentence of the last paragraph:
“The variables in the initialization block must all be of the same type”.

Everything is clear in this sentence and there is no problem in explanation. But it would be good that adding "declared" into the sentence:
“The variables declared in the initialization block must all be of the same type”.

Because in the second example of option 3 on this page we use different types (int and long) in the initialization block

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, this topic discusses the incompatible types of the initialization block of a for loop as well.

Mushfiq Mammadov wrote:But it would be good that adding "declared" into the sentence:
“The variables declared in the initialization block must all be of the same type”.

That might indeed be a small improvement. Although I wonder if other readers would not misinterpret and think something like the following code snippet would compileAlthough all declared variables in the initialization block have the same type, this code fails to compile (as you probably know).

Hope it helps!
Kind regards,
Roel>
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:First of all, this topic discusses the incompatible types of the initialization block of a for loop as well.

Thanks, Roel. I read this thread and saw that Jeanne Boyarsky had already noted it.
Jeanne Boyarsky wrote:That rule (about the same type) only applies to type declarations. If the type was declarated elsewhere, different types are allowed.


Roel De Nijs wrote:Although all declared variables in the initialization block have the same type, this code fails to compile (as you probably know).

Yes, we must use semicolon not comma
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:
Roel De Nijs wrote:Although all declared variables in the initialization block have the same type, this code fails to compile (as you probably know).

Yes, we must use semicolon not comma

Even with a semicolon it won't compile! Because you must use a comma to declare/initialize more than 1 variable in the initialization blockAnd as you can see the same applies to the updateStatement block as well, only comma allowed to define more than 1 update statement.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the updateStatement expression (aka the increment expression) does not need to update or increment/decrement variables. It could be any statement you like, e.g.Output:
1 + 9 = 10
2 + 8 = 10
3 + 7 = 10
4 + 6 = 10
5 + 5 = 10


Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mushfiq Mammadov wrote:
Roel De Nijs wrote:Although all declared variables in the initialization block have the same type, this code fails to compile (as you probably know).

Yes, we must use semicolon not comma

Even with a semicolon it won't compile! Because you must use a comma to declare/initialize more than 1 variable in the initialization block

I wanted to write briefly but my answer was not clear enough, you are right I only meant int i=0, int j=10; not for statement in generally. The for statement is explained very well in book, special thanks Jeanne and Scott for this.

We know that the for statement is consist of three parts (inside parentheses) and we must use only two semicolon to separate them otherwise we would get compile error.

Roel De Nijs wrote:And the updateStatement expression (aka the increment expression) does not need to update or increment/decrement variables. It could be any statement you like, e.g.

It is a very interesting example. I saw example without updateStatement but I haven't seen such as this yet. It is important thing which we pay attention is execution order. The update statement section is executed after body statement therefore i begins from 1 not 0. Thanks a lot, Roel

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:It is important thing which we pay attention is execution order. The update statement section is executed after body statement therefore i begins from 1 not 0.

Yes indeed!

Mushfiq Mammadov wrote:Thanks a lot, Roel

You are welcome.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic