Programming 2 Decision-Making
Planning Decision-Making Logic Pseudocode & flowcharts are very important when it comes to the planning of decision-making logic Don’t just sit down and start coding! Even if it seems simple, begin the practice of designing before coding
The ‘if’ Structure { System.out.println(“Perfect”)’ } Int quizScore =9; //if the following statement is TRUE, do the //stuff in curly braces If (quizScore ==10) { System.out.println(“Perfect”)’ }
Comparison Operators Equal to (==) Not equal to (!=) Less than (<) Greater than (>) Less than or equal to (<=) Greater than or equal to (>=) The ‘answer’ is always going to be true or false
Equal To / Not Equal To If (quizScore ==10) { //do stuff } TRUE if quizScore is equal to 10 FALSE if quizScore is anything but 10 If (quizScore !=10) TRUE if quizScore is anything but 10 FALSE if quizScore is equal to 10
Greater Than / Less Than If (quizScore >10) { //do stuff } TRUE if quizScore is 11 or above FALSE if quizScore is 10 or less If (quizScore <10) TRUE if quizScore is 9 or less FALSE if quizScore is 10 or greater
No semi—colon at end of ‘if’ Int quizScore = 9; If (quizScore == 10); { system.out.println(“Perfect”); }
Greater Than or Equal to Less Than or Equal to If (quizScore >= 10) { //do stuff } TRUE if quizScore is 10 or above FALSE if quizScore is 9 or less If (quizScore <= 10) TRUE if quizScore is 10 or less FALSE if quizScore is 11 or greater
Make sure Not to Use Assignment Operator Instead of Equivalency Int quizScore = 9; If (quizScore ==10) //correct! { System.out.println (“Perfect”); } ********************************** If (quizScore = 10) //Not Correct!
The ‘if/else’ Structure Int quizScore = 9; //if the statement below is TRUE, do the stuff in curlys If (quizScore ==10) //correct! { System.out.println (“Perfect”); } //if the ‘if’ above is FALSE, do THIS stuff in curlys else System.out.println (“ Not Perfect”);
Multiple statements in ‘if/else’
Variable Scope public Class CompareNumbers { Any variable declared inside a block is local to that block The System.out.println below will not work public Class CompareNumbers { publicstatic void main (String [] args) int a =1; int b = 2; if (a==b) sum = a + b; } System.out.println(sum); A Block is lines of code between curly braces; To correct the problem, declare the variable sum outside of the block See next slide
public Class CompareNumbers { publicstatic void main (String [] args) int a =1; int b = 2; int sum; if (a==b) sum = a + b; } System.out.println(sum); Sum will print in this example because the variable was declared outside of the block
Constants Constant variables are variables whose values CANNOT change public class CalcInterest { public static void main (String[] args) final double RATE = .008; double savingsTotal = 2000.98 double interestEarned = RATE * savingsTotal; System.out.println(interestEarned); RATE = .007; // can’t do this; cannot reassign }
Nested If/Else Statements
Logical AND & OR operators OR (||) – piping symbol Shift of \ Public class CalcSales { public static void main (String[] args) Double salesAmount = 2500 Int bonus; If( (salesAmount > 1000) && (salesAmount < 5000) ) bonus = 400; }
OR Operators Public class CalcSales { public static void main (String[] args) Double salesAmount = 2500 Int hoursWorked = 3200 Int bonus; If( (salesAmount > 5000) || (hoursWorked > 3000) ) bonus = 400; }
Combining AND and OR Public class CalcSales { public static void main (String[] args) Double salesAmount = 2500; Int hoursWorked = 3200; int yearsOfService = 20; Int bonus; If(( (salesAmount > 5000) || (hoursWorked > 3000) ) && (yearsOfService >=20 )) bonus = 400; }
Switch Statements
Switch Statements - continued
Boolean Values Public class CalcSales { public static void main (String[] args) boolean isMale = true; if (isMale) // do stuff } else // do other stuff
Boolean Values Public class CalcSales { public static void main (String[] args) boolean wonGame = false; Int myScore = 11; if (myScore >10) wonGame = true; } if (wonGame) System.out.println(“Congrats!”);
Code to get user input
Code to get user input Import java.swing.JOptionPane; Public class HelloNameDialog { Public static void main (String[] args) String result; Result = JOptionPane.showInputDialog(null, “What is your name?”); JOptionPane.ShowMessageDialog(null, “hello, “ + result + “!”); }
Code to convert input from string to integer