John Powel
John Powel

Reputation: 1424

How to execute the "if" and "else" at the same time?

Interesting problem:

Change if statement and print out "Hello World"

    public static void main(String[] args) {
        if(){
            System.out.println("Hello");
        }else{
            System.out.println("World");
        }
    }

My solution is to add "!System.out.println("Hello")" in the if statement,But it doesn't work, any ideas?

    public static void main(String[] args) {
        if(!System.out.println("Hello")){
            System.out.println("Hello");
        }else{
            System.out.println("World");
        }
    }

UPDATE: I think this works:

    public static void main(String args[]) {    
        if(System.out.append("Hello ")==null){
            System.out.print("Hello ");
        }else{
            System.out.println("World");
        } 
    }

In C:

main()
{   if(printf("Hello"),0)
         printf("Hello");
    else
       printf(" world!\n");
   getch();
}

Upvotes: 4

Views: 8785

Answers (12)

Niraj Singh
Niraj Singh

Reputation: 45

Consider the below code to execute both if-else block statements in C.

#include <stdio.h>
#include <stdbool.h>
int main()
{

  bool flag = false;
  if(flag)
  {
    printf("if block returns true-> now redirecting to else");
    goto el;
    i:
    printf(" redirected to if");
  }
  else
  {
    printf("if block returns false-> now redirecting to if");
    goto i;
    el:
    printf(" redirected to else");
  }
}

Just change the flag value in the above code. For example, if the flag value changed to true then first if block will execute and then else.

Upvotes: 1

Lei Sun
Lei Sun

Reputation: 1

public class Test {  

       public static void main(String args[]) {  

            if (args==null || new Test() {{Test.main(null);}}.equals(null)) {  
                     System.out.print("Hello ");
            } else {  
                     System.out.print("World!");  
            }  
       }  
}  

this can work, but a little bit complicate for understanding

Upvotes: 0

Harshad B
Harshad B

Reputation: 1

** Try this one **
class Test
{
    public static void main(String args[])
    {
        boolean one=true;
        for(int i=0;i<2;i++)
        {
        if(one)
        {
            System.out.print("Hello");
            one=false;  
        }   
        else

            System.out.println("World!");

        }       
    }
}`

Upvotes: 0

Biswajit Sahoo
Biswajit Sahoo

Reputation: 21

Check this one...

class Test {

    static boolean x = true;

    public static void main(String[] args) {
        ifAndElse();

    }

    public static void ifAndElse(){
        if (x) {
            System.out.println("hi");
            x = false;
            ifAndElse();
        } else {
            System.out.println("hello");
        }
    }
}

Upvotes: 0

user4074379
user4074379

Reputation: 11

This can also be achieved using "printf" with out specifying the format. the code goes as below:

if (System.out.printf("Hello") == null) {
    System.out.print("Hello");
} else {
    System.out.println("World");
}

Upvotes: 0

Jochen
Jochen

Reputation: 2295

My 2 cents: None of the solutions, including the orginigal C solution actually execute both the 'if' and the 'else'. All the solutions presented here execute and explicit printf("Hello") as part of the boolean expression in the condition. In all solutions, that condition is false and the else branch is then executed. But the actual if is not.

Upvotes: 4

Daniel Fischer
Daniel Fischer

Reputation: 183888

I can offer

if (System.out.printf("%s","Hello ") == null) {
    System.out.println("Hello");
} else {
    System.out.println("World");
}

Upvotes: 5

Tudor
Tudor

Reputation: 62439

Tadaaaa:

public static void main(String args[]) {    
    if(!new Object() {
        public boolean foo() {
            System.out.print("Hello ");
            return true;
        }
    }.foo()){
        System.out.println("Hello");
    }else{
        System.out.println("World");
    }
}

Upvotes: 22

Vlad
Vlad

Reputation: 18633

public class HelloWorld{
    public static void main(String[]args){
        if (new Object(){{ System.out.print("Hello "); }} == null){
            System.out.println("Hello");
        }else{
            System.out.println("World");
        }
    }
}

Upvotes: 2

Richard J. Ross III
Richard J. Ross III

Reputation: 55563

You could use append instead of println to determine if the write was successful:

if (System.out.append("Hello World" + System.getProperty("line.separator")) != null)
{
    // some code here
}

Upvotes: 0

Atreys
Atreys

Reputation: 3761

Switch on the contents of args and run the program twice with two different parameters.

Your program will either print out "Hello" or "World" unless you modify the input to println in addition to fixing your if() construct.

Upvotes: 1

maksimov
maksimov

Reputation: 5811

It doesn't work because in Java if expects an expression of type boolean. System.out.println has no return type, it is void. That's why it doesn't work.

Upvotes: 4

Related Questions