//condition inside if statment is true so system outprints "Hi"
if (true) {
System.out.println("Hi");
}
//true and not false so system prints out "Hello"
if (true && !false) {
System.out.println("Hello");
}
If/Else Statement
The If/Else statement extends the If statement by specifying an action if the If (true/false expression) is false. With the If statement, a program will execute the true code block or do nothing. With the If/Else statement, the program will execute either the true code block or the false code block so something is always executed with an If/Else statement.
int x = 10;
//if statement is false so the else statment runs
if (x<5) {
System.out.println("Hello");
}
else {
System.out.println("Greater than 5");
}
If/ElseIf Statement
The If/ElseIf statement allows you to create a chain of If statements. You are able to test more than two conditions unlike the If/Else statment. The If statements are evaluated in order until one of the If expressions is true or the end of the If/ElseIf chain is reached. If the end of the If/ElseIf chain is reached without a true expression, no code blocks are executed.
int x = 9;
//false conditions until last else if statement so that one runs
if (x<2){
System.out.println("Less than 2");
}
else if (x<4){
System.out.println("Less than 4");
}
else if (x<6){
System.out.println("Less than 6");
}
else if (x<8){
System.out.println("Less than 8");
}
else if (x<10){
System.out.println("Less than 10");
}
char x = 'd';
//none of the if or elseif statments are true so it goes through the chain and runs the else statment
if (x=='a'){
System.out.println("x is a");
}
else if (x=='b'){
System.out.println("x is b");
}
else if (x=='c'){
System.out.println("x is c");
}
else {
System.out.println("not a, b, or c");
}
If/ElseIf/Else statments can be used to decide different program outputs depending on what the user inputted. For example, they would be helpful in a menu program.
Scanner inputStream = new Scanner(System.in);
String userInputStr;
int choice;
System.out.println("choose an integer 1-5: ");
userInputStr = inputStream.nextLine();
choice = Integer.parseInt(userInputStr); //get user input for choice
System.out.println(choice);
//false conditions until choice equals 4 and only that else if statement runs
if (choice==1){
System.out.println("\nyou chose one");
}
else if (choice==2){
System.out.println("\nyou chose two");
}
else if (choice==3){
System.out.println("\nyou chose three");
}
else if (choice==4){
System.out.println("\nyou chose four");
}
else if (choice==5){
System.out.println("\nyou chose five");
}
else {
System.out.println("invalid input");
}
Switch Statement
A switch statement is usually more efficient than a set of nested ifs. The switch statement is a control flow statement. It evaluates a user selection and different parts of code can be executed quickly. Below, a switch statement will preform the same function as the chain of nested ifs above.
Scanner inputStream = new Scanner(System.in);
String userInputStr;
int choice;
System.out.println("choose an integer 1-5: ");
userInputStr = inputStream.nextLine();
choice = Integer.parseInt(userInputStr);
System.out.println(choice);
switch (choice) { //based on what the user inputted for the choice variable, different cases will run
case 1:
System.out.println("\nyou chose one");
break;
case 2:
System.out.println("\nyou chose two");
break;
case 3:
System.out.println("\nyou chose three");
break; //default prevents it from moving on to and running the following cases
case 4:
System.out.println("\nyou chose four");
break;
case 5:
System.out.println("\nyou chose five");
break;
default:
System.out.println("invalid input");
}
Logic Gates
Logic gates are used to carry out logical operations on single or multiple binary inputs and give one binary output. In simple terms, logic gates are the electronic circuits in a digital system.
- Buffer/YES : output is the same as the input
- NOT : output is the opposite of the input
- AND : returns true if both inputs are true
- NAND : returns false if both inputs are true
- OR : returns true if at least one of the inputs are true, returns false if both inputs are false
- NOR : returns false if at least one of the inputs are true, returns true if both inputs are false
- XOR : returns true if the two inputs are different
- XNOR : returns true if two inputs are the same
These tables demonstrate the input and output combinations of different logic gates
- Tips to help remember the symbols:
- each main shape is unique to a logic gate (ex: triangle = buffer/YES)
- a small circle represents the addition of "NOT" to the logic gate
- a small curved line represents the addition of "exclusive" or "X" to the logic gate
Syntax
- union symbol (∪) : OR
- intersection symbol (∩) : AND
- apostrophe (') : NOT
boolean a = true;
boolean b = false;
//Not (A and B)
boolean result1 = !(a && b);
//Not A or Not B
boolean result2 = ((!a) || (!b));
if (result1 == result2){
System.out.println("result 1 and 2 are the same");
}
else{ //result 1 and 2 will always be equal, the code should never reach here
System.out.println("Error");
}
//Not (A or B)
boolean result3 = !(a || b);
//Not A and Not B
boolean result4 = ((!a) && (!b));
if (result3 == result4){
System.out.println("result 3 and 4 are the same");
}
else{ //result 3 and 4 will always be equal, the code should never reach here
System.out.println("Error");
}
Truth Tables
A truth table is a breakdown of a logic function by listing all possible values the function can attain. Such a table typically contains several rows and columns, with the top row representing the logical variables and combinations, in increasing complexity leading up to the final function.
Resources
- https://www.computerhope.com/jargon/i/ifstatme.htm
- https://eecs.oregonstate.edu/ecampus-video/CS161/template/chapter_4/ifelse.html
- https://www.onlinemathlearning.com/demorgans-law.html
- https://blog.penjee.com/what-is-demorgans-law-in-programming-answered-with-pics/
- https://www.cs.utah.edu/~germain/PPS/Topics/truth_tables_and_logic.html