1 / 40

Functional Paragraphs

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 }

lavey
Download Presentation

Functional Paragraphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional Paragraphs another use for { }

  2. 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.

  3. operation ( condition ) { action, based on condition } ----------------------------------------------------------- // example int x = 0; while ( x < 100 ) { cout << x << endl; x = x + 1; }

  4. Iteration

  5. 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

  6. loop while a condition is “true” while ( x < 6 ) { ..... bool input = false; while ( input == false ) // loops until input is changed { ..... while ( true ) // loops forever { .....

  7. remember While…. int x = 0; // initialize a counting variable while (x < 10) // conditions to keep looping { x = x + 2; // how much to increment }

  8. 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

  9. 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

  10. 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 (…..

  11. 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

  12. "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

  13. really? different? • aspects of C++ SHOULDN'T be computer-dependent • solution: long long factorial = 0;

  14. Reading • Chapter 2 • Sections 2.2, 2.3 • Pages 56-83

  15. Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables

  16. 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

  17. Compilation will it work?

  18. 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

  19. Illegal #1 – mis-defined variables int y; y = 14.0001; char q; q = "abcdef"; double z; z = "hello"; bool operatorFault; operatorFault = “true”; // !!??!!

  20. 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.

  21. Illegal #2 – mixing variables in same equation int x; double y; char z; x = 12; y = 16.001; z = x + y;

  22. Illegal #3 - Operation wrong for variable type: char Q, R, S; Q = R / S; // remember "/" is divide

  23. 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

  24. 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

  25. 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.).

  26. Scientific notation Try this: double x;x = 35.62e6; cout.scientific;cout << x << endl; cout.fixed;//eliminates scientific notationcout.precision(12);cout << x << endl;

  27. The while loop while ( something ) // performs test, and { // if test evaluates to true do something // performs this action } // goes back to the top

  28. the “do-while” loop do { // repeated code here } while ( time > 0);

  29. what's the difference • while tests first, then performs • do...while performs, then tests • do...while will always execute at least once

  30. 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

  31. Try running this…. then this

  32. 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 { }

  33. int Counter = 0; for (Counter = 4; Counter < 10; Counter = Counter + 2) { } Counter is AUTOMATICALLY 4, 6, 8

  34. 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

  35. 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

  36. shorthand 1 • x++ means x = x+1

  37. 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)

More Related