"the basis of all logical thinking is sequential thought. This process involves taking the important ideas, facts, and conclusions involved in a problem and arranging them in a chain-like progression that takes on a meaning in and of itself. To think logically is to think in steps".- Albrecht
In programming, basic logical and decision making skills can be demonstrated by showing skills in using control flow structures in a program.
The if-then and if-then-else Statements, is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.
/*
* Generates letter grade from numeric grade given
* pre: grade > 0
* post: letterGrade generated
*/
public static String getLetterGrade(double grade){
String letterGrade = "";
if(grade >= 85 ){
letterGrade = "A*";
}else if(grade >= 80){
letterGrade = "A";
}else if(grade >= 75){
letterGrade = "B";
}else if(grade >= 70){
letterGrade = "C";
}else if(grade >= 60){
letterGrade = "D";
}else{
letterGrade = "F";
}
return letterGrade;
}
How Compound Boolean expression evaluates with && and || operators can be shown with truth tables. A truth table shows the possible outcomes of compound Boolean expressions.
double price = 0;
double cost = 0;
System.out.print("Enter number of eggs purchased: ");
int eggs = input.nextInt();
int numOfDozens = eggs / 12;
int extra = eggs % 12;
if (numOfDozens < 4) {
price = 0.50;
} else if (numOfDozens >= 4 && numOfDozens < 6) {
price = 0.45;
} else if (numOfDozens >= 6 && numOfDozens < 12) {
price = 0.40;
} else {
price = 0.35;
}
cost = ((price * numOfDozens) + (extra * (price / 12)));
System.out.println("The bill is equal to: " + cost);