Modifying the if statement but problem with braces.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebouh
    New Member
    • Feb 2007
    • 77

    Modifying the if statement but problem with braces.

    hi guys.
    Another problem facing in my profiler program is with branches.
    I need to change this:
    Code:
    x = 5;
         y = x + 2x + 7;
         if (x > y)
               printf(“x is %d\n”, x);
        else
              printf(“y is %d\n”, y);
    to this:
    Code:
         x = 5;
         y = x + 2x + 7;
         if (x > y) { branch[0]++;
               printf(“x is %d\n”, x);
        }
        else { branch[1]++;
              printf(“y is %d\n”, y);
         }
    The problem is when i read an "if" line, i have to check include thebraces since i'm adding another statement that must be inside the if. Now i know the braces won't be there for the above one, but what if i have a :
    Code:
    if (....)
                if (.....)
                 {
                   ....
                 }
    How would i know when to close the matching braces that i opened??
    Can anyone think of a way?

    Thank you.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Sebouh-

    Could you please clarify your qestion? It's a good idea to put all loop and conditional statements in brackets, just in case you want to go back later and add something, and if statements, if you don't have the brackets, will execute only the statement on the next line (however, you can do chains of if - if - if statements, but it's not good practice). I would suggest including everything you want to be done when an 'if' statement is executed inside brackets, but again, I'm not sure I understand your question...

    Comment

    • Sebouh
      New Member
      • Feb 2007
      • 77

      #3
      Originally posted by sicarie
      Sebouh-

      Could you please clarify your qestion? It's a good idea to put all loop and conditional statements in brackets, just in case you want to go back later and add something, and if statements, if you don't have the brackets, will execute only the statement on the next line (however, you can do chains of if - if - if statements, but it's not good practice). I would suggest including everything you want to be done when an 'if' statement is executed inside brackets, but again, I'm not sure I understand your question...
      OKay, let me explain better.
      I want to count how many times the branch is taken, that's why i add a branch[i]++. I can always open a braket, put the branch thing there with the other statements and then close the braket. But i don't know when to close the braket. When do the statements (or chain of ifs) end? I need to know when they end so that i can let's say close the first if's braket.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by Sebouh
        OKay, let me explain better.
        I want to count how many times the branch is taken, that's why i add a branch[i]++. I can always open a braket, put the branch thing there with the other statements and then close the braket. But i don't know when to close the braket. When do the statements (or chain of ifs) end? I need to know when they end so that i can let's say close the first if's braket.
        In an if statement, it's either all done, or not. It ends where you want it to, after everything has been done that you want to happen if the selected branch has been taken:

        Code:
        if (conditional)
            // only this statement is done
            // this one is not, even though it is indented
        
        if (conditional2) {
            // this statement is done
            // and this one is too
            // and all the ones up until the last bracket
        }
        // and this one is done, because the conditional is over, and it's the next after the if
        
        if (false_condition)
            // this statement isn't done, because it's false - it never enters the 'if'
        
        if (false_condition2) {
            // this isn't done
            // and neither are any of these
            // until after the bracket is closed again
        }
        // but this one is, because it's not in the if
        Your branch[i]++ will work, but I'm not sure what you're saying about 'ending' if statements, you want to end them, when you're done doing things that relate specifically to that branch.

        Does that answer your question?

        Comment

        • stroken
          New Member
          • Mar 2007
          • 24

          #5
          Originally posted by sicarie
          ...
          Code:
          if (conditional)
              // only this statement is done
              // this one is not, even though it is indented
          ...
          ...
          This should be
          Code:
          if (conditional)
              // only this statement is done
              // this one is always done, its outside the scope

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by stroken
            This should be
            Code:
            if (conditional)
                // only this statement is done
                // this one is always done, its outside the scope
            Ah, good catch! Thanks stroken - I did mean that it was not done as part of the if statement - it was outside it (which would mean it is always done). Thanks again.

            Comment

            • Sebouh
              New Member
              • Feb 2007
              • 77

              #7
              Maybe i'm being unclear.
              Ok, what my program must do is modify the C code it get's as input. An example of that modification is to take this code:
              Code:
              if (true)
                  <statement>;
              and produce this code:
              Code:
              if (true)
              {
                  branch[i]++;
                  <statement>;
              }
              But if i wan't to modify this:
              Code:
              if (true)
                  if (true)
                  {
                      <stmt1>;
                      <stmt2>;
                  }
              I have to add a { to the first if, and add a } at it's end, to make the branch[i]++ part of this if.
              The problem i'm facing is: When do i know where to insert the }? It' not the case that the first is has 1 line under it.

              I think i'm starting to come up with a solution. Please, tell me if this would work.
              If the first if has no braces, i would add one, and update a brace-stack. Everytime i get a {, i'd push one, and pop it if i get a matching } and at the end when the stack is left with a {, i would insert the } in the code.

              Comment

              • Sebouh
                New Member
                • Feb 2007
                • 77

                #8
                Well after some analysis, i've come to the conclusion that the stack solution doesn't work either. There is the question on when to check for 1 left "{". with chained ifs where the last if contains a large ( > 2 ) body, during the procssing of the body, i wiil en dup checking the stack and inserting a "}" there, leading to:
                Code:
                if (...)
                {
                    if (....)
                    {
                    }                  // checked for single { and found true, so inseted a }
                        <stmt>
                        <stmt>
                    }
                And after more analysis and brainstorming, i've come up with a possible solution that includes recursion. I'll let you know if it worked... ;)

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Do you know if the code will be properly formatted? You could do it based on indentation, though that would not work with anything that isn't properly indented...

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    @OP: did you read my last reply in your other thread ?

                    kind regards,

                    Jos

                    Comment

                    • Sebouh
                      New Member
                      • Feb 2007
                      • 77

                      #11
                      Well many of the stuff i'm doing work only with proper formatting, not indentation wise but proper location of braces,proper statements (not 10 on 1 line)...

                      JosAH: I did read your last post, but i'm not familiar with the concept. So if i have to read about them and learn, i'm not sure it will be a shortcut. I'm due friday on this assignment and i'm not half way there yet. Besides, i don't think the instructor expects me to work with those, so i'm playing it safe.
                      However thanks for that reply, and i will be reading about them as soon as i get the chance.

                      Comment

                      Working...