Bookmark Topic Watch Topic
  • New Topic

Need little help errors  RSS feed

 
Nikola Pejovski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Today i started learning java, and it is very similar to cpp. And now i wrote little program just to test my knowledge for this beginner stuff, and i get non commented error at 22 and at 46 unreachable code. What is the problem?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
You have a rather strange end condition in your for loop:


I think what you mean here is:
- i.e. referring to the variable i, rather than the number '1' in the conditional part of the for loop.


With your end condition of "1<3". This will ALWAYS evaluate to true.
Therefore the program will never exit the loop.
Therefore after your for loop you have "unreachable code"

Also, the standard way of declaring a loop is to declare the loop counter in that statement as well:
>
 
Nikola Pejovski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator

I changed the code is it good now? when i changed like this i lost the error on 44, but i'm still getting the unreachable code error on 22.
EDIT: OMG haha i just saw that i typed 1 instead i lol im feeling so silly right now, i just want to kill my self :P tnx for help and sorry for bothering you
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Nicola,

Nicola Pejovski wrote:I changed the code is it good now?
No. I'm afraid more problems.

Beside that, that for loop has to be fixed in all three "for" loops accordingly by changing "1's" to "i", "j" and "k", also..
Your printf statement is used in improper way. Go through official Oracle's tutorial how to use printf (<- click on it) statement.
Some examples you could find nearly at the bottom of tutorial's page. Hope it helps.

And welcome to Ranch.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Welcome again

And only ever write \n if somebody asks for the LF character. Not even if you have a book with \n in. After printf always write %n.
 
Nikola Pejovski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
thank you for the replies, these were very useful tips
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Nikola Pejovski wrote: . . . java . . . is very similar to cpp. . . .
Despite Java®'s history, it is not at all similar to C++. The syntax is similar but the semantics is completely different. Some keywords, e.g. protected, mean different things. It is dangerous to allow the apparent similarities to confuse you.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Nikola Pejovski wrote:thank you for the replies, these were very useful tips
You're welcome
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!