Jupyter Notebook lesson on ifs
- Build your own Jupyter Notebook lesson on ifs
- Add to lesson switch-case
- Finish lesson with De Morgan's law
// if, if-else, and if-elseif-else are all conditional statement and return a boolean value(true or false)
//else statement does not include condition.
//Example of only one if statement
int a = 13; //set a value
int b = 14; //set a value
public ifstatement(){
if (a < b){ //if the condition is true then execute the code inside the curly braces
System.out.println("highest value:" + b); // code that will be executed
}
}
//Example of if-else statement
public ifelsestatement(){
if (a < b){ //if the condition is true then execute the code inside the curly braces
System.out.println("highest value:" + b); // code that will be executed
}
else if (a > b){ //if the first if statement is not true and the condition of the else-if statement is true, then execute the code inside the curly braces
System.out.println("highest value:" + a); // code that will be executed
}
}
//Example of if-elseif-else statement
public ifelseifelse(){
if (a < b){ //if the condition is true then execute the code inside the curly braces
System.out.println("highest value:" + b); // code that will be executed
}
else if (a > b){ //if the first if statement is not true and the condition of the else-if statement is true, then execute the code inside the curly braces
System.out.println("highest value:" + a); // code that will be executed
}
else { //if all if-statement above this else-statement is not true, then execute the code inside the curly braces
System.out.println("highest value: none"); // code that will be executed
}
}
Add to lesson switch-case
- Create and if-elseif-elseif-elsif-else statement, 5 or more conditions.
- Covert the 5 or more decisions to a switch-case-case-case-case-otherwise.
- Make a markdown block before each code example
- Comment/establish a style of comments for your if-elseif and switch-case code blocks
int a = random(1,500);
int b = random(1,1000);
int c = 500;
if(c < a = b){ //if a is equal to b and greater than c, execute the code below
System.out.println("two highest value a and b:" + a); // code that will be executed
}
else if(c > a = b){ //if the condition of statement above is not true, then check for this condition.
System.out.println("highest value c:" + c); // code that will be executed
}
else if(b < a < c){//if the condition of statement above is not true, then check for this condition.
System.out.println("highest value c:" + c); // code that will be executed
}
else if(c < a < b){//if the condition of statement above is not true, then check for this condition.
System.out.println("highest value b:" + b); // code that will be executed
}
else if(a = c > b){//if the condition of statement above is not true, then check for this condition.
System.out.println("two highest value a and c:" + b); // code that will be executed
}
else if(a = c < b){//if the condition of statement above is not true, then check for this condition.
System.out.println("highest value:" + b); // code that will be executed
}
else{//if all the conditions of statements above are not true, then execute the code below.
System.out.println("Can you tell me the answer?"); // code that will be executed
}
int a = 50;
int b = 50;
if (!((a < b) || (a > b))){
System.out.println("a and b is equal"); // code that will be executed
}