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 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 are the init-statement, expression, post-statement in a for? Can a for replace a while/do- while? is the reverse possible? What is a loop variable? Where is it declared in a for? What is its scope? how is break used inside a looping construct? what is continue, how is it used? what is a nested iterative construct? what is inner/outer loop? How many loops can be nested in C++?

2 Predefined Functions 2

3 Predefined Functions C++ comes with libraries of code that can be used 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 the executable? function name - identifier distinguishing the function from others; example the square root function is called: sqrt argument - the value the function starts out with; a 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 the_root = sqrt(9.0); function call (function invocation) - expression consisting of function name followed by arguments in parentheses the function accepts arguments of a certain type and returns a value of certain type; sqrt accepts and returns double to use a function you need to specify an include directive: #include <cmath> argument may be arbitrary expression including return value of a function the_root = sqrt(myVar + 9.0); the_root = sqrt(abs(myVar));

4 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: double c=double(a)/b; explicit type conversion is called type casting warning: wrong application of casting: double c=double(a/b); note that type conversion may be implicit: example: int a=55; double c = a + 30; The integer expression is implicitly converted to double before assignment

5 Random Number Generation
(pseudo) random number generators are pre-defined functions that are used in programs to create events unpredictable by the user (e.g. events in games) need to include <cstdlib> creates a series of numbers, the numbers within the series are (pseudo) random srand(SEED) – initializes the 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 between 0 and RAND_MAX – a named constant predefined in <cstdlib> 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;

6 Selecting Random Series with time()
time() – returns number of seconds since 01/01/1970, good for initializing unique series, needs <ctime> selecting series srand(1); - repeats series every time you run your program – good for debugging srand(time()); - 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