Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.

Similar presentations


Presentation on theme: "CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University."— Presentation transcript:

1 CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University

2 Making Decisions Background – Relational Operators Determine the relationship between two things and return true or false. < less than; <= less than or equal to > greater than; >= greater than or equal to == is equal to (yes, two equal signs right next to each other) != not equal to int x = 7; int y = 9; True or False: x < y : _______________ y < x : _______________ 3 != x: _______________ 3 != x : ____________ x == y : ____________ 7 < x : ____________ 7 <= x : ____________

3 the if Allows code to be executed if a certain condition is met Scanner s = new Scanner (System.in); int x = s.nextInt(); if (x < 10) { System.out.println(“Number smaller than 10”); } if this condition is true, then execute the code that is in this block.

4 the if … else If a condition is true, do something. Otherwise, if it is false, do something else. Scanner s = new Scanner (System.in); System.out.print(“Enter Grade: “); int g = s.nextInt(); char letter; if (g < 60) { System.out.println(“Failed”); letter = ‘F’; } else { System.out.println(“Passed”); letter = ‘P’; }

5 if…else Can check multiple conditions int x = 5; int y = 10; if( x < 6) { //do something here } else if (y < 10) { //do something else here } else { //if all else fails, do this } Note: Only one of the blocks will be executed… It will be the first one that evaluates to true.

6 Checkpoint If the sum of x & y is less than 100, print “super job” to the screen int x, y; Scanner s = new Scanner (System.in); System.out.print(“Enter two numbers: “); x = s.nextInt(); y = s.nextInt(); //Add if statement here

7 Checkpoint If the values of a and b are not equal, display “System Failure”. Otherwise, display “System Stable”. int a, b; Scanner s = new Scanner (System.in); System.out.print(“Enter the two values: “); a = s.nextInt(); b = s.nextInt();

8 Breakout 1

9 Formatting Output Format Money amounts for 2 decimal places… double salary = 45433.09; double increase = 0.0587; System.out.println(salary + salary * increase); //old way double newSal = salary + salary * increase; String salString = String.format("%.2f", newSal); System.out.println(salString); Format Specifier – will convert a floating point number (float or double) to string with certain constraints

10 Basic Loop The while loop uses a condition to determine if a group of statements will re-execute int x = 0; while (x < 4) { System.out.println(“I Love CSE 1341H”); x = x + 1; } Instead of going to the next line after the brace, a loop will return to the condition and re-evaluate it. x is Loop-control variable

11 Basic Loop The while loop uses a condition to determine if a group of statements will re-execute int x = 0; while (x < 4) { System.out.println(“I Love CSE 1341H”); x = x + 1; } Super-important statement… If it wasn’t there the loop would continue forever… Why?

12 Sum 10 Numbers from User Scanner s = new Scanner(System.in); int sum = 0; //stores the running total int c = 0; //loop control variable int temp; //temp space to hold number from user while (c < 0) { System.out.print (“Enter a number: “); temp = s.nextInt(); sum = sum + temp; c = c + 1; } System.out.println(“Sum is “ + sum);


Download ppt "CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University."

Similar presentations


Ads by Google