Possibility to execute both if and else together

Discussion in 'C' started by anand12, May 1, 2008.

  1. anand12

    anand12 New Member

    Joined:
    May 1, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi Friends..,
    I m new to this forum as well as C. Is it possible to execute both If... & Else... at the same time? Thanks in advance.




    With Regards,
    Anandhakumar
     
  2. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    That's not the point of if-else statements, so I don't know why you'd want to do it...

    But if you want to get technical, I don't think it is possible. If you were really set on it, you could maybe nest an if-else statement, I guess.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Use goto. From if's last statement jump to a statement using goto.
     
  4. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    but sir , using of Goto is not a good sort of coding for a problem.......... :undecided:smug:
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Then do not try stuff like executing if and else. They also could not be considered good
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, you can't, because the whole point of if/else is to do one thing if an expression is TRUE and another if the expression is FALSE. If you want to execute both blocks of code then you need to rejig the expression, for example instead of
    Code:
    if (test) { ... }
    else { ... }
    
    which is almost equivalent to
    Code:
    if (test) { ... }
    if (!test) { ... }
    
    (almost, because in this construction you could set test to FALSE in the first if block and cause the second to be executed), whereas if/else doesn't allow for that) you could use
    Code:
    if (test || do_both) { ... }
    if (!test || do_both) { ... }
    
     
  7. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    that's also correct sir.....:nice::nice::nice:
     
  8. syed_nit

    syed_nit New Member

    Joined:
    Aug 13, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    #include<stdio.h>
    #include<unistd.h>
    int main()
    {
    pid_t pid;
    pid = fork();
    if(pid==0)
    {
    printf("\nIF IS RUNNING\n");
    }
    else
    {
    printf("\nELSE IS RUNNING\n");
    }
    return 0;
    }
     
  9. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    yes i think i remember there is a code exist for it though i don't remember the code now

    There is not any logical explanation for it but i think this code is a part of class room teaching in indian colleges.

    teachers consider this question as sacred

    you can find this question in every year question paper of our college.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice