• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Flow Control, If Statement.

 
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is taken from whizlabs.



I tried compiling this code and I get this error on line 10.
"error: 'else' without if"
What I know is that, the if condition execute the statement on Line 8 when it evaluates to true and the statement on Line 9 will always execute regardless the condition evaluates to either true or false.
I don't know what am missing here.
Please help me out.
Thank you.
 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually problem is that after 1st if statement you have putted another statement and then you have your else statement which is wrong because->

if you are using if statement without curly braces then you can write ATMOST1 statement after it and that statement should not be declarative statement means->



Now see->




Now see->

else without if means->



Here above because we don't use curly braces so statement after a++ is not part of if statement and hence if we write else after --a then it says then else without if because there is no previous if . Because that if is end now , that if is applicable only upto 1st statement which is (a++).

Correct code->



Above by using proper braces and else statement directly after if the code compiles fine.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O Shea wrote:Actually problem is that after 1st if statement you have putted another statement and then you have your else statement which is wrong because->

if you are using if statement without curly braces then you can write ATMOST1 statement after it and that statement should not be declarative statement means->



Now see->




Now see->

else without if means->



Here above because we don't use curly braces so statement after a++ is not part of if statement and hence if we write else after --a then it says then else without if because there is no previous if . Because that if is end now , that if is applicable only upto 1st statement which is (a++).

Correct code->



Above by using proper braces and else statement directly after if the code compiles fine.



IF I understand you very well O Shea, so after your first statement of the if condition it it illegal to add any statement if you don't put them in blocks(curly braces).
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is legal but that statement is considered as normal program instruction and not of if statement.
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this->

 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O Shea wrote:Try this->



You are right... but the moment I add an else clause to it...then I get into trouble.
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes because else should be placed directly after if or after else-if .

Well this is from which test of Whizlabs?? 1Z0-803 or 808?
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O Shea wrote:Yes because else should be placed directly after if or after else-if .

Well this is from which test of Whizlabs?? 1Z0-803 or 808?


IZ0-808
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:What I know is that, the if condition execute the statement on Line 8 when it evaluates to true and the statement on Line 9 will always execute regardless the condition evaluates to either true or false.
I don't know what am missing here.


What you know is wrong! No code will be executed because you get a compiler error due to line9. It would have been true if you remove the else block (lines 10-12) from the code. If you want the current code to compile successfully, you have two options: either wrap lines 8 & 9 within a pair of curly braces or remove line8/line9.
 
reply
    Bookmark Topic Watch Topic
  • New Topic