Download presentation
Presentation is loading. Please wait.
1
announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s lab 3 that you got from your roommate.
2
Functional Paragraphs another use for { }
3
operation ( condition ) // parentheis { action, based on condition } // curly brackets ----------------------------------------------------------- // example int x = 0; while ( x < 100 ) { cout << x << endl; x = x + 1; }
4
Iteration
5
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
while…. 3 parts int x = 0; // 1. initialize a counting variable while (x < 10) // 2. conditions to keep looping { x = x + 2; // 3. how much to increment }
8
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 (…..
12
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
13
"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
14
really? different? aspects of C++ SHOULDN'T be computer- dependent solution: long long factorial = 0;
15
Reading Chapter 2 Sections 2.2, 2.3 Pages 56-83
16
Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables
17
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
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 TRUNCATED! r = (double) x; // r will be 14.0000
24
The while loop while ( something ) // performs test, and { // if test evaluates to true do something // performs this action } // goes back to the top
25
the “do-while” loop do { // repeated code here } while ( time > 0);
26
what's the difference while tests first, then performs do...while performs, then tests do...while will always execute at least once
27
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
28
Try running this…. then this
29
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 { }
30
int Counter = 0; for (Counter = 4; Counter < 10; Counter = Counter + 2) { } Counter is AUTOMATICALLY 4, 6, 8
31
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
32
Flow of Control within a program int main ( ) { int x = 1; for (x = 0; x<5; x=x+1) { } loopx = 0, 1, 2, 3, 4 1 2 3
33
shorthand 1 x++ means x = x+1
34
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)
35
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
36
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.).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.