• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Simple Control Flow Question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I merge some code in Thinking in Java as below:
//: c03:ListCharacters.java
// Demonstrates "for" loop by listing
// all the ASCII characters.
///:~
The book states the syntax for for-loop:
for(initialization; Boolean-expression; step)
statement
Is anything wrong in my code?
Thanks.

[This message has been edited by Marilyn deQueiroz (edited August 14, 2001).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Notice how in the section you commented out, you declare your variables on two separate lines. Why? Because they are different types.

Also, notice how you declared
<pre> int i , j ; </pre>
after
<pre> char c ; </pre>
If you wrote this line this way
<pre> int i , int j ; </pre>
it would not compile.

Likewise, if you declare variables in the initialization section of a for loop, they must all be the same type.

So if you change
<pre>char c = 0, int i = 0, int j = 1 ; </pre>
to
<pre>int c = 0, i = 0, j = 1 ; </pre>
this problem will be resolved.

[This message has been edited by Marilyn deQueiroz (edited August 14, 2001).]
 
zan hsieh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I actually intend to ask it is possible to initialize more than one primitive types in the first argument of for-loop. If you could, please write an example.
Thanks.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can initialize more than one variable, but they need to be of the same type (you can only pick ONE type).
The clue that Marilyn was trying to give you is that the semi-colon is required to end one primitive type declaration, before beginning another.
char c ; //notice the semi-colon
int i, j; // two of the same followed by a semi-colon
In the init portion of the for loop you only get ONE semi-colon, then you are in the next portion of the statement. Therefore only ONE primitive type declaration.
 
zan hsieh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic