Thread: Parentheses in IF statements.

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Parentheses in IF statements.

    My question regards the following code.
    Code:
    	
    while (--a >= 0)
    	{
                    f++;
    		if (f >= g)
    			f = 0;
    		h1 = 2;
    		if (m)
    			h2 = 2;
    	}
    If the IF statement is false, is the following line, "f = 0," skipped? Are the next two lines skipped? or are the next four line skipped?

    Could some one rewrite the code with Parentheses so that is more clear?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    
    if (something something something)
    Do everything up to the first semicolon ;  <- Only this line is executed in the if statement
    
    //or
    
    if (something something something)
    {
    do stuff;
    as;
    much;
    as;
    you;
    want;
    } //End of the if statement. If the if statement was true, it does everything upto this point
    ..
    Code:
    while (--a >= 0)
    {
        
    	
    	f++;
    
    	if (f >= g)
    	{
    		f = 0;
    
    	}//End of if
    
    
    	h1 = 2;//This is always ran in while loop, it is not in any if's
    
    	if (m)
    	{
    
    	h2 = 2;
    
    	}//End of 2nd if.
    
    }//End of while-loop
    Last edited by Enahs; 11-30-2005 at 12:08 PM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    When you don't use brackets around an if statement only the next statement is considered to be in it. There for if your if statement
    Code:
    if (f >= g)
    was false, then f would not be set equal to zero, but everything after that would still happen, like h1 would be set to 2.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The indenting gives a clue about what belongs to the if statement and what doesn't:
    Code:
    while (--a >= 0)
    {
    	f++;
    	if (f >= g)
    		f = 0;
    	h1 = 2;
    	if (m)
    		h2 = 2;
    }
    Here it is rewritten:
    Code:
    while (--a >= 0)
    {
    	f++;
    	if (f >= g)
    	{
    		f = 0;
    	}
    	h1 = 2;
    	if (m)
    	{
    		h2 = 2;
    	}
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Terminology: braces are {}, brackets are [] (unless further qualified) and parentheses are ().

    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Braces, that's the one I was looking for.

    ... what a ball-buster.

    Though, I've heard on many occasions braces called brackets and brackets called square-brackets.

    ...and how the hell do I post braces and brackets outside of code tags? I keep getting the "Format your code properly" error. There must be something I can uncheck or something.
    Last edited by SlyMaelstrom; 11-30-2005 at 01:10 PM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by SlyMaelstrom
    ...and how the hell do I post braces and brackets outside of code tags? I keep getting the "Format your code properly" error. There must be something I can uncheck or something.
    I put a space after the bracket, such as [ size ] You should also scroll down the page and check the box "disable simleys in text".

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I usually put an empty set of code tags in my post.

    [code][/code]
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah that's a good idea. Thanks.
    Sent from my iPadŽ

  10. #10
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Is there a way to implement syntax coloring within
    Code:
    dfsfdsdfsdf
    , that would be great...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There are some tools that will generate BBcode markup for syntax highlighting code. You put the code there, transform it, and copy the result into the post.

    There's also the build-in PHP syntax coloring, which you can access with the php tag, but it only really works for PHP, and doesn't highlight many of C++'s keywords.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    If you use FireFox and grease monkey you can easily make it auto insert the color tags within the code tag for whatever you want.
    http://greasemonkey.mozdev.org/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. having problems with my if statements
    By bajanstar in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 01:39 PM