Build your own Jupyter Notebook lesson on ifs

  • Explain if, if-else, and if-elseif-else.
  • Make a markdown block before you sample code
  • Comment in code to describe each decision
// 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
        }
    }
highest value:14

Add to lesson switch-case

  1. Create and if-elseif-elseif-elsif-else statement, 5 or more conditions.
  2. Covert the 5 or more decisions to a switch-case-case-case-case-otherwise.
  3. Make a markdown block before each code example
  4. 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 = random(ram1,ram3);
cannot find symbol
  symbol:   method random(int,int)

Finish lesson with De Morgan's law

  1. Describe De Morgan's law
  • ex. !(A || B) = (not A) and (not B) ; !(A && B) = (not A) or (not B)
  1. Illustrate De Morgan's law
  • Not (A and B) is the same as Not A or Not B.
  • Not (A or B) is the same as Not A and Not B.
  1. Show some code running that shows understanding
int a = 50;
int b = 50;

if (!((a < b) || (a > b))){
    System.out.println("a and b is equal"); // code that will be executed
}
a and b is equal