• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

if statemens in java

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody

As far as I know if statements are executed in the same order that they are coded(if true) so I am just wondering what happens if I have three if condition and the second condition is true first so it is executed and after that the first condition is true second? For example:
if(){   //Second true
}
if(){  //First true
}
if(){
}

I was coding a simple programme and I came across this issue if the first condition is true first then the programme works perfectly but if the second condition is true first then the first condition is ignored and is never checked again.
 
Bartender
Posts: 11116
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example the 'if's would be processed in the order shown (top-down) and one 'if' is not dependent on any other 'if'.
If instead you wrote
The first 'if' that was true would be processed and any following 'if's that might have been true as well will NOT be processed.
 
Rancher
Posts: 5120
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if the second condition is true first then the first condition is ignored and is never checked again.


That implies to me that there could another thread of code that is changing the conditions while the code in the list of if statements is executing.  The condition for the first if is changed after the first if has executed.
Or do the statements controlled by the second if statement change the values for the condition used in the first if statement?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use if and else if statements. I hope you learnt it in C/C++ (IF you studied C/C++ before)
Good Luck
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the programme gets an input from the user and depending on the input it goes through one of the conditions and it this processes continuously repeats. If the first if statement is executed then the programme works fine with the second and third statement but if the second 'if statement' is executed then the first one is not executed anymore even if it is true. I tried it with both if and else if.
 
Norm Radder
Rancher
Posts: 5120
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if the second 'if statement' is executed then the first one is not executed anymore even if it is true.  


If the condition is true, the if statement's code block will be executed.  

Can you make a small complete program that compiles, executes and shows the problem?  What you say is happening must be caused by something else in the program.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is something else cause I tried it with while loop as well in a small version of the programme and it's the same


So if you enter a smaller number than the random number(Second condition true) followed by a greater number than the random number(First condition true) it crashes. On the other hand if the first condition is true followed by second condition it works perfectly
 
Norm Radder
Rancher
Posts: 5120
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the console from when you execute the program that shows the problem?  Add some comments to the print out explaining what the problem is.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it would also help if you posted a COMPLETE program. I should be able to cut-n-paste your code into a new file, compile it, and run it.  This is just a code snippet...there's no way to tell if maybe something else is going on or not.

And just saying "it crashes" doesn't help. If it crashes, it will print a stack dump...that's important information as well.
 
Carey Brown
Bartender
Posts: 11116
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your while should be outside of your ifs.

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your while should be outside of your ifs.


This solved the problem!
So could you please explain why my logic didn't work. I mean my logic also makes sense and theoretically it should work

I also tried the code below but it didn't work either without any if.
 
        Could you please explain the difference between these logics please?
Thanks for your help
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saeid jamali wrote:
So could you please explain why my logic didn't work. I mean my logic also makes sense and theoretically it should work



It is easier to envision if you just draw it out. Take a pencil and paper, pretend you are the JVM, and follow the flow of the code as a diagrams. Keep track of the variables, the loops, the conditionals, and follow the code flow.

Henry
 
Norm Radder
Rancher
Posts: 5120
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of the code?  What value is it looking for?  Does there need to be an enclosing loop so that value is found?
 
Carey Brown
Bartender
Posts: 11116
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could have written it this way but you still need the outer loop and this doesn't gain you anything and loses some clarity.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic