Uploaded by Neutral

Programming Elements

advertisement
PROGRAM ELEMENTS and
STRUCTURE
2/13/2023
VARIABLES
 is a portion of the computer’s main
memory used to store a numeric or
string constants.
 Value might change during the program
execution but it can hold one value at
the given time.
2/13/2023
Rules in writing variables:
• Variable name can consist of letter, alphabets
and start with underscore character.
• First character of variable should always be
alphabet and cannot be digit.
• Blank spaces are not allowed in variable name.
• Special characters like #, $ are not allowed.
• A single variable can only be declared for only
1 data type in a program.
2/13/2023
RULES IN WRITING VARIABLES:
• As C++/JAVA is case sensitive language so if we
declare a variable name and one more NAME
both are two different variables.
• JAVA has certain keywords which cannot be
used for variable name.
2/13/2023
Check which one is valid
•
•
•
•
•
•
•
•
•
•
CTR
Num1
1_Num
void
Emp name
Sum
Total_Amount
cout
Num_of_Students_
_employeeNum
2/13/2023
Remember: “Once declared,
the type of the variable DOES
NOT change during program
execution.”
2/13/2023
Numeric Variable
Is a variable that contain or hold numeric
constants (computational values) only.
DATA TYPES:
int
float
double
2/13/2023
String Variable

Is a variable that can contain or hold
string constants (non-computational
values) only.

Data types
char

string

2/13/2023
CONSTANTS
•
•
•
A constant is a literal value that does
not change during the program
execution.
Value that we assign to a variable.
Value can be numeric or string.
2/13/2023
TYPES OF CONSTANTS
Numeric Constant
 String Constant
2/13/2023
Numeric Constant
• may be of an integer (whole
number) or real number (w/ a
decimal place).
• May be positive or negative.
• Contain only the characters from
0-9, + or -., no other character
must appear together with the
constants.
2/13/2023
EXAMPLES
32
-17
+8.6
3140
0.25
+14.75
23.625
-1.20
-0.125
2/13/2023
Examples of incorrect numeric constants
2,500
$125.70
(comma is not allowed)
($ is not allowed)
36cm
specified)
(unit of measurement should not be
12 ½
(slash (/) symbol is not allowed)
2/13/2023
13
String Constant
• is a character or a group of characters
enclosed in quotation marks (“ ” ) symbols .
• It can be the numbers form 0-9.
• Letters A to Z and all special characters like
space, %, $, etc.
2/13/2023
Examples
• ‘X’
• “844-4286”
• “Juan De La Cruz”
• “University of Saint
Louis”
• “25%”
• “(078)844-3514”
• “ComPro1”
• “BSIT-1”
• “#28”
2/13/2023
EXPRESSIONS
 a group of program elements consisting of
operands and operators.
 Yield a value of a certain type depending on
the type of expressions used.
• Operand → can be a constants and or a
variables
• Operators→ can be arithmetic, logical,
relational and or string.
2/13/2023
Download