Sei sulla pagina 1di 19

Character Escape Sequence

\b \f \n \r \t \ \ Backspace Form feed New line Carriage return Horizontal tab Double quote Single quote character

\\ Backslash \v Vertical tab \a Alert

C++ Data Types


char wchar_t int float double bool void Character Wide character Integer Floating point Double floating point Boolean Valueless

#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:

5+6 5+6*2 2.5 + 6 2 * 2 12 / 6.0 2 * 2

= 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

Increment and Decrement


This operation contains only one operand, that is, the operand which value will be incremented/ decremented
Symbol
++ --

Description Examples of Description Expression


Increment operand by 1 Decrement operand by 1 i++ i-i=i+1 i=i1

Introduction to Computer Programming

Relative Precedence of the Relational and Logical Operators


Highest ! >, >=, <, <= ==, != && ||

Lowest

if Statements

if if - else

if else - if

if Statement

Syntax: if (expression) statement; Dont forget the brackets !!

or
if (expression) { statement1; statement2; }

Dont forget the curly brackets !!

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?

num1 num2 min

? 20 15 ? 15 20 ?

Key-in 2 numbers: 20 _ 15 Smallest: 15 _ _


Introduction to Computer Programming

if - else Statement
Syntax:

or

if (expression) statement; else statement; if (expression) { statement1; statement2; } else statement3;

Introduction to Computer Programming

Example:
void main() { int mark; cout<<Mark: ; cin>>mark; if (mark >= 50) cout<<Pass\n; else cout<<Fail\n;
}
Introduction to Computer Programming

Potrebbero piacerti anche