Decisions and Loops - Introduction To Pre-Defined Functions
Decisions and Loops - Introduction To Pre-Defined Functions
1
Learning Outcomes
Be able to use do-while loops in
simple programs and appreciate the
difference between while and do-
while loop
Understand short-circuit evaluation
Be able to write multi-way if
condition and switch statement
Start understanding pre-defined
functions in C++
2
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);
3
4
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
Subtracts 1 from the value of a variable
x --;
is equivalent to x = x 1;
5
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)
6
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
7
Using Short-Circuit
Evaluation
Short-circuit evaluation can be used to prevent
run time errors
Consider this if-statement
8
Loops
In a particle accelerator, subatomic particles traverse a loop-shaped
tunnel multiple times, gaining the speed required for physical
experiments.
The Large
Hadron Collider
Geneva,
Switzerland
9
Loops (contd)
10
11
For Loop
for ( init; condition; increment
)
{
statement(s);
}
#include <iostream>
using namespace std;
int main ()
{ // for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
cout << "value of a: " << a <<
endl;
}
return 0;
}
12
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
13
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)
indented cout << x << " is less than "
<< y;
else
cout << y << " is less than "
<< x;
14
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.
15
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
16
Multi-way if-else-statements
An if-else-statement is a two-way
branch
Three or four (or more) way branches
can be
designed using nested if-else-
statements
Example: The number guessing game
with the number stored in
variable number, the guess in
variable
guess. How do we give hints? 17
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
18
The switch-statement
The switch-statement is an
alternative for
constructing multi-way branches
19
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
}
20
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
21
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;
22
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
23
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?
24
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
25
Designing Loops
26
PRE-DEFINED FUNCTIONS
27
Predefined Functions
C++ comes with libraries of
predefined
functions
29
Function Call Syntax
Function_name (Argument_List)
Argument_List is a comma separated list:
(Argument_1, Argument_2, ,
Argument_Last)
Example:
side = sqrt(area);
cout << 2.5 to the power 3.0 is
<< pow(2.5, 3.0);
30
Function Libraries
Predefined functions are found in libraries
The library must be included in a program
to make the functions available
An include directive tells the compiler which
library header file to include.
To include the math library containing sqrt():
#include <cmath>
Newer standard libraries, such as cmath, also
require
the directive
using namespace std;
31
Other Predefined Functions
abs(x) --- int value = abs(-8);
Returns absolute value of argument x
Return value is of type int
Argument is of type x
Found in the library cstdlib
fabs(x) --- double value = fabs(-8.0);
Returns the absolute value of argument x
Return value is of type double
Argument is of type double
Found in the library cmath
32
33