[go: up one dir, main page]

0% found this document useful (0 votes)
11 views16 pages

chapter_6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

chapter_6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Map of the course

1. History (of the Programming language C++)


2. Literature
3. A first example
4. Some remarks on the compilation and binding of programs
5. The programming environment Dev-C++
6. Expressions in C++
7. Formatted input and output
8. Control structures
9. Functions
10. Arrays
11. Using files
6. Expressions in C++
• A C++-program consists of a sequence of expressions.
• Each expression ends wtih a semicolon (;).
• Expressions can be grouped together by blocks.
{ open a block
} close a block
• Blocks can be nested.

6.1 Structure of a C++-(main-)function


in t m a in ( in t a r g c , in t * a r g v [ ] )
{ // o p e n b lo c k o f f u n c tio n b o d y
// d e f in itio n o f c o n s ta n ts a n d v a r ia b le s
// a ll e x e c u ta b le s ta te m e n ts
// ( in p u t, a s s ig n m e n t, o u tp u t, e tc .)
} // c lo s e b lo c k
6.2 Some elements of a C++-statement

• C++ - keywords: do, while, if, switch, case, for


• Constants 12, ”Breite”
• Symbolic names: ix, breite
• Operators: *, +, -, /, ., ->, >>, <<, =
• Standard functions: scanf, printf, sin, cos
• Basic data types: int, double, char

• Comment line: //
Can be used at any position in the program.
All following characters of this line are regarded as a
comment and not compiled.

• Comment block: /* Start comment


*/ End comment
6.3 Variables and constants
A variable is like a glass of marmalade … it stores something

Volume
Content (Size/Type)
(Value)

Label
(Name)

Taken from http://thefigandthistle.wordpress.com


6.3 Variables and constants

Definition:

Variables and Constants …


… are uniquely identified by a symbolic name.
… must have a type.
… store a value.

Variables can change their value during the execution of a program.

Constants cannot change their value during execution.


Rules and conventions for symbolic names:

valid • letters a-z, A-Z, no special letters (ä,ö,ü,ß); capital and small letters allowed
characters: • digits 0-9
• underscore (‘_’)
• a distinction is made between capital and small letters
(C/C++ is case sensitive!!)

mandatory • symbolic names must not contain blanks


rule: • symbolic names must not begin with a digit

rule of • A symbolic name should only consist of small letters or a mixture of


convention: capital and small letters.

Examples:
allowed: _n, x1, x_1, Flaeche
not allowed: 1x, Fläche, x 1
not recommended: F, FLAECHE
Data Types
There are various types of glasses, each storing different types of content

The same is true for variables …


Taken from alpack.ie
Numbers
Examples
Integers: • 0, 1200, +12, -4 (without decimal point)

‘Real’ Numbers: • floating point notation: 1.0, 0.082, 7., -2.83, +0.
• exponential notation: 1.E3, 7.082E-4
(in general: significant * 10^exponent)
Usage

Integer: int symbolic_name;


type symbolic_name;
´Real´ number: double symbolic_name;

in t i; // d e c la r a tio n o f a n in te g e r i
d o u b le d ; // d e c la r a tio n o f a r e a l n u m b e r d

Example: in t i; i is a variable of type int. It is assigned to the


i = 12;
value 12. 12 is a constant
Characters and character constants
ASCII-table (character table)
(American Standard Code for Information Interchange)
special characters alpha-numerical characters
and control characters (A-Z, a-z, 0-9) Example: 1_a
DEC Char DEC Char DEC Char DEC Char DEC Char DEC Char DEC Char DEC Char
0 nul 16 dle 32 sp 48 0 64 @ 80 P 96 ` 112 p
1 soh 17 dc1 33 ! 49 1 65 A 81 Q 97 a 113 q
2 stx 18 dc2 34 " 50 2 66 B 82 R 98 b 114 r
3 etx 19 dc3 35 # 51 3 67 C 83 S 99 c 115 s
4 eot 20 dc4 36 $ 52 4 68 D 84 T 100 d 116 t
5 enq 21 nak 37 % 53 5 69 E 85 U 101 e 117 u
6 ack 22 syn 38 & 54 6 70 F 86 V 102 f 118 v
7 bel 23 etb 39 ' 55 7 71 G 87 W 103 g 119 w
8 bs 24 can 40 ( 56 8 72 H 88 X 104 h 120 x
9 ht 25 em 41 ) 57 9 73 I 89 Y 105 i 121 y
10 nl 26 sub 42 * 58 : 74 J 90 Z 106 j 122 z
11 vt 27 esc 43 + 59 ; 75 K 91 [ 107 k 123 {
12 np 28 fs 44 , 60 < 76 L 92 \ 108 l 124 |
13 cr 29 gs 45 - 61 = 77 M 93 ] 109 m 125 }
14 so 30 rs 46 . 62 > 78 N 94 ^ 110 n 126 ~
15 si 31 us 47 / 63 ? 79 O 95 _ 111 o 127 del
Characters and character constants
Assignment of a single character: type char

char s;
s = ‘c ’;

The expression ‘c’ is a character constant.


Character constants always have to consist of a single character.

Assignment of character strings (see Section 10.4)


Every string is terminated internally by the zero character ‘\0‘

Example for a character strings: ”Bauinformatik”, ””

char s;
s = ‘c ’; // o .k .
s = “c “; // w r o n g , a s th is s tr in g c o n s is ts o f th e tw o c h a r a c te r s ‘c ’ u n d ‘ \0 ’
6.4 Arithmetic expressions, operators
An arithmetic expression is composed of: constants, variables and operators

Arithmetic operators are: +, -, *, /

more operators: % (modulo): modulus of an


integer division

Example: 2_b

priority rule: 1. brackets


2. multiplication, division
3. addition, subtraction

Attention:
It is very important to note that arithmetic expressions may perform a type casting
(or type conversion).
Type conversion

Data type of the result of an arithmetic expression:


1. operand 2. operand result

int int int

int double double

double double double

........ ........ ........

Example: 2_a, 2_c


Assignment, abbreviated notations
<symbolic_name> = <arithmetic expression>
Note: An assignment may result in another type casting.

symbol meaning examples

= Simple assignment i = 6;
i = j;

+= Value of variable is increased by right-hand side j += 5;  j = j + 5;

-=, *=, /= Analogous to addition

++ Value of variable is increased by 1 i++;  i = i + 1;


 i += 1;
-- Value of variable is decreased by 1

Examples: 2_d
Logical expressions: Relation
The relation between two arithmetic expressions aa1 und aa2
aa1 relational operator aa2
yields a condition (also: logical expression)

C++-statement meaning math. expression

< less than <

<= less than or equal 

== equal to =

>= greater than or equal 

> greater than >

!= not equal <>

The result of a conditional statement is an integer value: 0 means false, otherwise true
Example: 3_b
More logical expressions

Logical operators:

C++-expression meaning

! not

&& and

|| or (inclusively)
(on keyboard: “Alt Gr” + “<”)

The result of a logical operation is, as in a condition, an integer value.

0 means that the logical value is false, all other values correspond to the
logical value true.
logical and (&&):
operand 1 operand 2 result
1 1 1
1 0 0
0 1 0
0 0 0

logical or (||):
operand 1 operand 2 result
1 1 1
1 0 1
0 1 1
0 0 0

Logical not (!) inverts the value of a logical expression.


Example: 3_c

You might also like