Monday, May 19, 2014

If-Else Control Flow Selection Statements

If Else:
*The arguments to the if statements should be Boolean type
Code:
1
package com.ifelse;

public class SelectionStatementsIfElse {
public static void main(String[] args){
int x=0;
if(x){
System.out.println("Hello...");
} else{
System.out.println("Hai....");
}

}
}

O/p:
………………………………………………………………………………..
2. package com.ifelse;

public class IfelseTest {
public static void  main(String[] args) {
int x=10;
if(x=20){
System.out.println("Hello..");
}
else{
System.out.println("Hai...");
}
}
}

o/p:
3.
package com.ifelse;

public class IfElseTest2 {

public static void main(String[] args) {

int x=10;
if(x==10){
System.out.println("hello..");
}else{
System.out.println("Hai..");
}

}

}

o/p:
……………………..
4.
package com.ifelse;

public class IfElseTest3 {
public static void main(String[] args){
boolean b=true;
if(b=false){
System.out.println("Hello..");
}else{
System.out.println("hai...");
}
}

}
....
5.
package com.ifelse;

public class IfElseTest3 {
public static void main(String[] args){
boolean b=true;
if(b=false){
System.out.println("Hello..");
}else{
System.out.println("hai...");
}
}

}

6.
package com.ifelse;

public class IfElseTest4 {
public static void main(String[] args){
boolean b=false;
if(b==true){
System.out.println("Hello..");
}else{
System.out.println("hai...");
}
}

}

O/P:
7.
package com.ifelse;

public class IfElseTest4 {
public static void main(String[] args){
boolean b=false;
if(b==false){
System.out.println("Hello..");
}else{
System.out.println("hai...");
}
}

}
O/P:
Hello..
*) Curly brases {} are optional and without Curlybrases we can take only one Statenent and which should not be declatative statement.
8.
package com.ifelse;

public class BrasesTest3 {
public static void main(String[] s){
if(true){
System.out.println("Hello..");
}
}

}
o/p:
Hello..
……………….
8.
package com.ifelse;

public class BrasesTest3 {
public static void main(String[] s){
if(true)
int x=10;

}
}
o/p:
9.
package com.ifelse;

public class BrasesTest3 {
public static void main(String[] s){
if(true);
}
}
O/P:
No ERROR
10.
package com.ifelse;

public class BrasesTest3 {
     public static void main(String[] s){
          if(true){
              int x=10;
          }
     }
}

o/p:

No Error

No comments :

Post a Comment