Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 2
Programming Concepts
M.E . (CSN & IT)
1. int
2. float
3. char
-2002
+15
+0000000000000015 = 15
Float
23.4
3.456
-7.890
EXAMPLE:
1,234,567,890
CAN BE REPRESENTED AS
3
1234568x10
char
SYNTAX / SEMANTICS
(RESERVED WORDS).
NAMING RULES
total
Score1
X
ScoreOne
score_one
• Score One
• 1score
VARIABLE DECLARATIONS
type variable_name_list;
CONSTANT VALUES
characters (strings).
NAMED CONSTANT
CONSTANT DEFINITIONS
EXPRESSIONS
operators.
operand
OR
(num + 2) / 3.56
TAX_RATE
test
income * TAX_RATE
+ ADDITION
- SUBTRACTION
* MULTIPLICATION
/ DIVISION
% DIVISION (MODULUS)
(REMAINDER FROM INTEGER DIVISION)
EXAMPLES:
MATH
C++
SUM + 5.6 sum + 5.6
EXAMPLES:
EXPRESSION VALUE
13 + 16 29
27 - 12 15
8 * 15 120
24.0 / 5.0 4.8
9 / 8 1
7 / 8
0
9 % 9
0
7 % 8
9 % 8
7
0 % 6 1
0
PRECEDENCE
EXAMPLES:
10 / 2 * 4
20
15.0
1.5
4.5
5 + 6 = 11
8 / 5 = 1
9 / 5 = 1
10 / 5 = 2
RESULTS:
8 % 5 = 3
9 % 5 = 4
10 % 5 = 0
PARENTHESES
PERFORMED FIRST.
5.0 * 2 / ( 4 * 2 )
10.0
1.25
5 + 2 / ( 4 * 2.0 )
8.0
0.25
5.25
( 5 + 3 * ( 2 + 4 / 2 ) + 7 / 2 ) / 3
12
3
17
20
6
( 5 * ( 11 - 5 ) * 2 ) * 4 + 9
( 5 * 6 * 2 ) * 4 + 9
60 * 4 + 9
240 + 9
249
2003 Prentice Hall, Inc. All rights reserved.
31
+ X
- ( A + B )
N * ( - 2 )
THE ASSIGNMENT
STATEMENT
variable_name = expression;
ANOTHER EXAMPLE:
.
.
total = score1 + score2;
counter = counter + 1;
.
.
total 185.0 87 + 98
average ?
score1 87
score2 98
counter 1 0+1
sum = num;
AND
num = sum;
Relational Operators
USING COMMENTS
// comment
OR
/* comment */
COMPILER DIRECTIVES
#include <filename>
#include <iostream>
// FILE: FindAvg1.cpp
// Finds the average of three test scores
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void main ()
{
// local data:
const int NUMBER_OF_TESTS = 3;
float test1, test2, test3, average;
// Get the grade marks
cout << "Enter the three test marks: ";
cin >> test1 >> test2 >> test3;
average = (test1 + test2 + test3) /
NUMBER_OF_TESTS;
// Display the average
cout << "The average of the 3 test marks is "
<< average << endl;
return;
}