Tell the syntax to execute code if number in range otherwise exit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #16
    Regarding the advantages of using braces with if instructions...
    A common mistake is to forget that the compiler does not see indentation. These two snippets are different:
    Code:
    if (a > 10)
       a += 100;
       a *= 10;
    printf("%d\n", a);
    
    if (a > 10) {
       a += 100;
       a *= 10;
       }
    printf("%d\n", a);
    Even experienced programmers may fail to notice this error in a 500-line source file.

    Comment

    • HolyDoom Witch
      New Member
      • Oct 2012
      • 34

      #17
      Just for the info:
      That example with "if<condition>& &<condition> " is given in the Operators chapter in its section, only, with emphasis on something else inserted into it (but still easily derivable, if one understands what is being said there).

      Now that I will have separate documents even for each section and with all the experimenting in progress, you can say I discovered this number in range-code after KNOWING it. :D

      (Hint: Then I would have posted the same to know if everybody knows this "if-&&" code!;)

      Comment

      • HolyDoom Witch
        New Member
        • Oct 2012
        • 34

        #18
        Ah, forgot to mention one thing though. Well, I discovered it today. Same kind "if" code was not working with "else" that day, but I did not understand this.

        Even if you place too many conditions with ifs before an action, an "else" would need one and ONLY one "if" to work, or it won't work. So rest of the ifs would need be in a brace set starting second condition.

        And as it seems, in a nested "else if":
        Code:
        else if (condition1)
         {if (condition 2)
           action;
         }
        after the first and main "if", the program does not go beyond the first nested "else if", if there are more than one "else ifs". Using if-&& code happens to get you past all the conditions.

        I don't know what the hell is the problem. This language is one hell of a nutty problem I've seen. Because this SHOULD work, especially when the first else-if is working.

        Thanks

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #19
          You may be making this too hard. There is only an if-else.

          Code:
          if (condition)
          {
              ...true
          }
          else
          {
           ....false
          }
          You can have a nested if inside either the true or false block . There is no else-if operator. You get this by omitting braces:

          Code:
          if (condition)
          {
              ...true
          }
          else
          {
            if (condition1)
            {
            }
          }
          which can re-written as:
          Code:
          if (condition)
          {
              ...true
          }
          else
          
            if (condition1)
            {
            }
          and then rearranged since whitespace doesn't count:
          Code:
          if (condition)
          {
              ...true
          }
          else if (condition1)
            {
            }
          and you sre on your way to a real mess. This style of coding (also known as go-to)beomes increasng hard to follow once several conditions are involved. You lose track of which braces go with which if or which else. The only way to follow it is with a debugger.

          The reason for the braces is to enclose multiple statements. When you remove them, the code block stops at the first semi-colon whch limits you to one statement.

          Then there's:
          Code:
          if (10)
          {
              ...true
          }
          else if (condition1)
            {
            }
          you may find that the compiler ignores the else because 10 is always true. It just drops the code from your program since it can never be reached.

          Comment

          • HolyDoom Witch
            New Member
            • Oct 2012
            • 34

            #20
            H! WeaknessForCats

            Thanks for replying, but I think now this is off the topic in this thread. Since we already resolved the title of this thread, I made a new thread here:
            http://bytes.com/topic/c/answers/948...if#post3745814.
            Please paste your reply there.

            Thanks

            Comment

            • HolyDoom Witch
              New Member
              • Oct 2012
              • 34

              #21
              And by the way, one may give as many conditions as they want using Logical Operators, AND use as many logical operators too:
              Code:
              if (condition && condition || condition) action;
              etc.
              ***

              Comment

              Working...