Tricky Program in Java

Tricky Program in Java

Publish Date: Sep 6
0 0
class Try{

public static void main(String[] arg){
 int a=5;
 if(a==5)
   System.out.println("b");

 else
   System.out.println("c");


  }

}
Enter fullscreen mode Exit fullscreen mode
class Try{

public static void main(String[] arg){
 int a=5;
 if(a==5)
   System.out.println("b");

 else
   System.out.println("c");


  }

}
Enter fullscreen mode Exit fullscreen mode

What do you think the difference is between the above programs?

->In the first program, the if and else are not enclosed

with curly braces {}
->In the second program, the if and else are enclosed with
curly braces{}

What is the output?
->For first "b" is the output
->For the second, "b" is the output

I thought for a second that a program error would occur because curly braces are not there, but the output is
"b"

In the second case


class Try{

public static void main(String[] arg){
 int a=5;
 if(a==5)
   System.out.println("b");
   System.out.println("a");

 else
   System.out.println("c");


  }

}
Enter fullscreen mode Exit fullscreen mode

When we are using one statement in the program, it is running
Well, without any error, but in the second
output

Comments 0 total

    Add comment