Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.

Similar presentations


Presentation on theme: "CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must."— Presentation transcript:

1 CSE 110 Midterm Review Hans and Carmine

2 If Statements If statements use decision logic In order to properly use if statements relational operators must be used: == : equal > : one value is greater than the other < : one value is less than the other != : not equal compareTo() : method used to compare multiple Strings Whenever relational operators are used, a boolean value is returned

3 If Statements - What do they look like? if ( ) { //code to do stuff goes here } int num = 5; if (num > 1) { System.out.println(“My number is “ + num); } Output: My number is 5

4 If, else if, and else int num = 6; if (num > 10) { System.out.println(“Big number!”); } else if (num == 10) { System.out.println(“Almost big.”); } else { System.out.println(“Small number!”); } Output: Small number!

5 ‘If’ Problem String animal = “dog”; boolean barks = false; int age = 2; int legs = 3; if (animal.equals(“cat”)) { age = age + 5; } else if (animal.equals(“dog”)) { barks = true; } else { barks = false; } if (barks == true && age < 5) { legs = legs + 1; System.out.println(“The dog is young.”); } if (legs == 7 || legs == 3) { System.out.println(“The dog doesn’t have 4 legs); } else { if (legs == 4 && barks != false) { System.out.println(“It’s a normal dog.”) } else { System.out.println(“Is this even a dog?”); }

6 If Solution Output: The dog is young. It’s a normal dog.

7 For loops Explanation: For loops contain 3 different parts: Declaration (int i = 0) Stopping condition (i < 10) Changing the variable (i++) Syntax: for(Delcaration; Stopping Condition; Changing the variable){ //code } Problem: create a loop that prints out a countdown from n (n being the user input) to 0, then prints “All done!” at the end of the countdown

8 For loop Solution Scanner scan = new Scanner(System.in); System.out.println(“Enter a number:”); int n = scan.nextInt(); for (int i = n; i <= 0; i = i-1) { System.out.println(i); } System.out.println("All done!"); //This code prints Enter a number: 6 5 4 3 2 1 0 All Done!

9 While loop Similar to a for loop, a while loop runs the statements within the code block until the conditional returns false. Syntax: while(conditional statement) { //code } Problem: create a loop that counts down from 4 to 1. Then create a separate loop that counts from 1 to 4.

10 While loops Example int i = 4; while(i > 0) { System.out.println(i); i--; } int i = 1; while(i > 0) { System.out.println(i); i++; } int i = 1; while(i < 5) { System.out.println(i); i++; } //Output 4 3 2 1 //Output Infinite while loop //Output 1 2 3 4

11 Do while loops Do while loops can be thought of as an inverted while loop. The conditional goes at the end of the loop and is checked last, instead of first Syntax: do { //code }while(conditional);

12 Do while loop Example int i = 5; do { System.out.println(i); i--; }while(i > 0); int i = 0; do { System.out.println(i); i++; }while(i > 0); int i = 0; do { System.out.println(i); i++; }while(i < 5); //Output 5 4 3 2 1 //Output Infinite do while loop //Output 0 1 2 3 4

13 Switch statements A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax: switch(expression) { case value: //code case value: //code }

14 Switch Example int month = 3; String monthString; switch (month) { case 1:monthString = “January”; break; case 2:monthString = “February”; break; case 3: monthString = “March”; break; case 4:monthString = “April”; break; } What month is it? Solution: March

15 More Examples Boolean state = true; int x = 5; while(state) { if(x < 0) state = false; else if(x == 1) break; else{ System.out.println(x); x--; } System.out.println(“All Done!”) //Output 5 4 3 2 All Done! Boolean state = true; int x = 0; while(state) { if(x > 5) state = false; else x++; } for(int i = 0; i < x; i++) { System.out.println(i); } System.out.println(“Finish!”); //Output 0 1 2 3 4 Finish!

16 More Examples Scanner myScan = new Scanner(System.in); int num = 6; int choice = 0; for (int i = 0; i < num; i++) { choice = myScan.nextInt(); switch (choice) { case 1:i = i + 2; System.out.println(“case1”); break; case 2:num = num -2; System.out.println(“case2”); break; default:System.out.println(“default”); break; } What is the output if the user inputs?: 2 3 1 2 case2 default case 1 //The for loop has already been exited so nothing happens.


Download ppt "CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must."

Similar presentations


Ads by Google