Boolean Expressions and If Statement Lesson
import java.util.Scanner;
public class Booleans //define class
{
public static void main(String[] args) //java main method
{
boolean isSunny = true; //declare the boolean variables
boolean isRainy = false;
if (isSunny)
{
System.out.println("It is sunny.");
}
else
{
System.out.println("It is rainy.");
}
}
}
Booleans.main(null);
- If statements can be simple or complex in nature. Here is an example of a simple if statement
import java.util.Scanner;
public class Test //define class
{
public static void main(String[] args) //java main method
{
int num = 10; //declare the integer variable
if (num > 2) //if statement format with parameters
{
System.out.println("The number is greater than 2"); //if the number is greater than zero, print this statement.
}
}
}
Test.main(null);
- If statements often use else statements to specify what the code should do if the first condition is not met.
import java.util.Scanner;
public class Else
{
public static void main(String[] args)
{
int num = 1;
if (num > 2)
{
System.out.println("The number is greater than 2");
}
else //the computer will run this portion if the number is less than or equal to 2
{
System.out.println("The number is not greater than 2"); //This is what the computer will print
}
}
}
Else.main(null);
import java.util.Scanner;
public class switchExample
{
public static void main(String[] args)
{
int num = 1;
switch(num)
{
case 1:
if (num > 2)
{
System.out.println("The number is greater than 2");
}
else //the computer will run this portion if the number is less than or equal to 2
{
System.out.println("The number is not greater than 2"); //This is what the computer will print
}
case 2:
if (num > 3)
{
System.out.println("The number is greater than 3");
}
else //the computer will run this portion if the number is less than or equal to 2
{
System.out.println("The number is not greater than 3"); //This is what the computer will print
}
case 3:
if (num > 7)
{
System.out.println("The number is greater than 7");
}
else //the computer will run this portion if the number is less than or equal to 2
{
System.out.println("The number is not greater than 7"); //This is what the computer will print
}
case 4:
if (num < 3)
{
System.out.println("The number is less than 3");
}
else //the computer will run this portion if the number is less than or equal to 2
{
System.out.println("The number is greater than 3"); //This is what the computer will print
}
case 5:
if (num > 4)
{
System.out.println("The number is not greater than 4");
}
else //the computer will run this portion if the number is less than or equal to 2
{
System.out.println("The number is greater than 4"); //This is what the computer will print
}
case 6:
System.out.println("Thanks for running this code.");
}
}
}
switchExample.main(null);
import java.util.Scanner;
public class deMorgan //define class
{
public static void main(String[] args) //java main method
{
int a = 0; //initializing variables
int b = 0;
System.out.println(!(a==10 || b==10)); //Will print true if a or b does not equal 10
System.out.println(a!=10 && b!=10);
}
}
deMorgan.main(null);
- De Morgan's law can be used in other code. For example between different foods.
import java.util.Scanner;
public class deMorganExample //define class
{
public static void main(String[] args) //java main method
{
String food = "pasta";
String food2 = "pizza";
if (!(food.equals("pizza") || food.equals("bread"))) // Makes !(false value == true value)
{
System.out.println("The food is not pasta or pizza."); //Will print this
}
else // if not
{
System.out.print("Food type is pasta or pizza."); //Will print this
}
}
}
deMorganExample.main(null);