Download presentation
Presentation is loading. Please wait.
1
CMT4001 -- Programming Software Applications Week 3 Dr. Xiaohong Gao TP Room B107 Control Structures
2
Contents To become familiar with control structures To become familiar with the if Selection Structure To become familiar with the if/else selection structure To become familiar with nested control structures To become familiar with nested if/else structures
3
Control Structures Before writing a program Have a thorough understanding of problem Carefully planned approach for solving it While writing a program Know what “building blocks” are available Use good programming principles Computing problems All can be solved by executing a series of actions in a specific order
4
Control Structures Algorithm Procedure in terms of actions to be executed order in which these actions are to be executed Example: " Rise and Shine " algorithm Get out of bed, take off pajamas, take a shower, get dressed, eat breakfast, carpool to work Program control Specify order in which statements are to executed
5
Control Structures Pseudocode Artificial, informal language Helps develop algorithms Similar to everyday English Not actually executed on computers “Think out” a program before writing it in a programming language Easy to convert into a corresponding Java program Consists only of executable statements Declarations are not executable statements Actions: input, output, calculation
6
Control Structures Bohm and Jacopini –Selection structures Three types : if, if/else, and switch –Repetition structures Three types : while, do/while, and for Keywords –Words reserved for Java –Cannot be used as identifiers or variable names
7
Control Structures
8
Flowchart Drawn using symbols connected by arrows called flowlines Graphical representation of an algorithm Rectangle symbol (action symbol) –Indicates any type of action. Oval symbol: Indicates beginning or end of a program, or a section of code Oval containing "Begin" is first symbol Oval containing "End" is last symbol When drawing a portion, small circles used Useful for algorithms, but psuedocode generally preferred
9
Control Structures Flowchart of sequence structure Two actions performed in order total = total + grade; add grade to total add 1 to counter counter = counter + 1;
10
The if Selection Structure // JavaVendorMachine0.java – no decision import java.io.*; public class JavaVendor Machine0 { public static void main(String[]args) { System.out.println(“Drop cup into position”); System.out.println(“Pour hot water through filter”); System.out.println(“Pour cream into cup”); System.out.println(“Open door”); }
11
The if Selection Structure import java.io.*; public class CoffeeVendorMachine { public static void main(String[] args) throws Exception { char userChoice; //create a char field //to hold the selection //tell the customer what the options are System.out.println("Enter 1 for cream:"); System.out.println("Enter 2 for no cream:");
12
The if Selection Structure // get the user's selection userChoice = (char) System.in.read(); System.out.println("Drop small cup into position."); System.out.println("Pour hot water through filter"); //dispense a cream if the user enter '1' //nothing otherwise; if(userChoice==1) System.out.println("Poor cream into cup"); System.out.println("Open door"); }
13
The if Selection Structure Output of the program: Enter 1 for cream: Enter 2 for no cream: 1 Drop small cup into position. Pour hot water through filter. Pour cream into cup. Open door. (Cont..)
14
The if Selection Structure Output of the program: Enter 1 for cream: Enter 2 for no cream: 2 Drop small cup into position. Pour hot water through filter. Open door.
15
The if Selection Structure Selection structure Used to choose among alternative courses of action Pseudocode: If user choice is 1, print “Pour cream into cup” If condition true Print statement executed and program goes on to next statement If condition false Print statement is ignored and the program goes onto the next statement Java ignores whitespace characters
16
The if Selection Structure if ( condition ) statement; Condition body Boolean condition Keyword 1 2 3 if (userChoice ==1) System.out.println(“Pour cream into cup”);
17
The if Selection Structure Selection structure –Pseudocode statement If user choice is 1 than print “add cream” Pseudocode statement in Java if ( userChoice==1 ) System.out.println( “Pour cream into cup" ); Corresponds closely to pseudocode Flowcharts –Diamond symbol - decision symbol Important - indicates an decision is to be made Contains expression that can be true or false
18
The if Selection Structure Flowcharts Diamond symbol has two paths Indicates what to do if condition true or false Decision can be made on anything that evaluates to a value of data type boolean true or false true false userChoice== 1 print “Add cream” if is a single-entry/ single-exit structure
19
The if/else Selection Structure Selection structures – if Only performs an action if condition true – if/else Performs different action when condition true than when condition false Pseudocode If user choice is ‘S’print ”Provide a small coffee”” else Print “Provide a large coffee”
20
The if/else Selection Structure Java code: if(userChoice ==‘S’) { System.out.println(“Drop a small cup into place”); System.out.println(“dispose 8 oz.hot water”); } else { System.out.println(“Drop a large cup into place”); System.out.println(“Dispose 12 oz.hot water”); }
21
The if/else Selection Structure import java.io.*; public class CoffeeVendorMachine1 { public static void main(String[] args) throws Exception { char userChoice; //create a char //field to hold the selection //tell the customer what the options are System.out.println(); System.out.println("Enter S for small cup\n"); System.out.println("Enter L for large cup\n"); System.out.println("Enter your selection, please:");
22
The if/else Selection Structure //get the user's selection userChoice = (char) System.in.read(); //dispense a small cup if the user enters 'S', // a large cup otherwise if (userChoice == 'S') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); }
23
The if/else Selection Structure else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz.hot water"); } System.out.println("Open door"); }
24
The if/else Selection Structure Program’s output: Enter S for small cup. Enter your selection, please: Small Drop small cup into position. Dispense 8 oz.hot water. Open door. (Cont…)
25
The if/else Selection Structure Program’s output: Enter S for small cup. Enter your selection, please: Large Drop large cup into position. Dispense 12 oz.hot water. Open door.
26
The if/else Selection Structure if (condition) { statement; // if condition true statement; } else { statement; // if condition false statement; } 5 4 3 21 False branch True branch Boolean test expression Keywords Braces
27
The if/else Selection Structure Flowchart of if / else structure Note that only symbols (besides circles and arrows) are Rectangles: actions Diamonds: decisions Action/decision model model of computing Programmer's job to assemble structures by stacking and nesting, then the define actions and decisions truefalse print “large cup” print “Small cup” userChoice==‘S’
28
Nested if/else structures Test for multiple cases Place if / else structures inside if / else structures If first condition met, other statements skipped
29
Nested if/else structures Nested structures if ( studentGrade >= 90 ) System.out.println( "A" ); else if ( studentGrade >= 80 ) System.out.println( "B" ); else if ( studentGrade >= 70 ) System.out.println( "C" ); else if ( studentGrade >= 60 ) System.out.println( "D" ); else System.out.println( "F" ); else only executes when the if condition fails..
30
Nested if/else structures // program using nested logic import java.io.*; public class CoffeeVendorMachine5 { public static void main(String[] args) throws Exception { char userChoice;//create a char field to hold // the selection //tell the customer what the options are System.out.println(); System.out.println("Bold, Brazilian Roast (Extra Octane)"); System.out.println(" 1 - Small");
31
Nested if/else structures System.out.println(" 2 - Large"); System.out.println("\nSuper Soother Italian Expres:"); System.out.println(" 3 - Small"); System.out.println(" 4 - Large"); System.out.println("Enter your selection, please:"); System.out.flush(); //get the user's selection userChoice = (char) System.in.read(); //decide between Brazilian filter and Italian //filter if ( userChoice=='1' || userChoice == '2')
32
Nested if/else structures { System.out.println("Put Brazilian Roast filter in place"); if (userChoice =='1') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz. hot water"); }
33
Nested if/else structures } else { System.out.println("Put Italian Expresso filter in place"); if (userChoice =='3') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz.hot water"); }
34
Nested if/else structures } System.out.println("Open door"); }
35
The if/else Selection Structure Important note Java always associates else with previous if unless braces ( {} ) present Dangling-else problem if ( x > 5 ) if ( y > 5 ) System.out.println( "x and y are > 5" ); else System.out.println( "x is <= 5" );
36
The if/else Selection Structure Does not execute as it appears, executes as if ( x > 5 ) if ( y > 5 ) System.out.println( "x and y are > 5" ); else System.out.println( "x is <= 5" );
37
The if/else Selection Structure Important note Must force structure to execute as intended Use braces to indicate that second if in body of first if ( x > 5 ) { if ( y > 5 ) System.out.println( "x and y are > 5" ); } else System.out.println( "x is <= 5" ); Compound statements Compound statement - set of statements within braces Can be used wherever a single statement can if expects one statement in its body To enclose multiple statements, enclose them in braces
38
CoffeeVendorMachine5.java //program using nested logic import java.io.*; public class CoffeeVendorMachine5 { public static void main(String[] args)throws Exception { char userChoice; //create a char //field to hold the selection //tell the customer what the options are System.out.println(); System.out.println("Bold,Brazilian Roast(Extra Octane)"); System.out.println(" 1 - Small"); System.out.println(" 2 - Large"); System.out.println("\nSuper Soother Italian Expresso:");
39
CoffeeVendorMachine5.java System.out.println(" 3 - Small"); System.out.println(" 4 - Large"); System.out.println("Enter your selection,please:"); System.out.flush(); //get the user's selection userChoice = (char) System.in.read(); //decide between Brazilian filter and Italian filter if ( userChoice=='1' || userChoice == '2') { System.out.println("Put Brazilian Roast filter in place");
40
CoffeeVendorMachine5.java if (userChoice =='1') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz. hot water"); } else { System.out.println("Put Italian Expresso filter in place");
41
CoffeeVendorMachine5.java if (userChoice =='3') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz.hot water"); } } System.out.println("Open door"); }
42
Summary Control Structure Selection Structure
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.