• Post Reply Bookmark Topic Watch Topic
  • New Topic

if statemens in java  RSS feed

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • 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?
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2240
28
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2240
28
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 2240
28
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!