SlideServe
Menu
Préférences
© SlideServe · v2026

Control Structures

stinej avatar
26 diapositives · 399 Views · Jan 05, 2020

Learn about decision & repetition control structures, including if, if-else, if-else-if, switch statements. Practice code snippets and fill-in-the-blank exercises to enhance your programming skills.

Control Structures - slide 1
01 / 26Control Structures
1 / 17

Télécharger la présentation

Control Structures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Control Structures Chapter 6. Introduction to Programming Student’s Manual

  2. Importance We get rid of Sequential Programs - statements are executed one after the other Control structures - allow us to change the ordering of how the statements in our program are executed.

  3. Types of Control Structures • Decision Control Structures • Repetition Control Structures • Branching Statements

  4. Decision Control Structures • if statement B. if-else statement C. if-else-if statement D. switch statement

  5. 1-3. What are the three types of control structures?

  6. if statement • Specifies that a statement (or block of code) will be executed if and only if a certain boolean statement is true. Decision Control Structures

  7. if statement • Form of the if-statement: if( boolean_expression) statement; if( boolean_expression ) { statement1; statement2; ..... } Decision Control Structures

  8. if statement • Form of the if-statement: if( boolean_expression) statement; if( boolean_expression ) { statement1; statement2; ..... } Decision Control Structures

  9. Example code snippet int grade = 68; if( grade==60 ) System.out.println(“Congratulations!”);

  10. Example code snippet int grade = 89; if( grade>=75 ) { System.out.println(“Congratulations!”); System.out.println(“You passed!”); }

  11. 4. Replace comment with codes public class Juice { public static void main(String [] args) { BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); String answer = “”; System.out.println(“What is the chemical symbol for Lithium?”); try { answer = dataIn.readLine(); } catch( IOException e ) { System.out.println(“Error!”); } //if answer is Li, program outputs: “You are correct!” } }

  12. If-else statement Used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false.

  13. If-else statement if( boolean_expression) statement; else statement; if( boolean_expression ) { statement1; statement2; ..... } else { statement1; statement2; … }

  14. Example code snippet String name = “Emma”; if( name.equals(“Emma”)) { System.out.println(“Hi there, Emma!”); else { System.out.println(“Oh. Sorry. Wrong girl.”); }

  15. 5-6. Write down the form of an if-else statement which has multiple statements within the {}

  16. If-else-if statement if( boolean_expression1 ) statement1; else if( boolean_expression2 ) statement2; ……. else statement3;

  17. Example code snippet String payment=“”; try { payment = dataIn.readLine(); } catch( IOException e ) { System.out.print(“Error!”); } int amount = Integer.parseInt(payment); if( amount == 5 ) { System.out.println(“Your change is 3 pesos”); } else if( amount == 4 ) { System.out.println(“Your change is 2 pesos”); } else if( amount == 3 ) { System.out.println(“Your change is 1 peso”); } Else { System.out.println(“Are you kidding me?”); }

  18. 7.-9. Fill in the blanks. A program that outputs OUTSTANDING when the grade is greater than or equal to 90, GOOD if it is greater than or equal to 80, and FAILURE if otherwise.. int grade = 95; ___________{ System.out.println(“OUTSTANDING”); } ___________{ System.out.println(“GOOD”); ___________ { System.out.println(“FAILURE”); }

  19. Switch statement switch( switch_expression ) { case case_selector1: statement1; statement2; … break; case case_selector2: statement1; statement2; … break; … default: statement1; statement2; … break; }

  20. Example code snippet int grade = Integer.parseInt(dataIn.readLine()); switch(grade) { case 100: System.out.println(“Excellent!”); break; case 90: System.out.println(“Good Job!”); break; case 80: System.out.println(“Study harder!”); break; default: System.out.println(“Sorry, you failed.”); break; }

  21. 10-14. Fill in the blanks intx, y;BufferedReader object = new ____________                                                         (new InputStreamReader(System.in));System.out.println("Enter two numbers for operation:");try{      x = Integer.parseInt(object.readLine());      y = Integer.parseInt(object.readLine());System.out.println("1. Add");System.out.println("2. Subtract")System.out.println("enter your choice:");inta= Integer.parseInt(object.readLine()); __________(a){      ___________:System.out.println(“Their sum is =" + ______);break;case 2:System.out.println(“Their difference is=" + (x-y));__________:System.out.println("Invalid Entry!");      }    }catch(NumberFormatException ne){System.out.println(ne.getMessage() + " is not a numeric value.");System.exit(0);    }

  22. 15. Bonus question: Which of the following Spanish phrases translate to: “Thank You Very Much!” • Buenos Dias! • Muchas Gracias! • Buenas Noches! • Bienvenido! • Zahlamat PowHH!

  23. 16. Bonus: Which of the following Japanese phrases translate to “How are you?” • Onegai Shimasu? • Genki Desu? • Ogenki Desu ka? • Domo Arigato? • Do itashi Mashite?

  24. 17. Bonus: Take a good look at the picture

  25. How many stars did you see?

  26. Reminder: Group Project