Thread: IF Statement-- With curly brackets or semicolon

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    IF Statement-- With curly brackets or semicolon

    Hello,

    I couldn't understand how does this "else" belong to inner "if" other than outer if when we dont use curly brackets or semicolon after outer if?

    Code:
    #include <stdio.h>
    
    int main (void) {
    
    
    	int x=4,y=2,z=3;
    	if(x<y)
    		if(x<z) 
    			printf ("Minimum is x1 ");
    		else 
    			printf ("Minimum is z2 ");
    	
    	
    	return 0;
    }
    //NO WRITTEN OUTPUT
    Code:
    #include <stdio.h>
    
    int main (void) {
    
    
    	int x=4,y=2,z=3;
    	if(x<y);
    		if(x<z) 
    			printf ("Minimum is x1 ");
    		else 
    			printf ("Minimum is z2 ");
    	
    	
    	return 0;
    }
    //OUTPUT: Minimum is z2
    Code:
    #include <stdio.h>
    
    int main (void) {
    
    
    	int x=4,y=2,z=3;
    	if(x<y){
    	
    		if(x<z) 
    			printf ("Minimum is x1 ");}
    		else 
    			printf ("Minimum is z2 ");
    	
    	
    	return 0;
    }
    //OUTPUT: Minimum is z2
    [/CODE]

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Hint: The compiler doesn't understand, or better yet doesn't care about, indentation. The rule is that "else" is associated with the "if" that is tied to the most recent block of code.
    Devoted my life to programming...

  3. #3
    Registered User Bogdokhan's Avatar
    Join Date
    Jan 2016
    Location
    Belgium
    Posts
    8
    Hi,
    x is always smaller than y in your example.
    First code : nothing wil happen because your first if returns false : the next if..else is not executed
    Second code : no matter what the result of your first if because you finish its block with ";"
    Code:
    if(x<y);
    . x is also not smaller than z so the answer is z2
    Third example : the block between curly braces will not be executed because x is not smaller than y. Than comes the "else" block which is well executed

    Hugues

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Thank you both, but one point isnt clear yet for me.

    If we dont ue semicolon or curly bracket after we use an "if statement" the first line after "if" belongs that "if". (1)

    I used an "if" again. (2)

    I wrote one line code belonging (2) .....(3)

    I wanna write an "else statement" now next line. .....(4)

    So, how can (4) belong to (2)?
    IF Statement-- With curly brackets or semicolon-ads-z-png

  5. #5
    Registered User Bogdokhan's Avatar
    Join Date
    Jan 2016
    Location
    Belgium
    Posts
    8
    Without curly braces "blocks", the else belong to the last if.
    A rule I always use is to always make curly braces blocks, even for one line : it makes the code clear for anyone (included your compiler)

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You simply need to explicitly say that 2, 3 and 4 belong to 1, by putting curly braces around them( and "5", I guess ).
    Last edited by GReaper; 01-10-2016 at 06:38 AM.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Okay, thank you GReaper and Bogdokhan for giving a time for the question, I got it. Have a goood day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. missing semicolon?
    By hit in forum C Programming
    Replies: 2
    Last Post: 08-17-2012, 12:59 AM
  2. Curly Brace Placement
    By sean in forum General Discussions
    Replies: 96
    Last Post: 06-16-2009, 10:08 PM
  3. is curly braces here is a must?
    By mashour06 in forum C Programming
    Replies: 8
    Last Post: 04-25-2009, 04:41 PM
  4. Curly brace question
    By kenryuakuma in forum C++ Programming
    Replies: 12
    Last Post: 12-16-2008, 07:06 PM