How can you get a while loop inside an if statement to compile

This is a discussion on How can you get a while loop inside an if statement to compile within the C Programming forums, part of the General Programming Boards category; I am trying to optimize code so that the embedded system runs faster. Here is my question: Can someone explain ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Exclamation How can you get a while loop inside an if statement to compile

    I am trying to optimize code so that the embedded system runs faster. Here is my question:

    Can someone explain to me why this doesn't compile.
    Code:
    BYTE B;
    B = 1;
    if(while(delay), B){
    ...
    }
    Here the while loop is simply introducing a delay. "delay" is actually a complicated function that eventually returns 0. The details of this is irrelevant. Notice i didn't put any bracket near the while loop or any semicolon but instead i put a comma (because i'm inside an "if" statement, i cannot use a semicolon). We know that the result of the comma separated list is the last element of the list. The result of the argument of the if statement is B. While this doesn't compile, i can create a function that is equivalent:

    Code:
    BYTE func(void)
    {
        BYTE B;
        B = 1;
        whille(delay);
        return B;
    }
    
    Then i can do the following:
    
    if(func()){
    ...
    }
    The latter will compile but i want to avoid a function call because that adds overhead. My ultimate goal is to optimize the code. Why is the function call compiling and not the earlier code. What can i do to make it compile without using a function call? A "for" loop doesn't work either and i cannot use goto statements. I don't want to use machine code at this time unless i don't have a choice. My compiler doesn't allow "inline" functions. Is this even possible?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    A while loop cannot be used as an operand, therefore it can not be used with the comma operator. Your code would be equivalent to:
    Code:
    while(delay);
    if(B) {
        ...
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Thanks bithhub. Wow that turned out to be a simple solution!!! You have no idea how long i have tried to make this work. Now i can make my code much more efficient.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by marcogeneva View Post
    I am trying to optimize code so that the embedded system runs faster. Here is my question:

    Can someone explain to me why this doesn't compile.
    Code:
    BYTE B;
    B = 1;
    if(while(delay), B){
    ...
    }
    Here the while loop is simply introducing a delay. "delay" is actually a complicated function that eventually returns 0. The details of this is irrelevant. Notice i didn't put any bracket near the while loop or any semicolon but instead i put a comma (because i'm inside an "if" statement, i cannot use a semicolon). We know that the result of the comma separated list is the last element of the list. The result of the argument of the if statement is B. While this doesn't compile, i can create a function that is equivalent:

    Code:
    BYTE func(void)
    {
        BYTE B;
        B = 1;
        whille(delay);
        return B;
    }
    
    Then i can do the following:
    
    if(func()){
    ...
    }
    The latter will compile but i want to avoid a function call because that adds overhead. My ultimate goal is to optimize the code. Why is the function call compiling and not the earlier code. What can i do to make it compile without using a function call? A "for" loop doesn't work either and i cannot use goto statements. I don't want to use machine code at this time unless i don't have a choice. My compiler doesn't allow "inline" functions. Is this even possible?
    if(while(delay), B)

    Is this while loop correct because the op says that in a multiple operands in the if loop what gets evaluated is the last element in the list? THEN Can we have an if statement like

    if(func(), func1(), func2(),...., funcn()) and it would be equivalent to

    if(funcn());

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by roaan View Post
    if(while(delay), B)

    Is this while loop correct because the op says that in a multiple operands in the if loop what gets evaluated is the last element in the list? THEN Can we have an if statement like

    if(func(), func1(), func2(),...., funcn()) and it would be equivalent to

    if(funcn());
    You can put several expressions (while() isn't an expression) inside the parentheses of an if, but they will all be evaluated left-to-right. But the only that will be checked for true-or-false is funcn, that part is right.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    You can put several expressions (while() isn't an expression) inside the parentheses of an if, but they will all be evaluated left-to-right. But the only that will be checked for true-or-false is funcn, that part is right.
    Wow, even I didn't know that.
    I just tried this and it worked:
    Code:
    if ( a = 4, b = 3, c = 1, a - b - c )
    Is it guaranteed that the expressions will always be evaluated in order from left to right?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    if(while(delay), B)

    Is this while loop correct because the op says that in a multiple operands in the if loop what gets evaluated is the last element in the list? THEN Can we have an if statement like

    if(func(), func1(), func2(),...., funcn()) and it would be equivalent to

    if(funcn());
    As tabstop mentioned, while() is not an expression. To add to it, because of the comma operator, the multiple operands, are evaluted left to right (associativity of comma operator), and that's why the most right opearnd actually is assigned as in your case of if(funcn()).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cpjust View Post
    Wow, even I didn't know that.
    I just tried this and it worked:
    Code:
    if ( a = 4, b = 3, c = 1, a - b - c )
    Is it guaranteed that the expressions will always be evaluated in order from left to right?
    comma operator has the lowest precedence and thus = is done first and then LtoR associativity. It is guaranteed except when the parantheses are used.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by marcogeneva View Post
    Now i can make my code much more efficient.
    Doubtful - Probably at best you'll make it shorter.
    Given you're still learning the syntax of C, you wont have a clue what it takes to get the compiler to generate the most efficient code. Shorter code isn't necessarily faster, and even adding a line of code without changing any other lines can make something faster. Don't assume that the code written with the while loop in a function will necessarily be any slower at all.
    Write code for clarity; don't prematurely optimise.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Actually to figure out if the code is more efficient, you just need to look at the assembly code generated which is easy. Yes in most cases clarity is important but in this particular case, we can't release the product because of a performance issue so in this situation, optimization takes precedence over clarity. There is no doubt that avoiding a function call makes the code more efficient. When making a function call, variables needs to be put on the stack which takes a few extra clock cycles. Even the most experienced programmer makes a beginners mistake once in a while. I guess i wasn't thinking out of the box.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  2. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 08:04 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  4. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 11:50 AM
  5. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21