260 likes | 292 Views
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.
E N D
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.
Types of Control Structures • Decision Control Structures • Repetition Control Structures • Branching Statements
Decision Control Structures • if statement B. if-else statement C. if-else-if statement D. switch statement
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
if statement • Form of the if-statement: if( boolean_expression) statement; if( boolean_expression ) { statement1; statement2; ..... } Decision Control Structures
if statement • Form of the if-statement: if( boolean_expression) statement; if( boolean_expression ) { statement1; statement2; ..... } Decision Control Structures
Example code snippet int grade = 68; if( grade==60 ) System.out.println(“Congratulations!”);
Example code snippet int grade = 89; if( grade>=75 ) { System.out.println(“Congratulations!”); System.out.println(“You passed!”); }
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!” } }
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.
If-else statement if( boolean_expression) statement; else statement; if( boolean_expression ) { statement1; statement2; ..... } else { statement1; statement2; … }
Example code snippet String name = “Emma”; if( name.equals(“Emma”)) { System.out.println(“Hi there, Emma!”); else { System.out.println(“Oh. Sorry. Wrong girl.”); }
5-6. Write down the form of an if-else statement which has multiple statements within the {}
If-else-if statement if( boolean_expression1 ) statement1; else if( boolean_expression2 ) statement2; ……. else statement3;
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?”); }
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”); }
Switch statement switch( switch_expression ) { case case_selector1: statement1; statement2; … break; case case_selector2: statement1; statement2; … break; … default: statement1; statement2; … break; }
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; }
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); }
15. Bonus question: Which of the following Spanish phrases translate to: “Thank You Very Much!” • Buenos Dias! • Muchas Gracias! • Buenas Noches! • Bienvenido! • Zahlamat PowHH!
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?