wellnwill Marathahalli Bangalore

Selection/Conditional Statements

class Test97
{
 public static void main(String args[])
 {
   if(99==99)
   int x=10;
   }
}

// O/P-CompileTimeException
       -variable declaration not allowed here.
       
       
class Test98
{
  public static void main(String args[])
  {
    if(99==99)
    {
     int x=10;
     System.out.println(x);
    }  
  }
}

// O/P - 10
class Test100
{
  public static void main(String args[]){
  if(99>100);
  {
    System.out.println("hi");
  }
 }
}

//O/P - hi
class Test100
{
  public static void main(String args[]){
    if(99>100) 
   {
    System.out.println("hi");
   }
  }
}

// O/P - Compile Time Success

-> Semicolon is valid java statement, also known as empty statement.

class Test100
{
   public static void main(String args[])
   {
     if(99>100);
     {
        System.out.println("hi");
     }
   }
}




//O/P - hi
class Test99
{
 public static void main(String args[])
   {
    if(99==99);
      {                //local block
        int x=10;
        System.out.println(x);
       }
    }
}

// O/P-10, here we are using semi colon that is why block is not
           controlled by if and jvm is treating this block as local block.

-> Dangling else problem is not present in java. Every is mapped to the nearest .

class Test101
{
 public static void main(String args[])
 {
    int x=10;
    if(x==11)
    {
      System.out.println("if block");
    }
    else
    {
     System.out.println("else block");
    }
 }
}

// O/P - else block



class Test102
{
 public static void main(String args[])
 {
    int x=10;
    if(x==11)
    {
      System.out.println("if block1");
    }
   if(x==12)
     {
         System.out.println("if block2");
     }
    else
    {
     System.out.println("else block");
    }
 }
}
// o/p - else block
class Test103
{
   public static void main(String args[])
   {
      int num1=10, num2=20;
      if(num1 == num2) //checks if both are equal.
      {
         System.out.println("both are equal");
      }
      else if (num1 > num2) //checks if num1 is greater
      {
        System.out.println("num1 is greater");
      }
      else  // if both test expression is false
      { 
	   System.out.println("num2 is greater"); 
	  }
  }
}

// O/P - num2 is greater
Footer with Map

Top