Decisions and Loops: CS 200 - Introduction To Programming
Decisions and Loops: CS 200 - Introduction To Programming
1
• Please upload assignments at
- https://lms.lums.edu.pk
•Please subscribe to Google Groups if you have
not already done so
- cs200f10
• Class Website
- http://chand.lums.edu.pk/~cs200f10
2
Your Program is Like a Network
of Roads in a City
4
Using Boolean Expressions
• A Boolean Expression is an expression that is
either true or false
- Boolean expressions are evaluated using
relational operations such as
• = = , < , and >= which produce a boolean
value
• &&,– | and
|, and ! which also
boolean produce asuch
operations boolean
as value
• Type bool allows declaration of variables that
carry the value true or false
5
Evaluating Boolean Expressions
•Boolean expressions are evaluated using
values
from the Truth Tables in
- For example, if y is 8, the expression
!( ( y < 3) | | ( y > 7) )
is evaluated in the following sequence
! ( false | | true )
! ( true )
false
6
Precedence Rules
• Items in expressions are grouped by
precedence rules for arithmetic and boolean
operators
- Operators with higher precedence are performed
first
- Binary operators with equal precedence are
performed left to right
- Unary operators of equal precedence are
performed right to left
7
8
Complex Boolean Expression
9
Precedence Rules
10
Precedence Rule Example
• The expression
(x+1) > 2 || (x + 1) < -3
is equivalent to
( (x + 1) > 2) || ( ( x + 1) < -3)
•
and is also equivalent to
x + 1 > 2 || x + 1 < - 3
11
Evaluating x + 1 > 2 | | x + 1 < - 3
• Using the precedence rules
- First apply the unary -
- Next apply the +'s
- Now apply the > and <
- Finally do the | |
12
Block
• What is a block?
• Properties of a block?
• Scoping of a block?
13
Blocks
• Each branch of a switch or if-else statement is
a separate sub-task
- If the action of a branch is too simple to warrant a
function call, use multiple statements between braces
- A block is a section of code enclosed by braces
- Variables declared within a block, are local to the
block or have the block as their scope.
• Variable names declared in the block can be reused outside
the block
14
Statement Blocks
• A statement block is a block that is not a function
body or the body of the main part of a program
• Statement blocks can be nested in other
statement blocks
- Nesting statement blocks can make code difficult to
read
- It is generally better to create function calls than to
nest statement blocks
15
Scope Rule for Nested Blocks
• If a single
identifier is declared as a variable in
each of two blocks, one within the other, then
these are two different variables with the same
name
- One of the variables exists only within the inner
block and cannot be accessed outside the inner
block
- The other variable exists only in the outer block and
cannot be accessed in the inner block
16
While Loop Operation
• First, the boolean expression is evaluated
- If false, the program skips to the line following the
while loop
- If true, the body of the loop is executed
• During execution, some item from the boolean expression
is changed
- After executing the loop body, the boolean
expression is checked again repeating the process
until the expression becomes false
• A while loop might not execute at all if the
boolean expression is false on the first check
17
18
do-while loop
• A variation of the while loop.
• A do-while loop is always executed at least once
- The body of the loop is first executed
- The boolean expression is checked after the body
has been executed
• Syntax:
• do
{
statements to repeat
}
while (boolean_expression);
19
20
Increment/Decrement
•Unary operators require only one operand
- + in front of a number such as +5
- - in front of a number such as -5
• ++ increment operator
- Adds 1 to the value of a variable
x ++;
is equivalent to x = x + 1;
• -- decrement operator
21
Infinite Loops
• Loops that never stop are infinite loops
• The loop body should contain a line that will
eventually cause the boolean expression to
become false
• Example: Print the odd numbers less than 12
x = 1;
while (x != 12)
{
cout << x << endl;
x = x + 2;
}
• Better to use this comparison: while ( x < 12)
22
Short-Circuit Evaluation
• Some boolean expressions do not need to be
completely evaluated
- if x is negative, the value of the expression
(x >= 0) && ( y > 1)
can be determined by evaluating only (x >= 0)
• C++ uses short-circuit evaluation
- If the value of the leftmost sub-expression
determines the final value of the expression, the rest
of the expression is not evaluated
23
Using Short-Circuit Evaluation
• Short-circuit evaluation can be used to prevent
run time errors
- Consider this if-statement
24
Multiway Branches
•A branching mechanism selects one out of a
number of alternative actions
- The if-else-statement is a branching mechanism
•Branching mechanisms can be a subpart of
another branching mechanism
- An if-else-statement can include another
if-else-statement as a subpart
25
Nested Statements
• A statement that is a subpart of another statement
is a nested statement
- When writing nested statements it is normal to
indent each level of nesting
- Example: if ( x < y)
cout << x << " is less than " << y;
indented
elsecout << y << " is less than " << x;
26
Nested if-else Statements
• Use care in nesting if-else-statements
• Example: To design an if-else statement to
warn a driver when fuel is low, but tells the
driver to bypass pit stops if the fuel is close
to full. Other wise there should be no output.
27
Braces and Nested Statements
•Braces in nested statements are like
parenthesis in arithmetic expressions
- Braces tell the compiler how to group things
• Use braces around substatements
28
Multi-way if-else-statements
• An if-else-statement is a two-way branch
• Three or four (or more) way branches can be
29
Nested if-else Syntax
• A Multiway if-else statement is written as
- if (Boolean_Expression_1)
Statement_1
else if ( Boolean_Expression_2)
Statement_2
…
else if (Boolean_Expression_n)
Statement _n
else
Statement_For_All_Other_Possibilities
30
Class Exercise
• Write a program for a state that computes tax
according to the rate schedule:
- No tax on first $15,000 of income
- 5% tax on each dollar from $15,001 to $25,000
- 10% tax on each dollar over $25,001 to $50,000
- 15% tax on each dollar over $50,001 to $75,000
- 20% tax on each dollar over $75,001 to $100,000
- 25% tax on each dollar over $100,001
31
The switch-statement
•The switch-statement is an alternative for
constructing multi-way branches
32
switch-statement Syntax
• switch (controlling expression)
{
case Constant_1: statement_Sequence_1
break;
case Constant_2:
Statement_Sequence_2
break;
case Constant_n:
Statement_Sequence_n
break;
default:
Default_Statement_Sequence
}
33
The Controlling Statement
• A switch statement's controlling statement
must return one of these types
- A bool value
- An enum constant
- An integer type
- A character
• The value returned is compared to the
constant values after each "case"
- When a match is found, the code for that case is used
34
The break Statement
• The break statement ends the switch-statement
- Omitting the break statement will cause the code
for the next case to be executed!
- Omitting a break statement allows the use of
multiple case labels for a section of code
• case 'A':
case 'a':
cout << "Excellent.";
break;
35
The default Statement
•If no case label has a constant that matches
the controlling expression, the statements
following the default label are executed
- If there is no default label, nothing happens when
the switch statement is executed
- It is a good idea to include a default section
36
More About
C++ Loop Statements
• A loop is a program construction that repeats a
statement or sequence of statements a number
of times
- The body of the loop is the statement(s) repeated
- Each repetition of the loop is an iteration
• Loop design questions:
- What should the loop body be?
- How many times should the body be iterated?
37
Designing Loops
38