Docsity

A Linear Notation and Generalizing Code in the Computer Science | CS 1030, Study notes of Computer Science

Toggle sidebar
bg1
3/26/2009
1
Chapter 11 โ€“ continued
Conditional Execution
Cascading If-else Statements
๎˜nested if-else structures are known as
cascading if-else
statements
because control cascades down the branches
๎˜the topmost level is evaluated first
๎˜if the test succeeds, then the corresponding statements
are executed and control moves to the next statement
following the cascading if
๎˜if the test fails, then control cascades down to the next if
test
๎˜in general, control cascades down the statement from one
test to another until one succeeds or the end of the
statement is reached
Cascading If-else Statements
example: nested if-else structure
A Cleaner Notation
๎˜when it is necessary to handle a large number of
alternatives, nested if-else statements can
become cumbersome and unwieldy
๎˜multiple levels of indentation and curly braces cause
the code to look cluttered make it harder to
read/understand
A Cleaner Notation
example: nested if statements vs. a more readable else-if Die Roll Example
๎˜consider a Web page that simulates the roll of a single die
๎˜will use an image to display the die
๎˜will use a button to initiate the die roll
๎˜when the user clicks the button, a random die roll is selected
and the corresponding image is displayed
pf3

Partial preview of the text

Download A Linear Notation and Generalizing Code in the Computer Science | CS 1030 and more Study notes Computer Science in PDF only on Docsity!

Chapter 11 โ€“ continued

Conditional Execution

Cascading If-else Statements

 nested if-else structures are known as cascading if-else

statements because control cascades down the branches

 the topmost level is evaluated first

 if the test succeeds, then the corresponding statements

are executed and control moves to the next statement

following the cascading if

 if the test fails, then control cascades down to the next if

test

 in general, control cascades down the statement from one

test to another until one succeeds or the end of the

statement is reached

Cascading If-else Statements

example: nested if-else structure

A Cleaner Notation

 when it is necessary to handle a large number of

alternatives, nested if-else statements can

become cumbersome and unwieldy

 multiple levels of indentation and curly braces cause

the code to look cluttered make it harder to

read/understand

A Cleaner Notation

example: nested if statements vs. a more readable else-if

Die Roll Example

 consider a Web page that simulates the roll of a single die

 will use an image to display the die

 will use a button to initiate the die roll

 when the user clicks the button, a random die roll is selected

and the corresponding image is displayed

Die Roll Page

 the RandomInt function

from random.js is used

to select the random roll

 depending on the roll

value, the correct image is

displayed

 since more than two

possibilities, a cascading

if-else is needed

Example

 Let's build that page

Generalizing Code

 note that each case in the cascading if-else follows the same pattern

if (roll == 1 ) { document.getElementById('die').src = "die 1 .gif"; } else if (roll == 2 ) { document.getElementById('die').src = "die 2 .gif"; } else if (roll == 3 ) { document.getElementById('die').src = "die 3 .gif"; } else if (roll == 4 ) { document.getElementById('die').src = "die 4 .gif"; } else if (roll == 5 ) { document.getElementById('die').src = "die 5 .gif"; } else { document.getElementById('die').src = "die 6 .gif"; }

Generalizing Code

document.getElementById('die').src =

"die" + roll + ".gif";

 this entire cascading if-else structure could be

replaced by the following:

Counters

 in software applications, if statements are often used to count occurrences of conditional or user- initiated events

 e.g., count the number of times dice rolls come up

doubles

 e.g., count the number of times the user guesses a

number correctly

Counters

 any variable that is used to record occurrences of an event is known as a counter

 initially, the counter is set to zero

 each time the specified action occurs, the counter is

incremented

 after a given time period, the value stored in the

counter will tell you the number of times the desired

event took place