1 / 26

Control Structures

Control Structures. Chapter 6. Introduction to Programming Student’s Manual. 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.

stinej
Download Presentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

More Related