Thread: if statement question

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    if statement question

    hello, i recently was asked to help a friend with a c++ error he had after he attempted something in a book. am not a pro on c++ myself, were both still learning it

    basically i read over it but could not understand what the problem was so spent a few hours trying to understand it before finally finding what was causing the logic error. ( it always compiled fine)

    this is the original code, it basically draws a square.


    Code:
     
    
    #include <iostream>
    
    int main(){
    
    const int rows = 5;
    const int cols = 5;
    
    std::cout << std::endl;
    
    for(int x=0; x!=rows; ++x){
    
    int y=0;
    
    	while(y!=cols){
    	
    	if(x == 0 || x == rows-1 || 
    		y == 0|| y == cols-1)	{ << bracer i mean this {
    
    			/* will result in constant
    				******* forever when compiled.
    				if the braces following the if
    				and else are removed e.g {}.
    				it works correctly */
    
    			std::cout << "*";
    	} // << bracer i mean this
    	else{ << bracer i mean this
    		std::cout << " ";
    		++y;
    	} // << by bracer i mean these
    
    	
    	} //  end while
    
    	std::cout << std::endl;
    } //  end for
    	return 0;
    
    }
    i have commented what and where the error occurs above. basically by using bracers for his if else statement the flow of the programme my friend had made it work incorrectly which i didnt know was possible.

    i have been told adding bracers to an if else statement is optional and including or not including them changes very little in a program. however in the above programme it can cause it to either work right or not at all.

    can someone please clarify ? what are the braces doing to cause the error?

    thank you in advance

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Braces cause everything within them to run when followed by a true if statement. However, braces are only optional if one command line proceeds the if statement. That means if you have only one semicolon, you can go without brackets, no problem. But if you need more than one line to run under certain consitions, you MUST have brackets. The reason for your error is that without brackets, "++y"; is not included in the conditional statement, thus it will run every time the loop replays. When it is included in the brackets, y will never increment because y will ALWAYS be 0, resulting in the infinite loop.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i have been told adding bracers to an if else statement is optional
    True, but the difference between optional and necessary can be pretty subtle (it still compiles, it still mostly works as expected).

    If you always put them in, then it saves having to think about the issue, or wonder whether you made the right choice or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    If you always put them in, then it saves having to think about the issue, or wonder whether you made the right choice or not.
    Speaking as a long time maintenance programmer, it sure makes maintaining the code easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 08:42 PM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM