Presentation is loading. Please wait.

Presentation is loading. Please wait.

U More sophisticated decisions may require a multiplicity of options. u These can be handled by combining a series of if…else constructs into what is called.

Similar presentations


Presentation on theme: "U More sophisticated decisions may require a multiplicity of options. u These can be handled by combining a series of if…else constructs into what is called."— Presentation transcript:

1 u More sophisticated decisions may require a multiplicity of options. u These can be handled by combining a series of if…else constructs into what is called an if…else…if construct. u Note, we are combining the if…else in a (possibly long) series BUT the result will be that only one of the possibilities can be ‘selected’. Selection

2 Syntax of if...else…if if (inspection expression) { StatementBlock ; } else if (inspection expression) { StatementBlock ; } else if (inspection expression) { StatementBlock ; } else if (inspection expression) { StatementBlock ; ………... } else { StatementBlock ; } // end if else if Statement Block Inspection else Statement Block Statement Block Statement Block Statement Block T T T T F F F F

3 Syntax of if...else…if u The ‘inspection expressions’ are evaluated in sequence until u the FIRST one to yield a TRUE result is selected and executed, OR u all of them yield a FALSE result in which case none of the statements blocks is ‘selected’ for execution OR u there is an else option that is selected when all of the preceding inspections have failed.

4 So many ifs (and no buts)! u We could get by with just the if, in much the same way that a carpenter could get by with just a two- pound hammer (i.e. they would get the work done, after a fashion, but their fingers would be bruised and the shelf, roof, chair or cabinet may not look quiet as nice). u Having a choice of tools for similar tasks makes it possible to choose the most appropriate tool for the task. u Of course the consequence is that we have a slightly bigger toolbox but the benefit is that we will be able to use all of our fingers all of the time!

5 Example of if…else written with if public void sendText(String toNumber, String message) { if(signalLevel < USABLE_SIGNAL_LEVEL) { setDisplay("Poor signal. Message cannot be sent.") ; } if(signalLevel >= USABLE_SIGNAL_LEVEL) { /** * Create a text message object using the current time, * the number of this phone (i.e. the sender), the number * of the receipient's phone and the actual text of the message. */ Text txt = new Text(now(), number, toNumber, message); /** * Send the text message object to the provider and let them * look after delivering it. The text object contains all the * information the provider needs for billing (i.e. the sender's * number and receipient's number - in case roaming charges apply). * It also has all the information necessary for delivery. */ provider.sendText(txt); }

6 Example of if…else written with if public void switchOn() { if(batteryLevel < USABLE_BATTERY_LEVEL) { screenOn() ; setDisplay("Battery Low.....Charge the battery") ; pause(1500) ; clearDisplay() ; screenOff() ; } if(batteryLevel >= USABLE_BATTERY_LEVEL) { signalLevel = checkSignal() ; screenOn() ; clearDisplay() ; showLatestText() ; switchedOn = true ; timeSwitchedOn = now() ; }

7 Sequence and Selection u Summary u Sequence u involves the execution of statements in the order they are presented (not the order we assumed was obvious or ‘intended’) u Selection u For convenience most programming languages provide a number of selection constructs u Choosing the most appropriate one can make the code easier to read and write

8 Iteration/Repetition u The only justification for using a computer is to exploit it’s ability to reliably repeat a series of operations. u Whilst selection involves a ‘once off’ style inspection of the state and a decision on the basis of that inspection, repetition involves repeated inspection of the state coupled with a decision to continue the repetition or to stop it. u Selection is a decision – repetition is a process.

9 Syntax of WHILE Construct while (inspection expression) { Statement1 ; Statement2 ;...... StatementN ; } /* end while */ u The keyword while signals (or acts as a cue for Java) the use of an iteration construct. True False Statement Block inspection

10 Syntax of WHILE Construct u The opening { marks or signals the beginning of the statement block (i.e. the statements to be repeated). u The closing } marks or signals the end of the statement block and a return to the sequential path. u The curly brackets { and } delimit the scope of the while inspection expression. u Repetition is usually referred to as looping and the statement block as the “loop body”.

11 Semantics of WHILE construct u The inspection expression is tested at the beginning of the loop. u If evaluation of the expression produces a FALSE result the loop body is NOT executed and the WHILE terminates (or has no effect). u Execution of the program resumes on the sequential path at the statement immediately succeeding the WHILE.

12 Semantics of WHILE construct u If the expression produces a TRUE result the loop body is executed and continues to be executed as long as the expression remains TRUE. u The loop must contain some mechanism for enabling the expression to become FALSE otherwise once entered the loop will NEVER be exited (i.e. we will have created an infinite loop)

13 Objections to while  “while loops are unnatural in ordinary speech. Compare the following two sentences: While you haven't reached Central Ave drive straight. Drive straight; exit when you reach Central Ave check parent status while parents not home PARTY update parent status endwhile check credit status while credit > 0 Talk update credit status endloop check surf status while surfs up Surf update surf status endwhile

14 Possible counter argument check parent status while parents not home PARTY update parent status endwhile check surf status while surfs up Surf update surf status endwhile check credit status while credit > 0 Talk update credit status endloop

15 Benefits of WHILE u While inspects the state before deciding to execute the loop body – if the required state is already established then there is no need to execute the loop body. u Thus, only when it is necessary to alter the state does the while construct invest resources in doing it.

16 While example public void chargeBattery() { while(batteryLevel<MAXIMUM_BATTERY_LEVEL) { batteryLevel = batteryLevel + charge() ; } showBatteryLevel() ; }


Download ppt "U More sophisticated decisions may require a multiplicity of options. u These can be handled by combining a series of if…else constructs into what is called."

Similar presentations


Ads by Google