Rohan G If Statements
Week 3 If Statements
- If Statement
- Else If Statement
- If Elseif Else Statement
- If Statements CheckAnswer Function
- Switch Case
- De Morgan's Law
if (condition) {
//output
}
else {
//ouput
}
if (condition) {
//output
}
else if (other condition) {
//ouput
}
if (condition) {
//output
}
else if (other condition) {
//ouput
}
else {
//output
}
class CheckAnswer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("What is the country of the capital?");
String answer = scanner.nextLine();
if (answer.equals ("Santiago") ) {
System.out.println("That is the capital of Chile!");
}
else if (answer.equals ("Bogota") ) {
System.out.println("That is the capital of Columbia!");
}
else if (answer.equals ("Lima") ) {
System.out.println("That is the capital of Peru!");
}
else if (answer.equals ("Buenos Aires") ) {
System.out.println("That is the capital of Argentina!");
}
else if (answer.equals ("Quito") ) {
System.out.println("That is the capital of Ecuador!");
}
else if (answer.equals ("Caracas") ) {
System.out.println("That is the capital of Venezuela!");
}
else {
System.out.println("This is not a valid capital!");
}
}
}
CheckAnswer.main(null);
public class SwitchCase {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a single digit value"); //user input for array
int x = scanner.nextInt();
switch (x){
case 0:
//If case is 0, this block executes
System.out.println("Absolute Value was 0");
break;
case -1:
//If case is 1, this block executes
System.out.println("Absolute Value was 1");
break;
case -2:
//If case is 2, this block executes
System.out.println("Absolute Value was 2");
break;
case -3:
//If case is 3, this block executes
System.out.println("Absolute Value was 3");
break;
case -4:
//If case is 4, this block executes
System.out.println("Absolute Value was 4");
break;
case -5:
//If case is 5, this block executes
System.out.println("Absolute Value was 5");
break;
case -6:
//If case is 5, this block executes
System.out.println("Absolute Value was 5");
break;
case -7:
//If case is 5, this block executes
System.out.println("Absolute Value was 8");
break;
case -8:
//If case is 5, this block executes
System.out.println("Absolute Value was 8");
break;
case -9:
//If case is 5, this block executes
System.out.println("Absolute Value was 9");
break;
default:
//This block is similar to else statement, is the default operation
System.out.println(x);
}
}
}
SwitchCase.main(null);
!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
if ((true) && !(false)) {
System.out.println("valid");
}
if (!(false) || (true)) {
System.out.println("opposite demonstrating de morgans law");
}