difference in logic...

Had trouble finding this specific on the net, unless i had the time to just read entire courses.
I was wondering what the exact difference was between this…

if (condition1){
     if (condition 2){
         do.something;
     }
     if (opposite of condition 2){
          do.somthing;
     }
}

and

if (condition 1  condition 2){
      do.something;
}else if (condition 1  opposite of condition 2){
      do.something
}

At face value, these are exactly the same (asuming by “condition” in the first example was meant to be “condition 2”).

The main difference here is readability, as the former is much cleaner than the latter (though it would still look better with an ‘else’ instead of ‘if (opposite of condition)’ ).

Where they functionally differ is when either condition is a function, here’s a breakdown:
First sample:
condition1() will be called once regardless.
condition2() will never be called if condition1() returns false, and twice if condition1() returns true.

Second sample:
condition1() will be called twice if it or condition2() returns false, or once if it and condition2() return true.
condition2() will never be called if condition1() returns false, once if condition2() returns true, and twice if condition2() returns false

That’s the only difference I can think of that would actually affect the outcome of your code, specifically if either of the functions modifies data, such as a pre or post increment.

There would also be a difference in performance, under most circumstances it would be relatively trivial but if a condition is expensive to evaluate or this code is inside a tight loop that would be important.

Ah yes sorry made a mistake, edited

well i tried to make key switch to turn a trail on and off and while the first logic didn’t work the second method did?

Huh, that’s odd. Were your conditions functions, or properties, that changed the state of the object?

Also, I just thought of something else that would matter; if the first do.something in your first example changed the value of condition2 then the second if in that block would be run, whereas in the second example you’re using else, so if the first block gets executed the else gets skipped.

To clarify:

if (condition1)
  if (condition2)
    // condition 2 is true
    condition2 = false; // change condition 2
  if (!condition2)
    // always runs because the first if block changed condition2
    do.stuff

This could be fixed by replacing “if (!condition2)” with “else”, which guarantees that the block will only be executed if the matching if block doesn’t.

Condition 1 is the input.getkeydown condition 2 is the bool to see if the trail is turned on or off. In both condition 2 and it s opposite i changed the bool. I m on a tab atm. Will post the codes if i’m on my pc.

so this is the non working script

using UnityEngine;
using System.Collections;

public class TrailEnabler : MonoBehaviour {
	
	public KeyCode UserKey=KeyCode.T;
	TrailRenderer trail;
	bool TrailOn;

	// Use this for initialization
	void Start () {
		
		trail = gameObject.GetComponent<TrailRenderer>();
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if (Input.GetKeyDown(UserKey)){
			if (TrailOn){
			
				trail.enabled = false;
				TrailOn = false;
			
			}
			
			if (!TrailOn){	
				
				trail.enabled = true;
				TrailOn = true;	
				
			}
			
		} 
		print (TrailOn);
	
	}
}

this the working one:

using UnityEngine;
using System.Collections;

public class TrailSwitch : MonoBehaviour {
	
	public KeyCode UserKey =  KeyCode.T;
	TrailRenderer Trail;
	bool TrailOn;
	

	// Use this for initialization
	void Start () {
		
		Trail = gameObject.GetComponent<TrailRenderer>();
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(Input.GetKeyDown(UserKey)  TrailOn){
			
			Trail.enabled = false;
			TrailOn = false;
			
		}else if(Input.GetKeyDown(UserKey)  !TrailOn){
			
			Trail.enabled = true;
			TrailOn = true;
			
		}
	
	}
}

The reason why the initial script is not working is because the moment TrailOn is set to false, it will be set to true again. If you replace

if(!TrailOn)

to

else

It should work.

EDIT
Actually, Tasgall and _Petroz did mention it haha.

sorry if i keep going on this, but why doesn’t it only do that if the keydown is pushed? i tought it would only do either one of them if the key is pressed and that it then would look what the state of TrailOn is and act accordingly?

Okay, when your TrailOn is true, it sets it to false and exits the if(TrailOn) statement. What happens is that it continues down until it hits the next if-statement which is your if(!TrailOn). Since you set your TrailOn to false in the previous if-statement, this if-statement is correct and therefore will enter it. So it will then set your TrailOn back to true because it fulfills the condition of the if-statement.

Else is only called when the if-statement does not get fulfilled. For example, using your if-statements,

if(TrailOn)
{ // Called when TrailOn = true 
}
else
{ // Called when it does no fulfill above if-statement (if this then do this, else do that) 
}

Whereas you compare to what you currently have

if(TrailOn)
{ // Called when TrailOn = true 
}
if(!TrailOn)
{ // Called when TrailOn = false. This is always called because you set TrailOn to be false in the previous if-statement. (if this then do this, if that then do that) 
}

Ah ok no, i see it now offcourse it will go trough both on keydown and the last if sets it to true so it stays that way.
Darn that i did get that when tasgall explained it in his breakdown! Doh!

Thanks for the patiance dudes.