开发者

Condition that is true and false at the same time in C or Java [duplicate]

开发者 https://www.devze.com 2023-01-27 16:26 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: What's the “condition” in C interview question?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What's the “condition” in C interview question?

Can something like this be done?

Code:

if(condition) {
    printf("Hello")
} 开发者_运维百科else {
    printf("World");
}

Result: Hello World

What should be the condition to be used to make that possible ?

Please help. Thanks in advance.


No.

The else statement will only execute if the if didn't.


You can cheat to get this result in C:

#include <stdio.h>
#define else printf(" ");

int main()
{
    int condition = 1;
    if(condition) { 
        printf("Hello");
    } 
    else { 
        printf("World");
    }
}

Output:

Hello World


The only idea that comes into my mind is something like

if (printf("Hello") > 0)
  printf("");
else
  printf(" world");

but it's not the same thing, you can't execute both branches of an if/else: one of two is chosen.


The instructor is expecting you to fill in fork() as the condition. This is not general C code but Unix/POSIX code, and works by splitting the process into a parent process and child process. fork returns the child process id (a nonzero number, thus true) in the parent and 0 (false) in the child.

Another potential way to solve the problem, if you can add code elsewhere, is to write setjmp(jmp_buf) as the condition, and add a longjmp(jmp_buf, 1); after the conditional. However this seems to break the rules of the problem.


if/elses are either/ors. Either the if portion is executed or the else, but never both.


No, the statement is if ... else, not if ... and then also maybe. The condition is evaluated once, and the branch is chosen.


Its insane, but:

int test = 0;
String s = "";

switch (test) {
    case 0: s += "Hello";
    default: s += "World";
}

System.out.println(s);


Boolean type variables in both languages do not allow what you want to do. The whole point is that they must be one or the other.

To accomplish what you want, you probably need a custom type.

//This type allows for quantum weirdness
public class NotABool{

    public boolean isTrue = false;
    public boolean isFalse = false;

    //Funky setMethod
    void set(boolean value){
       //...
    }
}
NotABool nab = new NotABool();

if (NotABool.isTrue){
   //Print "Hello"
}
if (NotABool.isFalse){
   //Print "World"
}

Can you clarify what you're trying to accomplish?


The functionality you are describing looks more like the switch structures.

In Java, you can only switch on int and enum types, but in C, you can switch on string as well as int types.

It would look like so:

int i = 0;

switch (i) {
    case 0:
         System.out.print("Hello ");
    case 1:
         System.out.print("World!");
         break;
  }

The resulting output would be "Hello World!"

When i is 0, it matches the first case and executes the code until the next break; statement is found. If i was 1, it would only print out "World!".


In general, no, it's not possible. The way the if/else is translated to Java bytecode (and in either case, to machine code), one process will run exactly one of the two branches of the statement. If we didn't have if/else, we'd end up using goto to synthesize it, and that would look a lot like this:

  if (condition) goto if_block;

else_block:
  printf("World");
  goto after_ifelse;

if_block:
  printf("Hello");

after_ifelse:

As you can see, if the condition's true (even if it could somehow also be false!), the process will follow the 'if' branch and skip past the other. There's no way to get around this in a single process; any way would require changing the code of the program, or broken hardware (particularly RAM or CPU), or enough radiation to kill you. And every compiler and environment i know of treats if/else that way, though it's common to have the else case after the if (and invert the condition), which makes the (quite valid) assumption that any boolean condition that's not true is false.

Now with all that said...in C, it's semi possible, but not in the way you're thinking -- and not on every OS. On *nix systems, there's a system call usually called fork(), which allows one process to become two (thereby sidestepping the "one process will run exactly one branch" limitation).

if (fork())
    printf("Hello");
else
    printf("World");

But (1) this code has an inherent race condition -- both branches are now set to run, but either one could run before the other. You'd need to wait on the child process. And (2) this isn't a "condition", it's a function call. If you're not allowed to add code, then this should not be available as an answer.

Or you could do some evil macro stuff to translate the else into something else entirely. But anyone reading your code later would want to hunt you down and confiscate your keyboard, and that's if they're nice.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号