Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
\b \f \n \r \t \ \ Backspace Form feed New line Carriage return Horizontal tab Double quote Single quote character
#include<iostream> #include<cmath> // needed for the sqrt() function main() { double x, y, z; x=5.0; y=4.0; z=sqrt(x*x + y*y); cout<<Hypotenuse is << z; return 0; }
Operators
+ * / % ++ -Addition Subtraction (also unary minus) Multiplication Division Modulus Increment Decrement
Arithmetic Expression
An expression may contain 2 or more
arithmetic operators
Main issue:
ORDER OF PRECEDENCE
Arithmetic Expression
Examples:
= 11 = 22 or 17? = ?? = ??
Arithmetic Expression
Order of Precedence:
Highest:
++ -*/ %
Unary minus
Lowest:
+ -
All operators have a precedence level. High precedence level operators are evaluated before lower ones. Operators of the same precedence level are evaluated from left to right
Shorthand Assignment
Operation += -= *= Examples of expression num += 5; num -= 5; num *= 5; Description num = num + 5; num = num 5; num = num * 5;
/=
%=
num /= 5;
num %= 5;
num = num / 5;
num = num % 5;
Relational Operators
Operation Description Examples of Expression Value
<
<= > >= == !=
Less than
Less than or equal to Greater than Greater than or equal to Equal to Not equal to
6<9
5 <= 5 2>6 9 >= 5 7 == 5 6 != 5
1 (true)
1 (true) 0 (false) 1 (true) 0 (false) 1 (true)
Mantic/Logical Operators
Symbol && || ! Description AND OR NOT
a
0 0 1 1
b
0 1 0 1
a && b
0 0 0 1
a || b
0 1 1 1
!a
1 1 0 0
!b
1 0 1 0
Lowest
if Statements
if if - else
if else - if
if Statement
or
if (expression) { statement1; statement2; }
if Statement
Example:
int num1, num2, min; cout<<Key-in 2 numbers: ; cin>>num1; cin>>num2; min = num1; if (num1 > num2) min = num2; cout<<Smallest: \n << min;
20 > 15?
? 20 15 ? 15 20 ?
if - else Statement
Syntax:
or
Example:
void main() { int mark; cout<<Mark: ; cin>>mark; if (mark >= 50) cout<<Pass\n; else cout<<Fail\n;
}
Introduction to Computer Programming