6
Iterative Constructs Review What are named constants? Why are they needed? What is a block? What is special about declaring a variable inside a block? What is the difference between while and do-while? Can one replace the other? what does for do? Why is it needed? what is init-statement, expression, post-statement in for? Can for replace while/do- while? is reverse possible? What is loop variable? Where is it declared in for? What is its scope? how is break used inside an iterative construct? what is continue? how is it used? what’s is iterate-and-keep-track idiom? what’s a nested iterative construct? what is inner/outer loop? How many loops can be nested in C++? 1

Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Embed Size (px)

Citation preview

Page 1: Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Iterative Constructs Review What are named constants? Why are they needed? What is a block? What is special about declaring a variable inside a block? What is the difference between while and do-while? Can one replace

the other? what does for do? Why is it needed? what is init-statement, expression,

post-statement in for? Can for replace while/do-while? is reverse possible?

What is loop variable? Where is it declared in for? What is its scope? how is break used inside an iterative construct? what is continue? how is it used? what’s is iterate-and-keep-track idiom? what’s a nested iterative construct? what is inner/outer loop? How many

loops can be nested in C++?

1

Page 2: Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Predefined Functions

Page 3: Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in

the form of pre-defined functions what is the program that adds pre-defined functions to your code to produce executable?

function name - identifier distinguishing the function from others; example square root function: sqrt

argument - the value function starts out with; function may have more than one argument; an argument is an expression (thus, can be just a simple constant or variable);

return value - value computed by the function

result = sqrt(9.0); function call (function invocation) - expression consisting of function name followed by

arguments in parentheses function accepts arguments of certain type and returns value of certain type; sqrt accepts

and returns double to use a function need to specify include directive:

#include <cmath> argument may be arbitrary expression

result = sqrt(myVar + 9.0); nested function call function call is used as an argument for another function

result = sqrt(abs(myVar));

3

Page 4: Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Type Changing Functions is there a problem with this code?

int a=9, b=2;

double c=a/b; C++ provides functions for explicit conversions between types: function double converts to type double:

int a=9, b=2;

double c=double(a)/b; casting - explicit type conversion is called type warning: wrong application of casting:

int a=9, b=2;

double c=double(a/b); note that type conversion may be implicit:

example:

int a=55;

double c = a + 30;

integer expression is implicitly converted to double before assignment

4

Page 5: Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Random Number Generation (pseudo) random number generation pre-defined functions are used in

programs to create events unpredictable by user (e.g. events in games) need to include <cstdlib> creates a series of numbers, numbers within series are (pseudo)

random srand(seed) – initializes random number generator, needs to be

invoked once in the program, no return value seed – selects the (pseudo) random series

rand() – returns a new integer random number in the series, can be used multiple times in program the number is from 0 to MAX_RAND – a named constant predefined

in <cstdlib> ranged random idiom: to get a random number in a specific range,

take a remainder of that range. Example, random number between 0 and 9 can be obtained as:

int myRandValue = rand() % 10;

5

Page 6: Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?

Selecting Random Series with time() time(nullptr) – returns number of seconds since 01/01/1970,

good for initializing unique series, needs <ctime> nullptr is there for historical reasons

selecting series srand(1); - repeats series every time you run your program –

good for debugging srand(time(nullptr)); - selects unpredictable series every

time you run your program – good for final compilation

srand() rand() invocation

seed 1 2 3 4 5 6

1 41 18467 6334 26500 19169 15724

1 41 18467 6334 26500 19169 15724

2 45 29216 24198 17795 29484 19650

2 45 29216 24198 17795 29484 19650 time() 14987 9212 26926 31604 11201 32623

6