Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемСтанислав Акинфов
1 Object-Oriented Programming Ramzi Saifan Program Control Slides adapted from Steven Roehrig
2 Today We Look At Control structures More example programs
3 Execution Control: if-else if (boolean_expression) statement else if(boolean_expression) statement : else if(boolean_expression) statement else statement
4 if-else Example public int test(int testVal, int target) { int result = 0; if (testVal > target) result = 1; else if (testVal < target) result = -1; else { System.out.println(They are equal); result = 0; } return result; }
5 Execution Control: return Exit a method, returning an actual value or object, or not (if the return type is void). public int test(int testVal, int target) { if (testVal > target) return 1; else if (testVal < target) return -1; else { System.out.println(They are equal); return 0; }
6 Three Kinds of Iteration while (boolean_expression)// evaluate first statement_or_block do statement_or_block// evaluate last while (boolean_expression) for (initialization ; boolean_expression ; step) statement_or_block Example: for (int i = 0; i < myArray.size(); i++) { myArray[i] = 0; }
7 public class FlowControl { public static void main (String [] rr) { int i; for(i=0;i<3;i++) System.out.println(" For loop "+i); i=0; do { System.out.println(" Do while "+i); i++; }while (i<3); i=0; while (i<3) { System.out.println(" While "+i); i++; } Output: For loop 0 For loop 1 For loop 2 Do while 0 Do while 1 Do while 2 While 0 While 1 While 2
8 public class BreakAndContinue { public static void main(String[] args) { for (int i = 0; i < 100; i++) { if (i == 74) break;// out of for loop if (i % 9 != 0) continue;// next iteration System.out.println(i); } Break and Continue
9 Selection Via switch for (int i = 0; i < 100; i++) { char c = (char) (Math.random() * 26 + a); switch(c) { case a: case e: case i: case o: case u: System.out.println(c + Vowel); break; case y: case w: System.out.println(c + Sometimes a vowel); break; default: System.out.println(c + Not a vowel); }
10 Using Javas RNGs (cont.) java.lang.Math static double random()random in [0, 1.0) The sequence doesnt seem to be repeatable Bad for debugging Good for experimental work
11 Using Javas RNGs (cont.) java.util.Random Constructors: Random() Random(long seed) Methods: nextInt()random in (-2 31, ) nextInt(int n)random in [0, n) nextFloat()random in [0, 1) setSeed(long seed) java.lang.Math static double random()random in [0, 1.0)
12 Java.lang.Math Public static double : exp, log, log10, pow, random, sin, cos, tan, and many others Public static int: abs, ceil, floor, round
13 String Concatenation: + String greeting = Hello; String s = greeting.substring(0,4); //4 is the length String s1= greeting.substring(0,3)+!; Int n = greeting.length(); Char last = greeting.charAt(4); s.equals(HELLO); //never use == to compare Strings The String class contains more than 50 methods, define any variable as String, type its name plus dot
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.