doubt in java

neha1

neha1

@neha1-XOkqHl Oct 25, 2024
hiiiiiiii
can any one tell me what condition we should give in if statement in order to execute both if and else statements??
for ex:
if( ???)
System.out.println("hello");
else
System.out.println("world");

it should print helloworld,,please fill ????

problem m facing is in ?? i provide int it doesnot accept...and ! condiiton is giving error if i use System.out.println() function,s return value...
pls solve it......

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ May 14, 2010

    Well, I don't get your question.
    The point of using if is to do one job or the other based on some condition.
    If you want to do both the jobs, then you dont need to use if itself.

    Perhaps if you provide some sample code and say where you face problems, we can be able to help..
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep May 14, 2010

    It is not possible
    at least for me don't know about james 😀
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M May 14, 2010

    ya goyal is right.
    but if it is a tricky question(we have to solve it by anyway).
    then i think that this is the method by which we have to do this.
    import java.io.*;
    class m
    {
    public static void main(String arg[])
    {
    boolean x;
    if(x=true)
    {
    System.out.println("mohit ");
    x=false;
    continue;
    }
    else
    {
    System.out.println("cboy hack the world");
    }
    }
    }
    but here what keyword is placed in the place of continue so that it will execute the if statement again.
  • optimystix

    optimystix

    @optimystix-4yMPKC May 14, 2010

    silverscorpion
    Well, I don't get your question.
    The point of using if is to do one job or the other based on some condition.
    If you want to do both the jobs, then you dont need to use if itself.

    Perhaps if you provide some sample code and say where you face problems, we can be able to help..
    exactly!
    there's no point using an IF ELSE statement if both have to be typed. Even if it is a trick question, it is a foolish one 'cos one will never need an IF ELSE statement if both the things have to be worked out.
  • HImanshu_Sharma

    HImanshu_Sharma

    @himanshu-sharma-HOM7Hg May 15, 2010

    neha1
    hiiiiiiii
    can any one tell me what condition we should give in if statement in order to execute both if and else statements??
    for ex:
    if( ???)
    System.out.println("hello");
    else
    System.out.println("world");

    it should print helloworld,,please fill ????

    problem m facing is in ?? i provide int it doesnot accept...and ! condiiton is giving error if i use System.out.println() function,s return value...
    pls solve it......
    public class demoif {

    public static void main(String[] args) throws Exception {
    if ( System.out.printf("Hello") == null ) {

    }
    else {
    System.out.println("World");
    }
    }
    }
    it is the perfect ans u can try
  • bharathkumarp

    bharathkumarp

    @bharathkumarp-kwU5lr May 16, 2010

    Hey neha,if you want get the output as hello world, then try without else,then you definitely going to get the out put..But you can't get the both if and else part...
    other wise if you want to get those output fallow this code(using labels)...
    int a=1;
    l1:if(a==1)
    {
    System.out.println("hello");
    a=0;
    }
    else
    System.out.println("world");
    goto l1;

    this is work in c and c++,, But i'm not sure in java.. Just try...
  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ May 16, 2010

    bharathkumarp
    Hey neha,if you want get the output as hello world, then try without else,then you definitely going to get the out put..But you can't get the both if and else part...
    other wise if you want to get those output fallow this code(using labels)...
    int a=1;
    l1:if(a==1)
    {
    System.out.println("hello");
    a=0;
    }
    else
    System.out.println("world");
    goto l1;

    this is work in c and c++,, But i'm not sure in java.. Just try...
    Hey, this is an infinite loop man.. It'll go on printing

    helloworldworldworldworld....
  • AGRU

    AGRU

    @agru-00oqkR May 21, 2010

    yup , silverscorpion is right it goes fr infinite loop.

    i believe that we use " if else "only in case we want to solve cases of either or still if u solely aim only to print " hello world "
    then try like

    import java.io.*;
    class trial
    {
    public static void main(String args[])
    {
    boolean x;
    int c=0;
    while (c<2)
    {
    if(x=true)
    {
    System.out.println("hello" );
    x=false;
    }
    else
    System.out.println("world" );
    c=c + 1;
    }
    }
    but its like getting something purposefully.
    help me if i m wrong.
  • bharathkumarp

    bharathkumarp

    @bharathkumarp-kwU5lr May 22, 2010

    hi #-Link-Snipped-#, ya i am wrong.. But i can modify that code as fallow, If there is mistake please inform me and let me know how can write....

    int a=1,b=0;
    l1:if(a==1)
    {
    System.out.println("hello");
    a=0;
    }
    else
    System.out.println("world");
    if(b==0)
    {
    b=1;
    goto l1;
    }