Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming 2 Decision-Making.

Similar presentations


Presentation on theme: "Programming 2 Decision-Making."— Presentation transcript:

1 Programming 2 Decision-Making

2 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

3 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”)’ }

4 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

5 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

6 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

7 No semi—colon at end of ‘if’
Int quizScore = 9; If (quizScore == 10); { system.out.println(“Perfect”); }

8 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

9 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!

10 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”);

11 Multiple statements in ‘if/else’

12 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

13 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

14 Constants Constant variables are variables whose values CANNOT change
public class CalcInterest { public static void main (String[] args) final double RATE = .008; double savingsTotal = double interestEarned = RATE * savingsTotal; System.out.println(interestEarned); RATE = .007; // can’t do this; cannot reassign }

15 Nested If/Else Statements

16

17 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; }

18 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; }

19 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; }

20 Switch Statements

21 Switch Statements - continued

22 Boolean Values Public class CalcSales { public static void main (String[] args) boolean isMale = true; if (isMale) // do stuff } else // do other stuff

23 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!”);

24 Code to get user input

25 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 + “!”); }

26 Code to convert input from string to integer


Download ppt "Programming 2 Decision-Making."

Similar presentations


Ads by Google