400 likes | 530 Views
Functional Paragraphs. another use for { }. announcements. The first graded lab will be posted Sunday 9/21 and due Saturday 10/4 at midnight It is brand new, so please don’t hand in last semester’s lab 3. operation ( condition ) { action, based on condition }
E N D
Functional Paragraphs another use for { }
announcements • The first graded lab will be posted Sunday 9/21 and due Saturday 10/4 at midnight • It is brand new, so please don’t hand in last semester’s lab 3.
operation ( condition ) { action, based on condition } ----------------------------------------------------------- // example int x = 0; while ( x < 100 ) { cout << x << endl; x = x + 1; }
Iteration • Repeatedly execute the same, bracketed section of C++ code • Execute until certain conditions are met • while loops (two types) - loop until a condition is met • “for” loops - loop a defined number of times
loop while a condition is “true” while ( x < 6 ) { ..... bool input = false; while ( input == false ) // loops until input is changed { ..... while ( true ) // loops forever { .....
remember While…. int x = 0; // initialize a counting variable while (x < 10) // conditions to keep looping { x = x + 2; // how much to increment }
while loop 1. variable and initial value int LoopCounter = 0; while (LoopCounter < 6) { cout << LoopCounter; // work done here LoopCounter = LoopCounter + 1; } 0 1 2 3 4 5 2. test of conditions for which loop continues 3. increment or decrement
while loop 1. variable and initial value int LoopCounter = 10; while (LoopCounter > 6) { cout << LoopCounter; LoopCounter = LoopCounter - 1; } 10 9 8 7 2. test of conditions for which loop continues 3. increment or decrement
write a program… • calculate (and display) the factorial of a number factorial of 6 : 6 * 5 * 4 * 3 * 2 * 1 • get the number from the user • ask the user to continue or quit will require the use of if (….. and while (…..
size of numbers The range of integer values that can be stored is -2,147,483,648 through 2,147,483,647 1,932,053,504
"long" ints long factorial = 0; • old computers (32 bit): -2,147,483,648 through 2,147,483,647 • new computers (64 bit): -9,223,372,036,854,775,808 thru 9,223,372,036,854,775,807
really? different? • aspects of C++ SHOULDN'T be computer-dependent • solution: long long factorial = 0;
Reading • Chapter 2 • Sections 2.2, 2.3 • Pages 56-83
Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables
Computers DO NOT completely check our programs • Called “Compilation” (i.e. Compile & Link) • Must be successful before a program can “run” • But not all errors are caught in compilation
Compilation will it work?
So, Illegality is... • “Compiler” Errors – however, some languages and compilers allow mis-designations of variables • Indeterminate results • “Run-time” Errors - only show up when you run the program
Illegal #1 – mis-defined variables int y; y = 14.0001; char q; q = "abcdef"; double z; z = "hello"; bool operatorFault; operatorFault = “true”; // !!??!!
Why “Type” Variables • Deterministic behavior of variables and the numbers they represent. • New definition of “Legality” : We will never perform mathematical or logical operations on variables of different types.
Illegal #2 – mixing variables in same equation int x; double y; char z; x = 12; y = 16.001; z = x + y;
Illegal #3 - Operation wrong for variable type: char Q, R, S; Q = R / S; // remember "/" is divide
to help - casting • converting INTS to DOUBLES, and DOUBLES to INTS - for the intentional translation of a counting to/from a measuring number int x = 14; double r = 3.14159; x = (int) r; // x will be 3 … or …. r = (double) x; // r will be 14.0000
build a Fahrenheit to Celsius converter C = (F - 32) * (5/9) F = ( C * (9/5) ) + 32 note: any division should be performed with doubles C = (F – 32.0) * (5.0/9.0) F = ( C * (9.0/5.0) ) + 32.0
cout - decimal precision • Internally 15 decimal places, due to memory limitations • What cout DISPLAYS is different • use "cout.precision( n ); " to set displayable values to n characters (plus dec pt.).
Scientific notation Try this: double x;x = 35.62e6; cout.scientific;cout << x << endl; cout.fixed;//eliminates scientific notationcout.precision(12);cout << x << endl;
The while loop while ( something ) // performs test, and { // if test evaluates to true do something // performs this action } // goes back to the top
the “do-while” loop do { // repeated code here } while ( time > 0);
what's the difference • while tests first, then performs • do...while performs, then tests • do...while will always execute at least once
loops, in general • you need a looping int variable, like x • you need to know the initial value e.g. int x = 1; • you need to know the condition under which to keep looping e.g. while (x<10) • you need to increment or decrement that variable e.g. x = x+1; or x = x-4; • that variable's behavior depends on where you use it, in relation to where you change it
for loop for (x = start; condition for x valid; increment ) { // repeated code here } for loops use an “auto-increment” INTEGER variable for ( x = 0; x < = 100; x=x+1 ) // x goes from 0 to 100 { }
int Counter = 0; for (Counter = 4; Counter < 10; Counter = Counter + 2) { } Counter is AUTOMATICALLY 4, 6, 8
for loop int x=0; int total = 0; for (x = 0; x < 10; x = x+1) { total = total + x; } cout << “x value: ” << x; cout << “total value: ” << total; on screen… x value: 9 total value: 45
Flow of Control within a program int main ( ) { int x = 1; for (x = 0; x<5; x=x+1) { } } 1 2 loop x = 0, 1, 2, 3, 4 3
shorthand 1 • x++ means x = x+1
leaving a loop continue; // re-loops break; // program falls out of immediate // brackets // note: continue continues, break breaks (the // loop) return 0; // exits main (i.e. the PROGRAM)