Professional Documents
Culture Documents
PROGRAMMING
FUNDAMENTALS
• Explain keywords.
• Implement, write, test and debug program
using data types, variables, constants and
keywords.
Variable provides
A variable is a Each data used for
one mechanism to
place to store a computer program
identify each data
piece of is stored in a
using one special
information variable.
name.
Declaration format :
Variables represent Variables are like
memory locations the storing boxes to •Datatype variable_name;
where the value a computer
may change. program.
int bilangan;
float berat;
char abjad;
The value of constant remains
unchanged for the whole duration of the
program execution.
• Name
• Type
• Values that are set to them
• Syntax :
or
#define constant_name constant_value
• Examples :
const double PI = 3.1459;
#define PI 3.1459
Constant declaration
Variables declaration
Method 1 (Declaring the variable and then
initializing it)
Method 2 (Initializing a variable using
parenthesis)
Example:
•x = 5;
•So, 5 are assigned to variable x.
•x = y = 0;
•variable x and variable y are
assigned to the same value,
which is 0.
ASSIGNMENT OPERATOR
• Compound assignment operator is the
operator that is combined with –, +, %, *,
/ and other operations
Expression Meaning
a += b a=a+b
a *= 5 a=a*5
a –= 2 a=a–2
a %= 3 a=a%3
EXAMPLE
ARITHMETIC OPERATOR
Calculation operation that can
be done on data in variables.
Symbol Operators
+ Add
- Subtract
/ Divide
* Multiply
% Modulus
EXAMPLE
EXERCISES
INCREMENT AND
DECREMENT OPERATOR
Variables usually can be increased
or decreased by 1
Symbol Operation
++ Add 1
-- Subtract 1
Example:
x++ can also be written as x = x + 1
y– – can also be written as y = y – 1
EXERCISES
RELATIONAL OPERATOR
Return the value of 1, if the relation is true
and 0 if it is false.
b) Equivalent Group
Symbol Operators
== Equal to
!= Not equals to
EXAMPLE
EXERCISE
62
LOGICAL OPERATOR
Symbol Operators
&& AND
|| OR
! NOT
a b a && b
true true true
true false false
false true false
false false false
Results of a && b:
a b a || b
true true true
true false true
false true true
false false false
Results of a || b:
Example
EXERCISE
THE ASSIGNMENT (=) VS
EQUALITY (= =) OPERATOR
• The “=” is an assignment operator is used to assign the
value on the right to the variable on the left.
THE ASSIGNMENT (=) VS
EQUALITY (= =) OPERATOR
• The ‘==’ operator checks whether the two given
operands are equal or not. If so, it returns true.
Otherwise, it returns false.
EXPRESSION USING
OPERATORS.
OPERATORS’ PRECEDENCE
1. () Highest priority
2. ! ++ ––
3. * / %
4. + –
5. < <= > >=
6. == !=
7. &&
8. ||
9. = Lowest priority
EXERCISES
1. 10 + 20 * 30
2. 100 + 200 / 10 - 3 * 10
3. 1 / 2 * (3 - 4) * 5 + (6 % 7 - (8 - 9) / 8)
4. int x, y = 5, z = 5;
x = y == z
5. int a = 1;
int b = 1;
int c = a || --b;
7. int a =0 ; int b =1 ;
( (a > 1) || (b < -1) )