Decisions and Loops - Introduction To Pre-Defined Functions
Decisions and Loops - Introduction To Pre-Defined Functions
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);
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;
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;
}
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
Using Short-Circuit
Evaluation
Short-circuit evaluation can be used to prevent
run time errors
Consider this if-statement
if ((kids != 0) && (pieces / kids >= 2) )
cout << "Each child may have two pieces!";
If the value of kids is zero, short-circuit
evaluation
prevents evaluation of (pieces / 0 >= 2)
Division by zero causes a run-time error
Loops (contd)
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;
}
10
Class Exercise
Write multiplication tables for
integers 1-10 for numbers 1-10?
12
Multiway Branches
A branching mechanism selects one
out of a number of alternative actions
The if-else-statement is a branching
mechanism
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:
indented
if ( x < y)
cout << x << " is less than "
<< y;
else
cout << y << " is less than "
<< x;
14
15
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-elsestatements
Example: The number guessing game
with
the number stored in
variable
number, the guess in
variable
guess. How do we give hints? 17
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
20
21
22
Class Exercise
Write a program for a state that computes tax
according to the rate schedule:
24
Designing Loops
Designing a loop involves designing
The body of the loop
The initializing statements
The conditions for ending the loop
25
PRE-DEFINED FUNCTIONS
26
Predefined Functions
C++ comes with libraries of
predefined
functions
Example: sqrt function
the_root = sqrt(9.0);
returns, or computes, the square root
of a number
The number, 9, is called the argument
the_root will contain 3.0
27
Function Calls
sqrt(9.0) is a function call
It invokes, or sets in action, the sqrt function
The argument (9), can also be a variable or an
expression
A function call can be used like any expression
bonus = sqrt(sales) / 10;
cout << The side of a square with area <<
area
<< is
<< sqrt(area);
28
(Argument_1, Argument_2, ,
Argument_Last)
Example:
side = sqrt(area);
cout << 2.5 to the power 3.0 is
<< pow(2.5, 3.0);
29
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;
30
32