Monday, September 6, 2010

Execute both if and else statements

There are two ways to execute both if and else statements.

1.

if(1)
printf("if loop");
else;
printf(" else loop");

output:

if loop else loop

In the above statement note that there is a semi-colon (;) in the else statement. This statement is called as NULL statement in 'C'.

The above program becomes:


if(1)
printf("if loop");
else
 ;
printf(" else loop");


2.

if(printf("if")==0)
printf(" if");
else
printf(" else");

output:

 if else

In the statement-2, the printf which is inside the if condition is executed. Generally if executes the statement which is given as condition, so executes the print statement and then checks it is equal to 0, here condition becomes false. When condition comes false else is executed.

In this above step-2 the print statement which is given inside the if loop is executed. So the question is:

if(condition)
printf("Hello");
else
printf(" world");

write the condition such that your output will be:

Hello world

11 comments:

  1. sorry friend there is no way to execute if else both simultaneously.there are some trick to make full u by giving same output.

    ReplyDelete
  2. if(printf("if")==0)
    ##printf(" if");
    else
    printf(" else");


    In this case out put will be if else but ##printf want execute the printf which inside if will execute.

    ReplyDelete
  3. Why not this one:

    if ( fork() ) {
    print "hello";
    }
    else {
    print "world";
    }

    ReplyDelete
  4. #include
    int main(){

    if(fork())
    {
    printf("hello");
    }
    else
    {
    printf("world");
    }

    }

    ReplyDelete
    Replies
    1. Its nt work...it show that frok has some prototype...wil u knw plz ensure it ..

      Delete
  5. //Both Execute IF else in java
    public class Test12 {
    public static int i=1;

    public static boolean foo() {
    boolean bl;
    if(i==1)
    {
    bl= true;
    }
    else
    {
    bl= false;
    }
    return bl;

    }
    public static void main(String[] args) {
    boolean bl;
    for (int i = 1; i <=2; i++) {
    bl=foo();

    if(bl)
    {
    System.out.print("ChimanBhai");
    Test12.i=2;
    foo();

    continue;

    }
    else
    {
    System.out.println(" Patel");
    }
    }


    }
    }

    ReplyDelete
  6. void main()
    {
    int a=1;
    if(a==1)
    {

    printf("hello");
    if(a==1)
    goto b;
    }
    else
    {
    b:printf("world");
    }
    }

    ReplyDelete
  7. if(1
    #define else if(1)
    )
    printf("if");
    else
    printf(" else");

    ReplyDelete
  8. int main()
    {
    if(printf("i")<0)
    printf("i ");
    else
    printf(" love");
    return 0;
    }

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. If(!printf("hello")
    {
    Printf("hello");
    }
    else
    {
    Printf("world");
    }

    I think it get execute

    ReplyDelete