1 cis 230 18-jan-06. 2 overview evolution of c++ programming style syntax & semantics comments...

29
1 CIS 230 18-Jan-06

Post on 22-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

1

CIS 230

18-Jan-06

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

4

Programming Style

• Names

• Comments

• Modularity

5

Syntax & Semantics

• Syntax

Ex. “Sue kicked the ball”

• Semantics

+ - =

[ ] ( )

< >

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

8

Variables & Declarations

• Variable Names

• Declaration

• Assignment

9

cout

• #include <iostream>

• using namespace std;

• <<

• “string”

• \n

10

cout, continued

Cout << “Here is\na sample\noutput;”

Result:

Here is

a sample

output

11

cout, continued

• cout << “The answer is” << result;

• cout << x << y << z;

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

> >= < <= != ==

&& ||

14

Arithmetic Operators

* / % + -

(Left to Right Precedence)

15

Integer Division

9 / 2 = 4; 9 % 2 = 1;

17 / 5 = 3; 17 % 3 = 2;

15 / 2 = 7; 14 % 2 = 0;

16

Examples

• a1 = 7 + 3 * 6 / 2 -1;

• a2 = 2 % 2 + 2 * 2 -2 / 2;

• a3 = ( 3 * 9 * ( 3 + (9 * 3 / 3 ) ) );

17

Increment & Decrement

++ --

• Prefix

• Postfix

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

23

Order of Operations

( )

++ --

* / %

+ -

< > <= >=

== !=

&&

||

=

24

Examples

x < y – 3

a = b != c

(a = b) != c

while (x < 7 && y > 5)

25

cin

• #include <iostream>

• using namespace std;

• >>

• Examples:

cin >> x;

cin >> x >> y >> z; Input: 13 26 4

x y z

13 26 4

26

Cin, continued

char ch1, ch2, ch3;

cin >> ch1 << ch2 << ch3; Input: a bc

ch1 ch2 ch3

a b c

27

cin, continued

int a;

float x, y;

cin >> x >> a >> y; Input: 17 23.59 4.6

x a y

17. 23 .59

28

Constants

• Cannot change

• UPPERCASE

• Type

Form: const type NAME = value;

29

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;