Professional Documents
Culture Documents
Selection and Flow Charts PDF
Selection and Flow Charts PDF
Selection and Flow Charts PDF
Some students find flow charts helpful to understand if statements. Below are
three code examples from the textbook with corresponding flow charts. To help
you better understand if statements, read each code example and compare it to
its corresponding flow chart.
It is often useful to write one if statement inside another if statement or inside
the else part of an if statement. Consider the problem to write a program that
reads from the keyboard the total weekly sales for a sales person and then outputs
the sales person’s commission according to this table.
If the sales amount is greater than or equal And the sales amount is less than Then the commission rate is
to
0 $300 0%
$1000 ∞ 3.0%
This can be easily written using nested if statements as shown in the following
code example.
Example 11
var sales = parseFloat(document.getElementById('sales'));
var rate;
if (sales < 300) {
rate = 0;
}
else if (sales < 600) {
rate = 0.02;
}
else if (sales < 1000) {
rate = 0.025;
}
else {
rate = 0.03;
}
var commission = sales * rate;
var output = 'The employee earned ' + commission;
document.getElementById('commission').innerHTML = output;
As another example, consider a sports team that wants to provide discounted
tickets to students and senior citizens and that wants to reward loyal fans that
attend multiple games according to this table.