You are on page 1of 78

DFC20113 -

PROGRAMMING
FUNDAMENTALS

CHAPTER 2 : BASIC PROGRAM ELEMENTS

BY: MADAM HANI


PART 1
• Explain variables

• Declare variables and constants with an


appropriate data types.
• Initialize variables.

• Identify the problem of uninitialized


variables.

• 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.

Every constant has :

• Name
• Type
• Values that are set to them
• Syntax :

const data_type constant_name = constant_value;

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)

Method 3 (Initializing a variable using


braces)
Method 4 (Declaring a variable using auto
class)
‘auto’ is a keyword which tells the compiler the
type of the variable upon its initialization
Method 5 (Declaring and
Initializing a variable through
‘auto’ keyword with parenthesis)
Method 6 (Declaring and
Initializing a variable through
‘auto’ keyword with braces)
Method 7 (Dynamic
Initialization)
C++ does not initialize most variables to a
given value (such as zero) automatically.

Thus, when a variable is assigned a memory


location by the compiler, the default value of
that variable is whatever (garbage) value
happens to already be in that memory location

A variable that has not been given a known


value (usually through initialization or
assignment) is called an uninitialized variable.
TRACE THE OUTPUT
❑Special word / standard in the standard library
❑Has a specific purpose that is understood by the
compiler
❑Not valid if it is used for another purposes
❑Reserved word are also called keywords.

Must be typed / written Cannot be used as the


in lowercase name of identifier
(case-sensitive) (constant & variable)
EXAMPLE TO ASSIGN VALUE TO
THE VARIABLE

Initialization (Memberi nilai awal)

Interactive method (Secara interaktif)


PART 2
• Identify the scope of variables

• Describe the scope of variables.


• Explain the local and global of variables.

• List the differences between local and


global variables based on the scope and
value after declaration.
• Thereare two main variable scopes:
 Local variable
 Global variable
• Local variable
 It is a variable that has been defined immediately after the
opening braces of a function.
 Each functions can have their own variables declarations.
 You can access (and change) local variables only from the
functions in which they are defined. Therefore that variable's
scope is protected.
 All local variables lose their definition when their block ends.
• Example of local variable:
TRACE THE OUTPUT
TRACE THE OUTPUT
• Global variable
 A global variable is a variable that has been defined before a
function (usually before the main() function).
 This means, all function in that program can use this variable.
 Global variables can be used anywhere in the program as soon
as it has been defined.
• Example of global variable
EXAMPLE
LOCAL VARIABLE VS.
GLOBAL VARIABLES

Parameter Local Global


It is declared inside a It is declared outside the
Scope
function. function.
If it is not initialized, a If it is not initialized zero
Value
garbage value is stored is stored as default.
EXERCISES

Create a variable named myNum and assign the value 50 to it.

Display the sum of 5 + 10, using two variables: x and y.


Create a variable
called z, assign x + y to
it, and display the
result.
PART 3
• Describe input output statements

• Identify the syntax used for input and


output
• Write programs using input and output
statements. Run test and debug the
program.
INPUT
OUTPUT
EXERCISES
PART 4
• Explain operators and expression
• Define operator.
• Explain the types of operators:
a. Assignment operators
b. Arithmetic operators
c. Increment and decrement operators
d. Relational operators
e. Logical operators

• Identify the syntax for each operator with


example.
• Differentiate the assignment (=) and equality
(= =) operator.
• Write expression using operators.
• Describe operators precedence.
• Solve expression according to the operators
precedence.
• Write a simple program that apply operators
and expression. Run test and debug the
program.
▪ An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations
▪ Operators are the foundation of any programming language.
▪ Thus, the functionality of C++ programming language is
incomplete without the use of operators.
▪ We can define operators as symbols that help us to perform
specific mathematical and logical computations on operands.
▪ In other words, we can say that an operator operates the
operands.
1. Assignment operator
2. Arithmetic operator
3. Increment & decrement operators
4. Relational operator
a) Not Equivalent Group
b) Equivalent Group
5. Logical operator
ASSIGNMENT OPERATOR

equal (=) symbol is used


This operator is used to
as symbol in assignment
change variable’s value.
operations.

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.

a) Not Equivalent Group


Symbol Operators
> Greater than
>= Greater than or equals to
< Less than
<= Less than or equals to
RELATIONAL OPERATOR

b) Equivalent Group

Symbol Operators

== Equal to

!= Not equals to
EXAMPLE
EXERCISE
62
LOGICAL OPERATOR

Return the value of 1, if the logical is true and


0 if it is false.

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;

int d = a-- && --b;


6. int var1 = 5;
int var2 = 6;
int var3;
var3 = ++ var2 * var1 / var2 + var2;

7. int a =0 ; int b =1 ;
( (a > 1) || (b < -1) )

You might also like