Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007....

14
Conditional Statements and Loops Conditional Statements and Loops

Transcript of Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007....

Page 1: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

Conditional Statements and LoopsConditional Statements and Loops

Page 2: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

If/elseif (<conditional expression>)

<statement1>else

<statement2>

if (<conditional expression>)<statement>

Page 3: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

examplesint x = scan.nextInt();if(x < 0)

System.out.println(“negative”);if(x > 0)

System.out.println(“positive”);if(x == 0)

int x = scan.nextInt();if(x < 0)

System.out.println(“negative”);else if(x > 0)

System.out.println(“positive”);else if(x == 0)

System.out.println(“zero”);

if(x == 0)System.out.println(“zero”);

int x = scan.nextInt();if(x < 0)

System.out.println(“negative”);else if(x > 0)

System.out.println(“positive”);else

System.out.println(“zero”);

int x = scan.nextInt();

if(x < 0) {

System.out.println(“negative”);

}

else if(x > 0) {

System.out.println(“positive”);

}

else if(x == 0) {

System.out.println(“zero”);

}

Page 4: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

checking for rangesint age = scan.nextInt();

if(age <= 0)

System.out.println(“wrong entry”);

else if (age < 10)

System.out.println(“Welcome to life”);

else if(age < 20)

int age = scan.nextInt();

if(age <= 0)

System.out.println(“wrong entry”);

if (age < 10)

System.out.println(“Welcome to life”);

if(age < 20)

��

else if(age < 20)

System.out.println(“You’ve time to play”);

else if(age <30)

System.out.println(“Time to learn java”);

else if(age <40)

System.out.println(“Go find a job”);

else if(age <50)

System.out.println(“Enjoy your life”);

else

System.out.println(“write your will”);

if(age < 20)

System.out.println(“You’ve time to play”);

if(age <30)

System.out.println(“Time to learn java”);

if(age <40)

System.out.println(“Go find a job”);

if(age <50)

System.out.println(“Enjoy your life”);

else

System.out.println(“write your will”);

Page 5: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

common mistakes� no semicolon after if or else line

if(x<10) ;System.out.println(“small”);

� dangling else goes to the nearest if� dangling else goes to the nearest if

if (true)

if ( false)

System.out.println("a");

else

System.out.println("b");

� no assignment in the condition

if(x = 5)

System.out.println(“five”);

Page 6: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

while loop

//calculate the root of x2 -5x + 6 = 0

int x = -1000;

//super HelloWorld

int i = 0;int x = -1000;

while( (x*x-5*x+6) != 0) {

x = x + 1;

}

System.out.println(“root = “ + x);

int i = 0;

while(i<10) {

System.out.println(“HelloWorld”);

i++;

}

Page 7: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

do-while loop

//check the input

int x;

//calculate the root of x2 -5x + 6 = 0

int x = -1000;int x;

do {

System.out.println(“Enter a positive number”);

x = scan.nextInt();

} while(x < 0);

int x = -1000;

do {

x = x + 1;

} while( (x*x-5*x+6) != 0) ;

System.out.println(“root = “ + x);

Page 8: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

for loop

//multiplication table of 5

for(int i = 0; i < 10; i++) {

//all multiplication tables

for(int j = 0; j < 10; j++) {for(int i = 0; i < 10; i++) {

System.out.println

(i + “ * 5 = “ + (i * 5));

}

for(int j = 0; j < 10; j++) {

for(int i = 0; i < 10; i++) {

System.out.println

(i + “ * “ + j +” = “ + (i * j));

}

System.out.println();

}

Page 9: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

exchanging loops

//multiplication table of 5

int i = 0;

while(i < 10) {

System.out.println

(i + “ * 5 = “ + (i * 5));

i++;//multiplication table of 5

for(int i = 0; i < 10; i++) {

System.out.println

(i + “ * 5 = “ + (i * 5));

}

i++;

}

int i = 0;

do {

System.out.println

(i + “ * 5 = “ + (i * 5));

i++;

} while (i<10);

Page 10: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

fun with loops and conditions!� We will write two programs

1. The computer chooses a number, you have to guess it.

2. You choose a number, the computer has to guess it.

Page 11: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

Task 1int x = (int) (100*Math.random());int y;boolean guessed = false;do {

System.out.println(“Guess my number (0-99)”);y = scan.nextInt();y = scan.nextInt();if(x < y)

System.out.println(“My number is smaller”);else if(x > y)

System.out.println(“My number is larger”);else {

System.out.println(“That is my number!”);guessed = true;

}} while(!guessed);

Page 12: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

Task 2 (stupid solution)System.out.println(“Choose a number (0-99) and I will guess it”);

boolean guessed = false;

for(int i=0; i<100 && !guessed; i++) {

System.out.println(“is “ + i + “ your number?”);

guessed = scan.nextBoolean();

}

Page 13: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

Task 2 (smart solution)System.out.println(“Choose a number (0-99) and I will guess it”);boolean guessed = false;int low = 0, high = 99;while(!guessed) {

int num = (low + high) / 2;System.out.println(“My guess is“ + num);System.out.println(“My guess is“ + num);System.out.println(“Enter 1 if equal, 2 if smaller, 3 if larger”);int a = scan.nextInt();if(a==1)

guessed = true;else if(a==2)

high = num;else if(a==3)

low = num;}System.out.println(“That was quick!”);

Page 14: Conditional Statements and Loops - McGill Universityclump/comp202/tutorials/ControlFlow... · 2007. 10. 5. · checking for ranges intage = scan.nextInt(); if(age

References� Programmer's Guide to Java™ Certification, A: A

Comprehensive Primer, Second Edition

� The Java Tutorials, http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/