java programmierkurs
play

Java-Programmierkurs Anweisungen zur Ablaufsteuerung WS 2012/2013 - PowerPoint PPT Presentation

Java-Programmierkurs Anweisungen zur Ablaufsteuerung WS 2012/2013 Prof. Dr. Margarita Esponda-Argero M. Esponda-Argero 1 Anweisungen zur Ablaufsteuerung if switch while do-while for M. Esponda-Argero 2 Anweisungen zur


  1. Java-Programmierkurs Anweisungen zur Ablaufsteuerung WS 2012/2013 Prof. Dr. Margarita Esponda-Argüero M. Esponda-Argüero 1

  2. Anweisungen zur Ablaufsteuerung if switch while do-while for M. Esponda-Argüero 2

  3. Anweisungen zur Ablaufsteuerung switch ( Ausdruck ) { if ( Ausdruck ) case Konstante1 : Anweisungen break; { Anweisungen } case Konstante2 : Anweisungen break; else . . . . . . . usw. { Anweisungen } default : Anweisungen } while ( Bedingung ) do { { Anweisungen Semikolon } Anweisungen while ( Bedingung ) ; } for ( Initialisierung ; Bedingung ; Inkrement ) { Anweisungen } M. Esponda-Argüero 3

  4. if-else-Anweisung Wahrheitswert → boolean Runde Klammern if ( Ausdruck ) { Anweisungen } else { Anweisungen } M. Esponda-Argüero 4

  5. if-else-Anweisung Kontrollfluss Anweisungen if Anweisungen if falsch Bedingung falsch Bedingung wahr wahr Anweisungen Anweisungen Anweisungen M. Esponda-Argüero 5

  6. if-else-Anweisung if( count == max ) if (a < b) { max = b; else count = 0; max = a; } Am Ende der if -Anweisung if( count == max ) ist der Inhalt der Variablen count = 0; max gleich der größten Zahl zwischen a und b. M. Esponda-Argüero 6

  7. if-else-Anweisung if ( punkte >= 90 ) { note = 1; } else { if ( punkte >= 80 ) { note = 2; } else { if ( punkte >= 70 ) { note = 3; } else { if ( punkte >= 60 ) { note = 4; } else { note = 5; } } } } M. Esponda-Argüero 7

  8. if-else-Anweisung ... if( punkte >= 90 ) note = 1; else if ( punkte >= 80 ) note = 2; else if ( punkte >= 70 ) note = 3; else if ( punkte >= 60 ) note = 4; else note = 5; ... M. Esponda-Argüero 8

  9. Pathologisches Beispiel count = 13; ; if (count == 5); { count ++; count += 5; } count--; count? 18 Kein Semikolon nach der Bedingung! M. Esponda-Argüero 9

  10. switch-Anweisung switch ( Ausdruck ) { case Konstante1 : Anweisungen break; case Konstante2 : Anweisungen break; . . . . . . . usw. default : Anweisungen } M. Esponda-Argüero 10

  11. switch-Anweisung short int switch ( Ausdruck ) { long • Click to edit Master case Konstante 1 : { char Anweisung 1 text styles Die switch-Anweisung Anweisung 2 erlaubt die Verzweigung in . . . Abhängigkeit vom Wert } eines ganzzahligen break; Ausdrucks, der mit einer case Konstante 2 : Anweisungen Reihe von Konstanten break; verglichen wird. . . . . . . . usw. default: Anweisungen } M. Esponda-Argüero 11

  12. switch-Anweisung Kontrollfluss a = k; Beispiel: switch (a) case 1: case 3: case 2: Anweisungen Anweisungen Anweisungen für a=1 für a=3 für a=2 nein nein break? break? ja ja M. Esponda-Argüero 12

  13. switch-Anweisung int zahl; String word; ... switch( zahl ) { case 1: word = "eins"; break; case 2: word = "zwei"; break; case 3: word = "drei"; break; case 4: word = "vier"; break; case 5: word = "fünf"; break; case 6: word = "sechs"; break; case 7: word = "sieben"; break; case 8: word = "acht"; break; case 9: word = "neun"; break; case 0: word = "null"; break; default: word = "error"; } M. Esponda-Argüero 13

  14. switch-Anweisung int monat, jahr, tage; ... switch( monat ) { case 1: case 3: • Click to edit Master case 5: case 7: text styles case 8: case 10: case 12: tage = 31; break; case 4: case 6: case 9: case 11: tage = 30; break; case 2: if ( jahr % 4 = = 0 && ( jahr % 100 != 0 || jahr % 400 = = 0 ) ) tage = 29; else tage = 28; break; default: System.out.println("falsche Monatsangabe"); }

  15. while-Schleife Wahrheitswert → boolean while ( Bedingung ) { Runde Klammern Anweisungen } M. Esponda-Argüero 15

  16. while-Anweisung Kontrollfluss while wahr Anweisungen Bedingung falsch M. Esponda-Argüero 16

  17. while-Anweisung public class Gluecksspieler { public static void main( String[] args ){ int bargeld = Integer.parseInt(args[0]); while ( bargeld > 0 ) { if (( (int)(Math.random()*1000)%2)==0) ++bargeld; else --bargeld; System.out.println( bargeld ); } System.out.println( "You're a big loser! \n" ); } } M. Esponda-Argüero 17

  18. Die Math-Klasse static double sin (double a) static double log (double a) static double cos (double a) static double pow (double a, double b) static double tan (double a) static double exp (double a) static double sqrt (double a) static double random () static long round (double a) Anwendungsbeispiel: static double ceil (double a) double x,y; static double floor (double a) . . . .. double zufallszahl = Math.random(); . . . . x = Math.sin (y); . . . . . M. Esponda-Argüero 18

  19. do-while-Anweisung do { Anweisungen } while ( Bedingung ) ; Bei der do-while-Anweisung wird zunächst der Schleifen-Rumpf ausgeführt und anschließend die Bedingung überprüft. M. Esponda-Argüero 19

  20. do-while-Anweisung Kontrollfluss do-while Anweisungen wahr Bedingung falsch M. Esponda-Argüero 20

  21. Die Keyboard-Klasse Ohne Gewähr! public static int readInt(); Einfache Klasse ohne public static double readDouble(); Fehlerbehandlung public static boolean readBool(); Die Methoden stürzen ab, public static float readFloat(); wenn die Eingabe mit dem public static short readShort(); Datentyp der Methoden nicht übereinstimmt. public static long readLong(); public static byte readByte(); public static String readText(); public static BigInteger readBigInteger(); M. Esponda-Argüero 21

  22. Die Keyboard-Klasse 1! = 1 public class TestKeyboard { 2! = 2 3! = 6 public static void main(String[] args ){ 4! = 24 int n, factorial = 1; 5! = 120 int counter = 0; 6! = 720 do{ 7! = 5040 System.out.print("n = "); 8! = 40320 n = Keyboard.readInt(); 9! = 362880 counter = n; 10! = 3628800 while (counter>0){ 11! = 39916800 factorial = factorial * counter; 12! = 479001600 counter--; 13! = 1932053504 } 14! = 1278945280 System.out.println(n+"! = "+factorial); 15! = 2004310016 factorial = 1; 16! = 2004189184 Überlauf! } while (n>=0); 17! = -288522240 } }// end of class TestKeyboard M. Esponda-Argüero 22

  23. for-Anweisung for ( Initialisierung ; Bedingung ; Inkrement ) { Anweisungen } for -Schleifen verwenden wir, wenn die Einschränkungen der Schleife ( Initialisierung, Bedingung und Inkrementierung ) bekannt sind. 1. Die Initialisierung wird einmal zu Beginn ausgeführt. 2. Der Schleifenrumpf wird ausgeführt, solange die Bedingung erfüllt ist. 3. Nach jedem Durchlauf des Rumpfes wird die Anweisung im Inkrement einmal ausgeführt und die Bedingung erneut geprüft. M. Esponda-Argüero 23

  24. for-Anweisung Die Initialisierung wird einmal zu Beginn ausgeführt. Vor jedem Durchlauf des Rumpfes wird die Bedingung geprüft. Nach jedem Durchlauf ... des Rumpfes wird die for (int i=0; i<=100; i++ ) { Anweisung im Inkrement einmal ausgeführt. System.out.println( i*i ); } ... M. Esponda-Argüero 24

  25. for-Anweisung Kontrollfluss for Initialisierung Inkrement true Bedingung Anweisungen false M. Esponda-Argüero 25

  26. for-Anweisung Alle Zahlen zwischen … 0 und 100, die for (int i=0; i<=100; i++ ) { genau durch 7 if ( i%7 == 0 ) System.out.println( i ); geteilt werden } können, werden … ausgegeben. Ausgabe 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 M. Esponda-Argüero 26

  27. for-Anweisung public class ForStatements { /*Hier berechnen wir die Fakultätsfunktion für alle Zahlen von 1 bis n. 1! 2! 3! 4! ... n! */ public static void main(String[] args) { int n=17; int fac = 1; for ( int index = 1; index <= n; index++ ) { fac = fac * index; System. out.println(index + "! = " + fac); } } } M. Esponda-Argüero 27

  28. for-Anweisung n 2 ≈ π 2 n 1 = 1 1 2 + 1 2 2 + ... + 1 ∑ … double sum = 0; R n = for(int i=1; i<=n; i++) i 2 6 { i = 1 sum += 1/i*i; falsch! } ... double pi = Math.sqrt(sum*6); double sum = 0; ... for(int i=1; i<=n; i++) { 2.449489742783178 sum += 1/(i*i); falsch! } double pi = Math.sqrt(sum*6); ... ... double sum = 0; 2.449489742783178 for(int i=1; i<=n; i++) ... ArithmeticException { double sum = 0; falsch! sum += 1.0/(i*i); for(int i=1; i<=n; i++) richtig! } { double pi = Math.sqrt(sum*6); sum += 1/(1.0*i*i); ... } double pi = Math.sqrt(sum*6); 3.1414971639472147 Infinity 3.141583104326456 M. Esponda-Argüero 28

  29. Klassenmethoden Methoden enthalten den ablauffähigen Programmcode. Es gibt keine Methoden außerhalb von Klassen. Methoden dürfen nicht geschachtelt werden. Beispiel: Methodenname Parameter Rückgabetyp Klassenmethode public static int umfang (int width, int height) { return 2*(width + height); } M. Esponda-Argüero 29

  30. return -Anweisung Die return-Anweisung beendet frühzeitig die Ausführung einer Methode. In einer Methode kann es mehrere return-Anweisungen geben. Methoden, die als Funktionen definiert sind, d.h. ein Ergebnis liefern, müssen durch eine return-Anweisung dieses Ergebnis zurückgeben. Wenn eine Methode kein Ergebnis zurückgibt, wird das Schlüsselwort void als Rückgabetyp verwendet, und eine return- Anweisung ist nicht erforderlich. M. Esponda-Argüero 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend