1 cis 230 18-jan-06. 2 overview evolution of c++ programming style syntax & semantics comments...
Post on 22-Dec-2015
213 views
TRANSCRIPT
2
Overview
• Evolution of C++
• Programming Style
• Syntax & Semantics
• Comments & White Space
• Data Types
• Variables & Declarations
• cout
• Operators
– Arithmetic
– Relational
– Logical
• cin
• Constants
3
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
6
Comments & White Space
• Comments
// One line comment
/* Starts comment
Block comments – may be several lines long
*/ Ends comment
• White Space
– Space, tab, <CR>
• Statement
7
Data Types
• Integers
ex. 15 -256 +43217
• Floating Point Numbersex. 6.5 -13.246 43 6. 0.97
• Characters
ex. ‘a’ ‘4’ ‘&’ ‘A’
• Booleans
12
cout, continued
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
13
Operators
• Arithmetic Operators
* / % + -
• Increment & Decrement
++ --
• Relational & Logical Operators
> >= < <= != ==
&& ||
16
Examples
• a1 = 7 + 3 * 6 / 2 -1;
• a2 = 2 % 2 + 2 * 2 -2 / 2;
• a3 = ( 3 * 9 * ( 3 + (9 * 3 / 3 ) ) );
18
Increment & Decrement
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
19
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
20
Logical Operators
• Goal:
Create more complex conditions
• Relational Expression:
operand operator operand
• Operators:
&& and
|| or
! not
21
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
22
Relational & Logical Operators
• Goal:
Create more complex comparisons
• Examples:
( age > 40 ) && ( height > 70 )
( age > 40 ) || ( height > 70 ) || ( weight > 150 )
! ( ( age > 40 ) && ( height > 70 ) )
25
cin
• #include <iostream>
• using namespace std;
• >>
• Examples:
cin >> x;
cin >> x >> y >> z; Input: 13 26 4
x y z
13 26 4