Announcement

Collapse
No announcement yet.

what is the difference (if statements)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    what is the difference (if statements)

    why are some 'if' statements followed by { } but others arent ?

    Whats the different between these?

    #2
    Afaik, you only need to use the {} when you have more than one instruction after the 'if' statement.

    So, with just one instruction, no need for {}. Often though, out of habit or good practice, people use the curly braces with just one instruction also.

    Comment


      #3
      If an if statement lacks a {} block following it, it applies to only the next statement.

      Generally I would recommend always using braces, as it helps avoid making this sort of error:

      Code:
      if (Debris(a) != none)
         PlaySound(soundcue'DestructionSound'));
         a.Destroy;
      Which looks innocent but actually destroys everything.

      That said I occasionally don't bother with braces, but only if I'm able to fit things easily on one line and it doesn't feel off. But even then it sort of hides the action at the end of the line where it can difficult to spot on review.

      Code:
      if (a.bIsDestroyable) a.Destroy();

      Comment


        #4
        Okay. Ive always written in {} to keep things formatted.

        Thanks guys

        Comment


          #5
          Just FYI on this issue, all existing code in Unreal "should" follow these standards:
          braces { }

          Brace wars are foul. Epic has a long standing usage pattern of putting braces on a new line. Please continue to adhere to that.

          if - else

          Each block of execution in an if-else statement should be in braces. This is to prevent editing mistakes - when braces aren't used, someone could unwittingly add another line to an if block. The line wouldn't be controlled by the if expression, which would be bad. Worse yet is when conditionally compiled items cause if/else statements to break. So always use braces.

          Code:
          if (bHaveUnrealLicense)
          {
             InsertYourGameHere();
          }
          else
          {
             CallMarkRein();
          }
          A multi-way if statement should be indented with each else if indented the same amount as the first if; this makes the structure clear to a reader:

          Code:
          if (TannicAcid < 10)
          {
             log("Low Acid");
          }
          else if (TannicAcid < 100)
          {
             log("Medium Acid");
          }
          else
          {
             log("High Acid");
          }

          Comment


            #6
            I never put mine in new lines.

            Comment


              #7
              For personal code, I always put starting brace on the line of the opening statement -- the statement opens it, i don't need a brace to tell me it's open. It's like comments that tell you what code does, rather than what it's for or why it does it. I can see what it does, by reading the code.

              Code:
              //increment a
              a++;

              Comment

              Working...
              X