Rohan G CollegeBoard Unit 3 Boolean Expressions and If Statements
Week 9 Unit 3 Lesson
- If/else statements allow you to create a code segment that has checks. If/ElseIf/Else statments can be used to decide different program outputs depending on what the user inputted. Switch statements are also an alternative method to if statements.
- Conditionals Exercises 1-20 Even
- Notes (courtesy of Bailey S)
// Exercise 2
int a = 1;
int b = 5;
int c = 1;
double d = Math.pow(b,2) - 4 * a * c;
if (d > 0) {
double root1 = (-b + Math.sqrt(d)) / (2 * a);
double root2 = (-b - Math.sqrt(d)) / (2 * a);
System.out.println(root1);
System.out.println(root2);
} else {
System.out.println("not real");
}
// Exercise 4
float blah = 25;
if (blah == 0) {
System.out.println("zero");
}
else if (blah > 0) {
System.out.println("positive");
} else {
System.out.println("negative");
}
if (Math.abs(blah) < 1 ) {
System.out.println("small");
}
if (Math.abs(blah) > 1000000 ) {
System.out.println("big");
}
// Exercise 6
import java.util.Scanner;
Scanner in = new Scanner(System.in);
System.out.print("Input floating-point number: ");
double x = in.nextDouble();
System.out.print("Input floating-point another number: ");
double y = in.nextDouble();
x = Math.round(x * 1000);
x = x / 1000;
y = Math.round(y * 1000);
y = y / 1000;
if (x == y)
{
System.out.println("They are the same up to three decimal places");
}
else
{
System.out.println("They are different");
}
// 8
import java.util.Scanner;
Scanner in = new Scanner(System.in);
System.out.print("Input an alphabet: ");
String input = in.next().toLowerCase();
boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <= 90;
boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <= 122;
boolean vowels = input.equals("a") || input.equals("e") || input.equals("i")
|| input.equals("o") || input.equals("u");
if (input.length() > 1)
{
System.out.println("Error. Not a single character.");
}
else if (!(uppercase || lowercase))
{
System.out.println("Error. Not a letter. Enter uppercase or lowercase letter.");
}
else if (vowels)
{
System.out.println("Input letter is Vowel");
}
else
{
System.out.println("Input letter is Consonant");
}
// 10
int i;
System.out.println ("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
System.out.println (i);
}
System.out.println ("\n");
// 12
import java.util.Scanner;
int i,n=0,s=0;
double avg;
{
System.out.println("Input the 5 numbers : ");
}
for (i=0;i<5;i++)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
s +=n;
}
avg=s/5;
System.out.println("The sum of 5 no is : " +s+"\nThe Average is : " +avg);
// 14
import java.util.Scanner;
int j,n;
System.out.print("Input the number(Table to be calculated): ");
{
System.out.print("Input number of terms : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println ("\n");
for(j=0;j<=n;j++)
System.out.println(n+" X "+j+" = " +n*j);
}
// 16
import java.util.Scanner;
int i,j,n;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println("");
}
// 18
import java.util.Scanner;
int i,j,n,k=1;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(k++);
System.out.println("");
}
// 20
import java.util.Scanner;
int numberOfRows;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
numberOfRows = in.nextInt();
int number = 1;
for (int row = 1; row <= numberOfRows; row++)
{
for (int column = 1; column <= row; column++)
{
System.out.print(number + " ");
number++;
}
System.out.println();
}
Notes (courtesy of Bailey S)
Truth Tables
Truth tables can be used to visualize boolean conditionals. They're useful for evaluating basic boolean expressions as well as more complicated compound boolean expressions, which use multiple kinds of boolean operators inside each other.
De Morgan's Law
De Morgan's Law is a bit more mathematical in nature, dealing with set theory, so I'll try my best to describe it here.
De Morgan's Law basically explains how certain boolean statements can be rewritten as different expressions, yet essentially be the same. More specifically, it states that "The complement of the union of two sets is the same as the intersection of their complements" or "The complement of the intersection of two sets is the same as the union of their complements".
What the heck does that mean? An easier way to think about it would be like this:
- not (A or B) = (not A) and (not B)
- not (A and B) = (not A) or (not B)
In a more formalalized, mathematical way:
But even with all that text, it's still pretty confusing, right? So it's probably best to visualize it using an actual image. This one I found online pretty much summarizes it best:
The green shaded area is basically what's being covered in each equality. Both sides cover the same thing.
Finally, let's go through an example of how this would apply in code. Essentially, we'll use a few variables to make a whole bunch of complicated compounded boolean expressions that actually mean the same exact thing: