Unformatted text preview:

Lecture 4:Control StructuresLecture OutlineWhat are Control Structures?Decision StatementsIf StatementsIf Statement Flow ChartIf-Else StatementsIf-Else Statement Flow ChartExample of Chained If-Else StatementsSwitch StatementsSwitch Statement Flow ChartBreak Statements in Switch StatementsRemember the Example…LoopsThe For LoopExampleAnother ExampleThe while LoopExampleUsing the break Statement in LoopsNested LoopsControl Structures Pop QuizControl Structures Pop QuizLecture SummaryLecture 4:Control StructuresKenya 2005©2005MIT-Africa Internet Technology InitiativeLecture Outline• What control structures are• Different types of control structures:– Block Statements– Decision Statements– Loops©2005MIT-Africa Internet Technology InitiativeWhat are Control Structures?• Without control structures, a computer would evaluate the instructions in a program step-by-step• Control structures allow you to change:– the order in which instructions are evaluated– which instructions are evaluated– and control the “flow” of the program• Control structures include:– block statements (anything contained within curly brackets)– decision statements– loopsDecision Statements©2005MIT-Africa Internet Technology InitiativeIf Statements• The “if” decision statement causes a program to execute a statement conditionallyif (expression) {statement;}next_statement;• The expression must produce either true or false, also known as a boolean value• If expression returns true, statement is executed and then next_statement• If expression returns false, statement is not executed and the program continues at next_statement©2005MIT-Africa Internet Technology InitiativeIf Statement Flow Chartexecute statementexecute next_statementexpression is true?noif (expression) { statement;}next_statement;yes©2005MIT-Africa Internet Technology InitiativeIf-Else Statements• The basic “if” statement can be extended by adding the “else” clause in order to do something if expression is falseif (expression) {statement1;}else{statement2;}next_statement;• Again, the expression must produce a boolean value• If expression returns true, statement1 is executed and then next_statement is executed.• If expression returns false, statement2 is executed and then next_statement is executed.©2005MIT-Africa Internet Technology InitiativeIf-Else Statement Flow Chartexpressionis TRUE?execute statement1execute statement2execute next_statementif (expression){statement1;} else {statement2;}next_statement;noyes©2005MIT-Africa Internet Technology InitiativeExample of Chained If-Else Statements• Note that you can combine if-else statements below to make a chain to deal with more than one caseif (grade == 'A')System.out.println("You got an A.");else if (grade == 'B')System.out.println("You got a B.");else if (grade == 'C')System.out.println("You got a C.");elseSystem.out.println("You got an F.");©2005MIT-Africa Internet Technology InitiativeSwitch Statements•The switch statement is another way to test several casesgenerated by a given expression.• The expression must produce a result of type char, byte,short or int, but notlong, float, or double.• For example:switch (expression) {case value1:statement1;case value2:statement2;default:default_statement;}• NOTE: Every statement after the true case is executed©2005MIT-Africa Internet Technology InitiativeSwitch Statement Flow Chartynnexpressionequalsvalue1?Do value1 thingDo value2 thingDo default actionexpressionequalsvalue2?Continue theprogramswitch (expression){case value1:// Do value1 thingcase value2:// Do value2 thing...default:// Do default action}// Continue the programy©2005MIT-Africa Internet Technology InitiativeBreak Statements in Switch Statements•The break; statement tells the computer to exit the switch statement• For example:switch (expression) {case value1:statement1;break;case value2:statement2;break;default:default_statement;break;}©2005MIT-Africa Internet Technology Initiativeexpressionequalsvalue1?expressionequalsvalue2?Do default actionDo value1 thingDo value2 thingbreakbreakbreakContinue the programswitch (expression){case value1:// Do value1 thingbreak;case value2:// Do value2 thingbreak;...default:// Do default actionbreak;}// Continue the programyynn©2005MIT-Africa Internet Technology InitiativeRemember the Example…• Here is the example of chained if-else statements:if (grade == 'A')System.out.println("You got an A.");else if (grade == 'B')System.out.println("You got a B.");else if (grade == 'C')System.out.println("You got a C.");elseSystem.out.println("You got an F.");©2005MIT-Africa Internet Technology Initiative• Here is the way to convert the chained if-else statement to a switch statement switch (grade) {case 'A':System.out.println("You got an A.");break;case 'B':System.out.println("You got a B.");break; case 'C':System.out.println("You got a C.");break; default:System.out.println("You got an F.");}Loops©2005MIT-Africa Internet Technology Initiative• A loop allows you to execute a statement or block of statements repeatedly.• There are three types of loops in Java:1. for loops2. while loops3. Do-while loops (will not discuss in this course)©2005MIT-Africa Internet Technology InitiativeThe For Loopfor (initialization_expression; loop_condition; increment_expression) {//statement}• The control of the for loop appear in parentheses and is made up of three parts.1. The first part, the initialization_expression, sets the initial conditions for the loop and is executed before the loop starts.2. Loop executes so long as the loop_condition is true and exits otherwise3. The third part of the control information, the increment_expression, is used to increment the loop counter. This is executed at the end of each loop iteration.©2005MIT-Africa Internet Technology InitiativeExampleint limit = 5;int sum = 0;for(int i = 1; i<=limit; i++){/* initialization_expressionloop_conditionincrement_expression */// sum = sum + 2;sum += 2;}• What is the value of sum ?sum = 2sum = 4sum = 6sum = 8sum = 10i = 1i = 2 i = 3i = 4i = 5 i = 610©2005MIT-Africa Internet Technology InitiativeAnother Examplefor(int div = 0; div<1000; div++) {if(div % 12 == 0){System.out.println(div+"is divisible by 12");}}•This loop will display every number from 0 to 999 that is evenly divisible by 12.©2005MIT-Africa Internet Technology Initiative• If there is more than one variable to set up or increment they are


View Full Document

MIT SP 772 - Control Structures

Download Control Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?