Thread: if conditions

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    if conditions

    Hi, pertaining to 'if conditions' and all its statements like "else, else if, if", if one condition of whole conditions thats written in the script(lets say there's more than two "if" conditions) achieved then the program will not continue to another condition however if there's another "if condition" that the program can achieve it.. right? (see the example below to be more simply-btw the code is just for understanding so don't focus of what it prints)
    Code:
    #include<stdio.h>
    int main()
    {
    	int num=2,i=0;
    	if(num<3)
    		printf("hello");
    	else if (num<5)
    		printf("hh");
    	else
    		printf("No");
    	return 0;
    }
    In my case, the code achieves the first "if" ofcourse, because the variable num is smaller than 3, so it prints on my screen "hello", however we can easily see it also achieves the second "else if", but the code doesn't print "hh" in my screen and thats weird for me!! because from my knowledge the "else if" and "if" equivalent to each other so in my case the program must enter the second condition also...am I right? if not so, then can anyone please me explain what're the differences between them? thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by RyanC
    In my case, the code achieves the first "if" ofcourse, because the variable num is smaller than 3, so it prints on my screen "hello", however we can easily see it also achieves the second "else if", but the code doesn't print "hh" in my screen and thats weird for me!! because from my knowledge the "else if" and "if" equivalent to each other so in my case the program must enter the second condition also...am I right? if not so, then can anyone please me explain what're the differences between them? thanks in advance.
    It is conventional to write an if-else chain the way that you did (though I would use braces anyway even though they are unnecessary here), but allow me to rewrite the same if-else chain:
    Code:
    if (num < 3) {
        printf("hello");
    } else {
        if (num < 5) {
            printf("hh");
        } else {
            printf("No");
        }
    }
    Now, it should be obvious why the second condition cannot be entered should control enter the if branch of the first if statement: it lies within the else branch of the first if statement!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    [Edit]
    By a matter of seconds...
    [/Edit]

    I'll try for a simple explanation first.

    The easiest way to explain the behavior is "The C++ language has no such `else if` branch.".

    Believe it or not, the C++ doesn't have an `else if` branch.

    Do you know how you can use a single statement or a block of statements after an `else` branch?

    Do you know that the `if` branch and associated statement or block of statements is really just a statement?

    *shrug*

    Do you understand?

    Your code can be differently written to show how the language behaves.

    Code:
    #include<stdio.h>
    int main()
    {
        int num=2,i=0;
        if(num<3)
            printf("hello");
        else
        {
            if (num<5)
                printf("hh");
            else
                printf("No");
        }
        return 0;
    }
    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    It is conventional to write an if-else chain the way that you did (though I would use braces anyway even though they are unnecessary here), but allow me to rewrite the same if-else chain:
    Code:
    if (num < 3) {
        printf("hello");
    } else {
        if (num < 5) {
            printf("hh");
        } else {
            printf("No");
        }
    }
    Now, it should be obvious why the second condition cannot be entered should control enter the if branch of the first if statement: it lies within the else branch of the first if statement!
    Very clearly-on touch, thank you!

  5. #5
    Registered User Euclid365BCE's Avatar
    Join Date
    Jul 2015
    Location
    USA
    Posts
    33
    Quote Originally Posted by RyanC View Post
    Very clearly-on touch, thank you!
    Also, isn't the if-else logic that laserlight visually represented such that IF relational expression is TRUE, then following statement executes; ELSE, next relational expression is evaluated. In short: the system doesn't know to evaluate multiple expressions in the first IF statement; hence, it stops executing when first expression evaluates to TRUE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some questions about conditions
    By student111 in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2012, 02:47 AM
  2. if and while conditions
    By firehydrant in forum C Programming
    Replies: 12
    Last Post: 03-05-2011, 03:07 AM
  3. How to set conditions for a input?
    By QuaX in forum C Programming
    Replies: 7
    Last Post: 04-20-2009, 05:05 AM
  4. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM
  5. While Loop Conditions?
    By ted_smith in forum C Programming
    Replies: 8
    Last Post: 01-17-2005, 10:20 PM