Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals and Loops

Similar presentations


Presentation on theme: "Conditionals and Loops"— Presentation transcript:

1 Conditionals and Loops
COMP 202 – Tutorial 3 Conditionals and Loops

2 If, Else if (x == z && z == (y – 16)) { int x = 7; v = 2*x + y;
int y = 23; int z = y – 16; int w, v; if (x == y) { w = 2*x; } else w = 2*y; if (x == z && z == (y – 16)) { v = 2*x + y; w = v + 1; } else { w = 2*y + z; v = w – 1; System.out.println(w + “ “ + v); // What comes out?

3 String name1 = “Neumann”;
if (name1.equals(name2)) System.out.println(name1); else if (name2 == name3) System.out.println(name3); else System.out.println(“No match”);

4 int x = scan.nextInt(); int y; if (x >0) if (x > 0) y = 1; y = 1; if (x < 0) else if (x < 0) y = -1; y = -1; if (x == 0) else if (x == 0) y = 0; y = 0;

5 A complicated example int x = 6; int y = 7; int z = 22; int w = 22;
int v = 0; if (x == y || z == w) if (y == w) v = z + x; else if (y == w) // else belongs to second if v = z – x; // an else corresponds to the last if before it.

6 A complicated example int x = 6; int y = 7; int z = 22; int w = 22;
int v = 0; if (x == y || z == w) { if (y == w) v = z + x; } else if (y == w) // NOW else belongs to first if v = z – x;

7 The Conditional Statement
int x = scan.nextInt(); int y = scan.nextInt(); int z = (x <= y) ? x+2 : y+2;

8 for int i, j; for (i = 0; i < 27; i++) j = i^2; int j = 23;
for (int i = 0; i < j; i++) System.out.println(i);

9 Nested for Loop int x,y; for (x = 0; x != 3; x++) {
for (y = 0; y != 3; y++) System.out.println("Coordinate "+x+","+y); }

10 while int i,j; int j = 23; i = 0; int i = 0; while (i<27)
{ j = i^2; i++; } int j = 23; int i = 0; while (i < j) { System.out.println(i); i++; }

11 do while int i,j; int j = 23; i = 0; int i = 0; do do { {
// Will happen at least once int j = 23; int i = 0; do { System.out.println(i); i++ } while (i < j);

12 Torment A user is required to pay $230 through a program. Use any loops/conditionals so that the user enters an amount. Then check if the amount is correct, and ask the user to modify it if not.

13 Answer – for Scanner scan = new Scanner(System.in);
final int AMOUNT = 230; // turned to constant int payment = 0; int modification = 0; for (/*empty field*/ ; payment != AMOUNT ; payment+=modification) { System.out.println("Payment given so far: " + payment); System.out.println("Please enter your payment/deduction:"); if (payment < AMOUNT) modification = scan.nextInt(); // If the user over-payed, the next modification will be a deduction. else modification = - scan.nextInt(); }

14 Answer – do-while Scanner scan = new Scanner(System.in);
final int AMOUNT = 230; // turned to constant int payment = 0; int modification = 0; do { System.out.println("Please enter your payment/deduction:"); if (payment < AMOUNT) payment += scan.nextInt(); else payment -= scan.nextInt(); System.out.println("Payment given so far: " payment); } while (payment != AMOUNT);


Download ppt "Conditionals and Loops"

Similar presentations


Ads by Google