473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Modifying the if statement but problem with braces.

78
hi guys.
Another problem facing in my profiler program is with branches.
I need to change this:
Expand|Select|Wrap|Line Numbers
  1. x = 5;
  2.      y = x + 2x + 7;
  3.      if (x > y)
  4.            printf(“x is %d\n”, x);
  5.     else
  6.           printf(“y is %d\n”, y);
  7.  
to this:
Expand|Select|Wrap|Line Numbers
  1.      x = 5;
  2.      y = x + 2x + 7;
  3.      if (x > y) { branch[0]++;
  4.            printf(“x is %d\n”, x);
  5.     }
  6.     else { branch[1]++;
  7.           printf(“y is %d\n”, y);
  8.      }
  9.  
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 :
Expand|Select|Wrap|Line Numbers
  1. if (....)
  2.             if (.....)
  3.              {
  4.                ....
  5.              }
  6.  
How would i know when to close the matching braces that i opened??
Can anyone think of a way?

Thank you.
Apr 16 '07 #1
10 1388
sicarie
4,677 Expert Mod 4TB
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...
Apr 16 '07 #2
Sebouh
78
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.
Apr 16 '07 #3
sicarie
4,677 Expert Mod 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. if (conditional)
  2.     // only this statement is done
  3.     // this one is not, even though it is indented
  4.  
  5. if (conditional2) {
  6.     // this statement is done
  7.     // and this one is too
  8.     // and all the ones up until the last bracket
  9. }
  10. // and this one is done, because the conditional is over, and it's the next after the if
  11.  
  12. if (false_condition)
  13.     // this statement isn't done, because it's false - it never enters the 'if'
  14.  
  15. if (false_condition2) {
  16.     // this isn't done
  17.     // and neither are any of these
  18.     // until after the bracket is closed again
  19. }
  20. // but this one is, because it's not in the if
  21.  
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?
Apr 16 '07 #4
stroken
24
...
Expand|Select|Wrap|Line Numbers
  1. if (conditional)
  2.     // only this statement is done
  3.     // this one is not, even though it is indented
  4. ...
  5.  
...
This should be
Expand|Select|Wrap|Line Numbers
  1. if (conditional)
  2.     // only this statement is done
  3.     // this one is always done, its outside the scope
  4.  
Apr 16 '07 #5
sicarie
4,677 Expert Mod 4TB
This should be
Expand|Select|Wrap|Line Numbers
  1. if (conditional)
  2.     // only this statement is done
  3.     // this one is always done, its outside the scope
  4.  
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.
Apr 16 '07 #6
Sebouh
78
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:
Expand|Select|Wrap|Line Numbers
  1. if (true)
  2.     <statement>;
and produce this code:
Expand|Select|Wrap|Line Numbers
  1. if (true)
  2. {
  3.     branch[i]++;
  4.     <statement>;
  5. }
But if i wan't to modify this:
Expand|Select|Wrap|Line Numbers
  1. if (true)
  2.     if (true)
  3.     {
  4.         <stmt1>;
  5.         <stmt2>;
  6.     }
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.
Apr 17 '07 #7
Sebouh
78
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:
Expand|Select|Wrap|Line Numbers
  1. if (...)
  2. {
  3.     if (....)
  4.     {
  5.     }                  // checked for single { and found true, so inseted a }
  6.         <stmt>
  7.         <stmt>
  8.     }
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... ;)
Apr 17 '07 #8
sicarie
4,677 Expert Mod 4TB
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...
Apr 17 '07 #9
JosAH
11,448 Expert 8TB
@OP: did you read my last reply in your other thread ?

kind regards,

Jos
Apr 17 '07 #10
Sebouh
78
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.
Apr 17 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: Albert | last post by:
Why doesn't: #include <stdio.h> void reverse(char, int); main() { char s;
13
by: Robin Becker | last post by:
When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous. Is the following idiom dangerous or unpythonic? def func(a): global func, data data =...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
12
by: reycri | last post by:
While the following is allowed: if (a == b) SomeFunction(); else OtherFunction(); The following is not: try
0
by: davidz | last post by:
My office has adopted Team Foundation Server to track and manage the development and deployment process at our site. I have been asked to modify one of the standard work item reports, "Related...
4
by: _mario.lat | last post by:
for struct: struct in6_addr { uint8_t s6_addr; }; is provided a costant: #define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}} what does means {{{, and }}}?
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
6
by: lasing | last post by:
Hi all, recently encounter following bugs: if (a b); //<- bug in the careless ";" here return 0; Due to the extra ";" in the if statement line, it always return 0. Anyone know g++...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.