Control Structures if else do while continue break switch case return for.

36
Control Structures if else do while continue break switch case return for

Transcript of Control Structures if else do while continue break switch case return for.

Control Structures

if else do while continue break switch case return for

What is the if statement?

"Probable-Possible, my black hen,

She lays eggs in the Relative When.

She doesn't lay eggs in the Positive Now

Because she's unable to postulate how."

-- Frederick Winsor

If examples

• if (age > 65) stopPayingSocialSecurity = true;

if (youAteLotsOfFood)

youGetFat = true;

if (grade < 65)

letterGrade = “F”;

if else examples

• if (grade < 65)letterGrade = “F”;

else if ((grade > 65) && (grade < 70))

letterGrade = “D”;

else if ((grade >= 70) && (grade < 80))

letterGrade = “C”; // etc....

Making If handle multiple statements..

• if (theCowsComeHome) {statement1;

System.out.println(“moo”);

} else {

// this is called a block of code

theOtherThingYouMightDo;

}

Conditional Operator

letterGrade = (grade < 65) ? “F” : “pass”;

Good for assignments with many simple boolean decisions.

The While statement

• Why should I use the while statement?– Do something as long as some condition is met.

• int i = 10;

while ( i >= 0) {

System.out.println(i);

i = i – 1;

}

Sample Output

• 10 9 8 7 6 5 4 3 2 1 0

Do statement

• Why do I need the do statement?

• int i=0;

do {

System.out.println(i++);

} while (!(i > 10));

Do{….} while(In.getBoolean(“continue?”));

while example

• while (userWantsToContinue) {– keepDoingStuff;

• }

• while (weHaveMoreData) {– keepDoingDataProcessing;

• }

Do Example

• do {– promptUserForCommand;

• } while(userInputIsNotQuit);

for statement

• for (init; booleanCondition; iter) {• }• for (int i=0; i <= 10; i++)

System.out.println(i);/// as an alternativeInt i=0; //just like a while….While (i<=10){System.out.println(i++);

}

Continue

continue is a reserved word.

Continue alters the flow of control

Continue must reside in a continue target.

Some continue targets are:

WHILE, DO, FOR, SWITCH

What does continue do?

• Makes the target go to the next value.

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

if (i < 5) continue;

System.out.println(i); // prints 5..9

}

Cont. in While

int i=0;

while (i < 10) {

i++;

if ((i % 2) = =0) continue;

System.out.println(i);

} // 1 .. 9

do continue!

int i=0;

do {

i++;

if ((i % 2) == 1) continue;

System.out.println(i);

} while(i < 10); //2..10

Break

• Break is a Java keyword

• Makes the target terminate.

• Must be in a break target.

• for, while, do, switch

Break example

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

if (i < 5) break;

System.out.println(i); // prints NOTHING

}

while-break

int i=0;

while (i < 10) {

i++;

if ((i % 2) = =0) break;

System.out.println(i);

} // prints 1

do-break

int i=0;

do {

i++;

if ((i % 2) == 1) break;

System.out.println(i);

} while(i < 10); //prints nothing

labeled Continue

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

System.out.println(i+j);

if (i + j > 10) continue start;

}

}//doubly nested for-loop with labeled continue.

labeled break

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

System.out.println(i+j);

if (i + j > 10) break start;

}

}//doubly nested for-loop with labeled break.

// prints 0..9,1..10, 2..11 then stops.

switch statement

• switch – reserved word

• switch – makes a table out of several different statements and selects which ones get executed. Like a complex if-else.

switch(argument)

• argument is byte, char, short, int

• NOT double, float, long or String

Switch Example

int i = 10;

switch(i) {

case 2: {

statement;

break;

}

case 10: {

statement;

break;

}

}

switch on char

char a = ‘q’

switch (a) {

case ‘q’: {

quit();

break;

}

case ‘s’: {

save();

break;

}

}

default

• default is a keyword

• default appears in switch statements

• default is used when no case is satisfied

Example default

char a = ‘z’

switch (a) {

case ‘q’: {

System.out.println(“quit”);

break;

}

default: {

System.out.println(“command not found”);

break;

}

}

return

• return is a reserved word.

• causes a sudden change in the flow of control to the invoking method.

example of return

public static void main(String args[]) {

System.out.println(“this is the return example”);

return;

}

example of return

public static void main(String args[]) {

double x = 10 * Math.cos(Math.PI);

if (x > 20) return;

System.out.println(x);

}

My first method using return

public static int addOne(int x) {return x + 1;

}

public static void main(String args[]) {

System.out.println(addOne(1));

}

degs to rad example

public static double deg2rads(double d) {return d * Math.PI / 180.0;

}

public static void main(String args[]) {

System.out.println(deg2rads(180));

}

F2C

public static double f2c(double f) {return (5.0/9.0) * (f –32);

}

public static void main(String args[]) {

System.out.println(f2c(212));

}

future value of an investment

public static double fv(

double p, double r, double y) {return p * Math.pow(1+r,y);

}

public static void main(String args[]) {

System.out.println(fv(100,0.07,30));

}

return a string

public static String getName() {return “CS131”;

}

public static void main(String args[]) {

System.out.println(getName());

}