CrazyEngineers
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • pradeep_agrawal

    MemberFeb 18, 2007

    DEP
    give me a one line code to execute if && else statements simultaneously.



    dont use a ; after else statement !!!
    😒 So your question is to write a C/C++ code in which the body of both if and else gets executed. Correct me if I am wrong.
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberFeb 19, 2007

    Hi DEP,

    Can you please elaborate your question. Its not yet very much clear to me as to what exactly needs to be done? IF and ELSE executing simultaneously, can't get what does that mean !!! Please excuse me if I am asking a foolish question😁!!!!!!!!!


    Thanks and Regards,
    Mayank Shukla
    Are you sure? This action cannot be undone.
    Cancel
  • reachrkata

    MemberFeb 21, 2007

    If I understood the question right, are you looking for the ?: operator - it does the same thing as an if and else but in 1 line.
    Are you sure? This action cannot be undone.
    Cancel
  • Saket Mishra

    MemberFeb 23, 2007

    hey u can use the ? operator. It is called conditional operator.there are two conditional operators supported ? and :

    for example

    int x,y;
    scanf("%d",&x);
    y=(x>5?3:4);
    Are you sure? This action cannot be undone.
    Cancel
  • Saket Mishra

    MemberFeb 23, 2007

    hey u can use the ? operator. It is called conditional operator.there are two conditional operators supported ? and :

    for example

    int x,y;
    scanf("%d",&x);
    y=(x>5?3:4);

    keep asking.
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberFeb 24, 2007

    pradeep_agrawal
    😒 So your question is to write a C/C++ code in which the body of both if and else gets executed. Correct me if I am wrong.
    firstly i apologise for not putting up my question clearly.
    i rephrase my question.

    "a c++ code whiich executes both 'if' and 'else' statements simultaneously.

    if the code is

    if(a==b)
    cout<<"true\n";
    else
    cout<<"false";

    OUTPUT should be:-

    true
    false


    am i clear now???
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberFeb 24, 2007

    Saket Mishra
    hey u can use the ? operator. It is called conditional operator.there are two conditional operators supported ? and :

    for example

    int x,y;
    scanf("%d",&x);
    y=(x>5?3:4);

    keep asking.
    hey saket,
    i know the conditional operator and i am sure that you know that its functionality is same as that of if/else. please read my question again in the new thread i have posted and my point will be clearer.

    thanks
    Are you sure? This action cannot be undone.
    Cancel
  • maheshkrishnan

    MemberMar 29, 2007

    Hi,

    There is way to make both "if " and "else" parts,like

    if(a==b)
    cout<<"true\n";
    else;
    cout<<"false";

    execute this you will get both the If and ELSE part.

    Thanks,
    mahesh krishnan
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberMar 30, 2007

    maheshkrishnan
    Hi,

    There is way to make both "if " and "else" parts,like

    if(a==b)
    cout<<"true\n";
    else;
    cout<<"false";

    execute this you will get both the If and ELSE part.

    Thanks,
    mahesh krishnan
    hi mahesh,
    well the thing is that when i was asked this question..i gave the same answer.But then they said that the question is to executing both ie and else statements simultaneously and by terminating the "else" with a semicolon the following statement "false " is no more a part of "else". So the answer isnt acceptable.

    please refer to the very first thread ,there i have explicitly mentioned not to use a semicolon after else.
    Are you sure? This action cannot be undone.
    Cancel
  • reachrkata

    MemberApr 1, 2007

    I dont know about C++, but may be this works in C -

    if (!(printf("TRUE"))
    ;
    else
    printf("FALSE");

    Does the same also work in C++ using cout ?
    😕
    Are you sure? This action cannot be undone.
    Cancel
  • maheshkrishnan

    MemberApr 1, 2007

    Hi DEP,
    I hope it is just a Crazy question and should not comes in any part of programs.
    Any way I will try one more time with a Old fassion of programming...

    if (1)
    {
    loop😛rintf("TRUE\n");
    goto loop2;
    }
    else
    {
    goto loop;
    loop2: printf("FALSE\n");
    }

    Is this acceptable., Letz c.

    Thanks,
    Mahesh Krishnan
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberApr 3, 2007

    maheshkrishnan
    Hi DEP,
    I hope it is just a Crazy question and should not comes in any part of programs.
    Any way I will try one more time with a Old fassion of programming...

    if (1)
    {
    loop😛rintf("TRUE\n");
    goto loop2;
    }
    else
    {
    goto loop;
    loop2: printf("FALSE\n");
    }

    Is this acceptable., Letz c.

    Thanks,
    Mahesh Krishnan


    hie mahesh,

    GREAT job !!! congratulations..😁
    keep up the good work,your answer is acceptable.
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberApr 3, 2007

    reachrkata
    I dont know about C++, but may be this works in C -

    if (!(printf("TRUE"))
    ;
    else
    printf("FALSE");

    Does the same also work in C++ using cout ?
    😕

    hi reachrkata,

    well your code is pretty cool and yes it works equally well in c++.
    but there is a hitch. See the question eas to execute the statement following "if" and not the test condition of "if". i am not very much sure whether the code is acceptable but i will surely let you as early as possible.


    but i must say GREAT WORK DONE.
    regards,
    DEP
    Are you sure? This action cannot be undone.
    Cancel
  • hajela_pushkar

    MemberAug 27, 2010

    😎 use a ; after else. The statement's of both the if and else will be executed.
    Are you sure? This action cannot be undone.
    Cancel
  • udgata

    MemberFeb 21, 2011

    Guys,

    This can be done using the below code in C ...

    if (!printf("True\n")😉
    else
    printf("False");


    Let me know, if you have questions...
    Are you sure? This action cannot be undone.
    Cancel
  • suhit

    MemberAug 9, 2011

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    if(printf("Hello ")==-1)
    {}
    else
    {
    printf("world");
    }
    getch();
    }


    Explanation:: As the library function printf()return the number of characters it prints and return 0 when nothing is printed, Hence in the if block whenever check occurs within the statement if(printf("Hello ")==-1),it becomes FALSE then after executing the printf("Hello ") part, the condition doesnt match as printf() does return the number of character, and it cant be negetive, so after print the "Hello " the control goes to the else block, Hence the printf("world"); part is executed, and the complete Hello World is printed on the screen, so both the blocks were executed.
    Note: in the if condition checking, u can check the by any negetive number instead of -1(eg. -2,-3,-4,-5,-6,...).
    Are you sure? This action cannot be undone.
    Cancel
  • DEBASMITA

    MemberAug 14, 2011

    if(condition)
    printf("i");
    else printf("am");
    pl can ne1 tel me wat shud be d "condition" so dat "i am" will be printed?I hav compiled all d codes given bt none of them are successful
    Are you sure? This action cannot be undone.
    Cancel
  • Pensu

    MemberAug 14, 2011

    @debasmita: i dont think you can print "i am" like this.....u might want to check solution provided by mahesh krishnan above.
    Are you sure? This action cannot be undone.
    Cancel
  • suhit

    MemberAug 14, 2011

    "
    DEBASMITA
    @Debosmita- if the control goes inside the if block, you will not be able to access the else block. If you really want to do this, you can use the go to statement that is already there in C. Like,

    main()
    {
    if(1)
    {
    printf("Hello");
    goto LINE_NUMBER;
    }
    else
    {
    printf("Hello");
    }
    }

    here LINE_NUMBER is the line number of the printf statement in else block...
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register