Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iterative Constructs Review

Similar presentations


Presentation on theme: "Iterative Constructs Review"— Presentation transcript:

1 Iterative Constructs Review
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 is iterate-and-keep-track idiom? what is tracking variable? what’s a nested iterative construct? what is inner/outer loop? How many loops can be nested in C++?

2 Predefined Functions library code

3 Name, Return Value, Argument
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 return value - value computed by the function argument – the value function starts out with; function accepts arguments of certain type and returns the value of certain type sqrt accepts and returns double to use a function, need to specify particular include directive: to use sqrt #include <cmath>

4 Invocation, Argument Variants
function call (function invocation) – expression consisting of function name followed by arguments in parentheses result = sqrt(9.0); semantics: argument is evaluated, function is executed, return value replaces function invocation invocation in expression – return value replaces invocation in expression evaluation result = sqrt(9.0) – myVar*5; standalone invocation – return value is ignored srand(55); argument variants function may have more than one argument result = pow(myVar, 55); an argument is an (arbitrary) expression result = sqrt(myVar* ); nested function call: use of one function call as argument to another result = sqrt(abs(myVar));

5 Type Changing Functions
is there a problem with this code? int a=9, b=2; double c=a/b; casting – explicit type conversion static_cast<type> converts to type example: double c=static_cast<double>(a)/b; wrong application of casting: double c=static_cast<double>(a/b); coercion – implicit type conversion int a=55; double c = a + 30; integer expression coerced to double before assignment

6 Random Number Generation
(pseudo) random number generation pre-defined functions are used to create events unpredictable by user (e.g. events in games) need to include <cstdlib> generates a series of numbers, numbers within single series are (pseudo) random srand(seed) – initializes random number generator, needs to be invoked once in the program, no return value seed – integer, selects the (pseudo) random series rand() – returns a new integer random number in the series, can be used multiple times in program, no argument the number is from 0 to MAX_RAND – a named constant declared 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;

7 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 time()


Download ppt "Iterative Constructs Review"

Similar presentations


Ads by Google