290 likes | 449 Views
CIS 230. 18-Jan-06. Evolution of C++ Programming Style Syntax & Semantics Comments & White Space Data Types Variables & Declarations. cout Operators Arithmetic Relational Logical cin Constants. Overview. Evolution of C++. BCLP (Basic Cambridge Programming Language)
E N D
CIS 230 18-Jan-06
Evolution of C++ Programming Style Syntax & Semantics Comments & White Space Data Types Variables & Declarations cout Operators Arithmetic Relational Logical cin Constants Overview
Evolution of C++ • BCLP (Basic Cambridge Programming Language) • Early 1960s; Martin Richards • B • 1970; Ken Thompson • C • Early 1970s; Dennis Ritchie • C++ • 1979; Bjarne Stroustrup
Programming Style • Names • Comments • Modularity
Syntax & Semantics • Syntax Ex. “Sue kicked the ball” • Semantics + - = [ ] ( ) < >
Comments & White Space • Comments // One line comment /* Starts comment Block comments – may be several lines long */ Ends comment • White Space • Space, tab, <CR> • Statement
Data Types • Integers ex. 15 -256 +43217 • Floating Point Numbers ex. 6.5 -13.246 43 6. 0.97 • Characters ex. ‘a’ ‘4’ ‘&’ ‘A’ • Booleans
Variables & Declarations • Variable Names • Declaration • Assignment
cout • #include <iostream> • using namespace std; • << • “string” • \n
cout, continued Cout << “Here is\na sample\noutput;” Result: Here is a sample output
cout, continued • cout << “The answer is” << result; • cout << x << y << z;
a = 23; b = 34; x = 3.945; cout << a << ‘\n’ << b << ‘\n’ << x << ‘\n’; cout << a << b << x; Results: 23 34 3.945 23343.945 cout, continued
Operators • Arithmetic Operators * / % + - • Increment & Decrement ++ -- • Relational & Logical Operators > >= < <= != == && ||
Arithmetic Operators * / % + - (Left to Right Precedence)
Integer Division 9 / 2 = 4; 9 % 2 = 1; 17 / 5 = 3; 17 % 3 = 2; 15 / 2 = 7; 14 % 2 = 0;
Examples • a1 = 7 + 3 * 6 / 2 -1; • a2 = 2 % 2 + 2 * 2 -2 / 2; • a3 = ( 3 * 9 * ( 3 + (9 * 3 / 3 ) ) );
Increment & Decrement ++ -- • Prefix • Postfix
a = 7; b = ++a; b = 8 AND a = 8 a = 7; b = a++; b = 7 and a = 8 a = 7; a = 7; --a; a--; a = 6 Increment & Decrement
Relational Operators • Goal: Compare conditions • Relational Expression: operand operator operand • Operators: < less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to
Logical Operators • Goal: Create more complex conditions • Relational Expression: operand operator operand • Operators: && and || or ! not
Evaluating Logical Operators Value: 1 0 Interpreted As: True False Example: A B A && B A || B 0 0 0 1 1 0 1 1
Relational & Logical Operators • Goal: Create more complex comparisons • Examples: ( age > 40 ) && ( height > 70 ) ( age > 40 ) || ( height > 70 ) || ( weight > 150 ) ! ( ( age > 40 ) && ( height > 70 ) )
Order of Operations ( ) ++ -- * / % + - < > <= >= == != && || =
Examples x < y – 3 a = b != c (a = b) != c while (x < 7 && y > 5)
cin • #include <iostream> • using namespace std; • >> • Examples: cin >> x; cin >> x >> y >> z; Input: 13 26 4 x y z 13 26 4
Cin, continued char ch1, ch2, ch3; cin >> ch1 << ch2 << ch3; Input: a bc ch1 ch2 ch3 a b c
cin, continued int a; float x, y; cin >> x >> a >> y; Input: 17 23.59 4.6 x a y 17. 23 .59
Constants • Cannot change • UPPERCASE • Type Form: const type NAME = value;
Constants, continued const float PI = 3.14159; const int STUDENTS = 31; const char INITIAL = ‘x’; //later in program: answer = PI * radius * radius; totalTests = 3 * STUDENTS;