Curly braces needed

I didn’t know this in C#. Without curly braces my code would count into minus figures and break. One way was to add braces and it worked okay.

        if (isAnswering)
            if (time <= 0) { time = timeForAnswer; isAnswering = false; Debug.Log("Asking..."); }
        else
            if (time <= 0) { time = timeToAnswer; isAnswering = true; Debug.Log("Show answer..."); }

C++ will work if the code looked like this.

Any way C++ would not work this way?

It’s because of the nestedness of the if’s. The code you posted actually look like this (braces and expanded to show the actual code)

if (isAnswering)
{
    if (time <= 0)
    {
        time = timeForAnswer;
        isAnswering = false;
        Debug.Log("Asking...");
    }
    else if (time <= 0)
    {
        time = timeToAnswer;
        isAnswering = true;
        Debug.Log("Show answer...");
    }
}

and tbh, I don’t think it would work differently in C++. But I’m no expert.

The rule to remember is “else goes with the closest if” unless braces cause it to be otherwise. The indentation of the code in the OP obscures that fact. yep, it’s bitten me before too (in C, which I programmed before I knew about C++ or C#).

1 Like

Good advice. The C++ code (I lied) was more like this:

if(a==0)
    if(b==1) { a=2; b=3; std::cout << "changed"; }

so I forgot to add the else clause. I tested and yup, I will remember, “else goes with closet if”.

I do know that Python works with indentation. If anyone knows Python, would my OP work as expected?

Yes, python would be different (colons) but I believe the structure would work

if isAnswering:
    if time <= 0:
        time = timeForAnswer
        isAnswering = False
        print("Asking...")
else:
    if time <= 0:
        time = timeToAnswer
        isAnswering = True
        print("Show answer...")

This is because Python is a functional programming language and relies on indenting to determine where statements belong.

in C# and C++, it doesn’t care about indenting or if statements are on the same line. Other programming languages such as Fortran, Pascal and Visual Basic to name a few offer some kind of begin and end to a statement for scope

So, you can do the following in C++/C#

if (statement is true) do a;do b;
while (statement is true) do c; do d;

a will only be done if the statement is true, b will always be done.
Same with the while - while true, c will be executed and d will be executed on finishing the loop

It is good practice to not put everything on one line and always use { } in your statements like if, for and while loops which will prevent issues like in the above example.

again, rather than do

if (statement is true) { do a;do b; }

which is actually perfectly valid, do this:

if (statement is true) 
{
  do a;
  do b;
}

This is far easier to read and debug, easier to add extra statements and less error prone.

These all apply to both C# and C++.

EDIT: In fact, thinking about it, all c-style languages such as JavaScript as well as Java follow the same rules.

1 Like

I hate to be “that guy” but python is not a functional programming language. It’s a multi-paradigm language that includes ‘functional’ just like C# and C++ does.

It is true, though, that python relies on indentation for all its scope, in the same way that C# and C++
(and other c-style languages) relies on scope either being a single statement in some cases, or encapsulated in curly braces

A language like Haskell is functional? From what I know it’s not a friendly language and is geared more towards mathematicians, not game developers for sure.

Yes, Haskell is purely functional

The point was, python, like F#, relies on indenting to define scope. I give you that it can be object oriented and procedural but ultimately it is a glorified loosely typed scripting language (which I use almost every day I might add, both for TDD and electronics)

C++ and most, if not all C based languages do not give two hoots about indenting and that is down to readability only. The most spectacular abuse of this ability to not use indents was a C program that was an entire chess program written in under 1kb of source code. Something like this would be impossible to do in python.

Privacy & Terms