I cant understand if else statement in timerScript.ive spent few hours+chatGPT

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimerScript : MonoBehaviour
{
[SerializeField] float timeToAnsweringQuestion;
[SerializeField] float timeToShowCorrectAnswer;
public bool isAnsweringQuestion; //DEFAULT bool is false;
float timerValue;
void Update()
{
TimerCountingUpOrDown();
}

private void TimerCountingUpOrDown()
{
    timerValue -= Time.deltaTime;
    if (isAnsweringQuestion) // if  why it didnt get executed firsy?, the default value is false,right? 
    {                                       
        if(timerValue <= 0)
        {
          isAnsweringQuestion = false;
          timerValue = timeToShowCorrectAnswer;
        }
        
    }

    else
    {   if (timerValue <= 0)
        {
          isAnsweringQuestion = true; //but after isAnsweringQuestion set to true,it execute the if Block
            timerValue = timeToAnsweringQuestion; ///////////////////////////////////////////////////////////////////
        }
    }

    

    Debug.Log(timerValue);
}

}

but if i set ā€œif (isAnsweringQuestion==true)ā€, the script work normally, if i set ā€œif (isAnsweringQuestion==false)ā€, it will loop to only on timeToShowCorrectAnswer

but it never stated that if (isAnsweringQuestion) is either true or false. in fact by defau,lt, and according to this course, if (isAnsweringQuestion) is equal to (isAnsweringQuestion==false)"

so why it excute as if it was written ā€œif (isAnsweringQuestion==true)ā€? it never stated in the scrip. im never been this conffused in this course until now. please help!

timerValue is 0 the first time Update() executes. Itā€™s the default value for a float. So, the first thing that happens is that we subtract Time.deltaTime from it. Now it is < 0.

Then we check if isAnsweringQuestion is true. Itā€™s not, so we execute the else part of the statement. In there we check if timerValue <= 0. It is, so we set isAnsweringQuestion = true.

Now, the next time Update() executes, isAnsweringQuestion == true and we enter the first if statement

1 Like

I still donā€™t understand, in the first iteration, tge if block is (isAnsweringQuestion). And the bool isAnsweringQuestion is false(it is by default )

Why if block with (isAnsweringQuestion) not executed first?

I mean i know if it get executed first it wont work as intended. But how is it possible it just skipped the first if block on first update?

I can come uo with my other method(maybe my method is inferior version of this) , but i just want to know how this if else logic used in this script work. Im so confused i canā€™t sleep. Itā€™s 1.30 A.M. here

because the value is false. if statements execute when the value is true. ā€˜if the value is true, do this, else do something elseā€™. The value is not true, itā€™s false. So we skip the if

1 Like

Wait, i think we have misunderstanding here. Isnā€™t it supposed to execute if the value is true?
Thus if the value equal, so it is true, and it will get executed

Like, if timerValue<=0, it will execute the code block because the value is equal, thus itā€™s true. Not because the value is false or true, but because the condition are met A.K.A. equal

Like for example, when i set if (isAnsweringQuestion) to if(isAnsweringQuestion==false), it will stay looping in the first if block only. Because the value of isAnsweringQuestion=false. Thus, the code in the first if block get executed

But, when i set it to
if(isAnsweringQuestion==true), it will run as intended , like, as if ā€œtrueā€ is the default value of if(isAnsweringQuestion) unless i make it ā€œfalseā€?

But isnā€™t the default value in the bool isAnsweringQuestion variable that i created earlier is false?

Like, if timerValue<=0, it will execute the code block because the value is equal, thus itā€™s true. Not because the value is false or true, but because the condition are met A.K.A. equal

If statements execute when the result of the expression (condition) is true.

If timerValue is 3 then the expression timerValue <= 0 is false because timerValue is not less than, or equal to 0. If timerValue is -3 then the expression timerValue <= 0 is true because timerValue is indeed less than or equal to 0.

isAnsweringQuestion is false. This is the default value of a boolean. So the expression isAnsweringQuestion is false because isAnsweringQuestion is false. It is the same as saying isAnsweringQuestion == true. It is not equal true, because isAnsweringQuestion is false. Therefore the result of the expression is false.

If you replace the variable with its value, it may make more sense.
isAnsweringQuestion is false (default), so we are saying if (false) do something. It will not do something because the if only executes when the expression is true: if (true) do something.

This is correct behaviour. isAnsweringQuestion is not equal to true, so your first if if(isAnsweringQuestion == true) is false. So, donā€™t execute this block


Edit
Run the following code and you will see what it does

public class Test: MonoBehaviour
{
    bool isAnsweringQuestion;
    private void Start()
    {
        if (isAnsweringQuestion)
        {
            Debug.Log("1: Expression is true");
        }
        else
        {
            Debug.Log("1: Expression is false");
        }

        if (isAnsweringQuestion == true)
        {
            Debug.Log("2: Expression is true");
        }
        else
        {
            Debug.Log("2: Expression is false");
        }

        if (isAnsweringQuestion == false)
        {
            Debug.Log("3: Expression is true");
        }
        else
        {
            Debug.Log("3: Expression is false");
        }
    }
}

This will write the following in the console:

1: Expression is false
2: Expression is false
3: Expression is true
2 Likes

WOW.thank you so much. thanks for the debug idea. i usulaly also did debug to know how the code works,or if it working as intended, but i didnt even do that because i feel very confused

i got so confused by this actually simple logic that isAnsweringQuestion and isAnsweringQuestion == false are not the same.

i got so confused that i forgot to use Debug.Log. oh, i feel like banging my head to the wall because of
how stupid i am

i feel like idiot here.but now i understand

Oh, now I see what your confusion was. Yes, the expression (condition) has to be taken as a whole

if (condition) action

isAnsweringQuestion is the whole condition and is false, but isAnsweringQuestion == false is also a whole condition and because they are equal, the result is true

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms