CrazyEngineers
  • doubt in java

    neha1

    Member

    Updated: Oct 25, 2024
    Views: 1.1K
    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......
    0
    Replies
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
  • silverscorpion

    MemberMay 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..
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMay 14, 2010

    It is not possible
    at least for me don't know about james 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • optimystix

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • HImanshu_Sharma

    MemberMay 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
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 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...
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberMay 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....
    Are you sure? This action cannot be undone.
    Cancel
  • AGRU

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 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;
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register