Download presentation
Presentation is loading. Please wait.
Published byShavonne Hart Modified over 9 years ago
1
Control Structures if else do while continue break switch case return for
2
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
3
If examples if (age > 65) stopPayingSocialSecurity = true; if (youAteLotsOfFood) youGetFat = true; if (grade < 65) letterGrade = “F”;
4
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....
5
Making If handle multiple statements.. if (theCowsComeHome) { statement1; System.out.println(“moo”); } else { // this is called a block of code theOtherThingYouMightDo; }
6
Conditional Operator letterGrade = (grade < 65) ? “F” : “pass”; Good for assignments with many simple boolean decisions.
7
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; }
8
Sample Output 10 9 8 7 6 5 4 3 2 1 0
9
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?”));
10
while example while (userWantsToContinue) { –keepDoingStuff; } while (weHaveMoreData) { –keepDoingDataProcessing; }
11
Do Example do { –promptUserForCommand; } while(userInputIsNotQuit);
12
for statement for (init; booleanCondition; iter) { } for (int i=0; i <= 10; i++) System.out.println(i);/// as an alternative Int i=0; //just like a while…. While (i<=10){System.out.println(i++); }
13
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
14
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 }
15
Cont. in While int i=0; while (i < 10) { i++; if ((i % 2) = =0) continue; System.out.println(i); } // 1.. 9
16
do continue! int i=0; do { i++; if ((i % 2) == 1) continue; System.out.println(i); } while(i < 10); //2..10
17
Break Break is a Java keyword Makes the target terminate. Must be in a break target. for, while, do, switch
18
Break example for(int i=0; i < 10; i++) { if (i < 5) break; System.out.println(i); // prints NOTHING }
19
while-break int i=0; while (i < 10) { i++; if ((i % 2) = =0) break; System.out.println(i); } // prints 1
20
do-break int i=0; do { i++; if ((i % 2) == 1) break; System.out.println(i); } while(i < 10); //prints nothing
21
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.
22
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.
23
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.
24
switch(argument) argument is byte, char, short, int NOT double, float, long or String
25
Switch Example int i = 10; switch(i) { case 2: { statement; break; } case 10: { statement; break; }
26
switch on char char a = ‘q’ switch (a) { case ‘q’: { quit(); break; } case ‘s’: { save(); break; }
27
default default is a keyword default appears in switch statements default is used when no case is satisfied
28
Example default char a = ‘z’ switch (a) { case ‘q’: { System.out.println(“quit”); break; } default: { System.out.println(“command not found”); break; }
29
return return is a reserved word. causes a sudden change in the flow of control to the invoking method.
30
example of return public static void main(String args[]) { System.out.println(“this is the return example”); return; }
31
example of return public static void main(String args[]) { double x = 10 * Math.cos(Math.PI); if (x > 20) return; System.out.println(x); }
32
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)); }
33
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)); }
34
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)); }
35
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)); }
36
return a string public static String getName() { return “CS131”; } public static void main(String args[]) { System.out.println(getName()); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.