controlStatement
Examples of if statements
Size 1.7 kB - File type text/x-javaFile contents
import java.util.Scanner;
public class ControlFlow
{
public static void main(String args[]){
Scanner input = new Scanner( System.in );
double i;
System.out.print("Enter an integer: ");
i = input.nextDouble();
//----------------------IF WITHOUT ELSE----------------------------------------
if (i > 0){
System.out.println("i is greater than 0");
}
if (i < 0){
System.out.println("i is smaller than 0");
}
if (i == 0){
System.out.println("i is equal to 0");
}
//------------------------------------------------------------------------------
if (i == 0){
System.out.println("i as a zero is boring");
} else {
System.out.println("i as a non zero is interesting");
}
//----------------------MULTIBRANCH IF WITH ELSE--------------------------------
if (i > 0){
i = i+10;
System.out.println("i changed to: " +i);
}else if (i < 0){
i++;
System.out.println("i is " +i);
}else if (i == 0){
i = 90;
System.out.println("i can't be zero. So I change to " +i);
}
//---------------------NESTED IF------------------------------------------------
if(i < 10){
System.out.print("Cold");
} else{
if (i < 50){
System.out.print ("Normal");
}
else
System.out.print("Hot");
}
}
}
Click here to get the file