Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Grade 10 W2 - Q2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 53

GRADE 10

Variables, constants, and


literals

S AI NT THOMAS BECKET ACADEMY


JERIMIAH 29:11

For I know the plans I have


for you,” declares the
LORD, “plans to prosper
you and not to harm you,
plans to give you hope and
a future.
1
int 2
float 3
char 4
str

5
Boolean
6
char 7
int 8
Boolean

9
str 10
double 11
float 12
double

RESET
LEARNING TARGETS
1. Differentiate variables, constraints, and
literals.
2. Use variables, constraints, and literals in
creating programs.
3. Identify and assign the correct data type to a
variable.
4. Construct statements and code blocks using
Visual C#.
What is a variable?
A VARIABLE is a programmer-
defined word that holds the value of
the user. It also saves a portion of
the memory to store a determined
value.
Since variables are programmer-
defined words there are rules to
follow in declaring them in a
program.
Rules in declaring
variable
1. It must begin with a letter or an underscore (_),
followed by letters, digits, or underscores.
2. It must not include blanks.
3. It must not be the keywords which are reserved
words by the language.
4. It must not be given a name with the same name as
other functions.
5. Variables in C# are case sensitive. For example,
variable DepEd is different from deped.
What is a constant?
A CONSTANT is an expression
with a fixed value that cannot be
changed at runtime, unlike a
variable whose value can be
changed during runtime based on
the user’s input.
To declare a constant in C#, the const
keyword is used.
This syntax should be followed:
const <data_type> <constant_name> = value;

Example:
const short a=10;
In the const short a=10; declaration,
a is declared as a constant with a
fixed value of 10.

The fixed values are also called


literals.
There are character constants in C# that
when preceded by the backlash (\)
symbol, have special meaning. Some
are used to format the result as they
appear in the computer screen.
Character Constant in C#
Description Escape Code
Newline \n
Carriage return \r
Tab \t
Vertical tab \v
Backspace \b
Form Feed (page feed) \f
Alert (beep) \a
Single quote (‘) \’
Double quote (“) \”
Question mark (?) \?
Backslash (\) \\
WHAT ARE LITERALS?
Literals are used as particular values
within the source code of a program,
for example: a = 5; 5 in this piece of
code, is a literal constant.
Literal constants can be integer
numerals, floating-point numerals,
characters, strings, or Boolean
values.
Integer Literals are constant and are
also called decimal numerals.

For example: 235, 612, 18, 29.


Character and string literals are nonnumerical
constants like:
‘z’ – character
‘j’ – character
‘a’ – character
“jasmin” – string
“Hello World” – string
The difference between a character and a
string literal is, a character represents a single
character constant, while a string literal is
composed of several characters. Also, a
character is enclosed with single quotation
marks (‘ ’) while a string has double
quotation marks (“ “).
Lastly, the Boolean literals can have
either a True or a False value.
WHAT
IS A
DATA TYPE?
Variables are allocated in the
computer’s memory.

A variable should be identified


based on the type of data it can hold,
or simply called a data type.
CHARACTERISTICS OF A
DATA TYPE?
1. Name – value type

2. Size – how much memory they


use

3. Default value – the initial value


A variable is declared like this:
<data type>><variable>

Example:
string name;
int a, b, c;
A variable can have an initial value by adding a
value to it during declaration. It is declared as:
<Data type><variable name> = value;

Example:
int A=5;
double B=2.9;
double pi=3.1416;
Data types in C# can be categorized as
value types, reference types and pointer
type.

The difference between the three


categories are how they are presented in
the memory.
VALUE
DATA TYPES
VALUE DATA TYPES
– holds data within its own
memory location.
Example:
int I = 42;
char ch = ‘A’;
bool result = true;
INTEGER (int)
It is the most common numeric
data type used to store numbers
without a fractional component
(-707, 0, 707).
FLOATING POINT (float)
It is also a numeric data type used to store
numbers that may have a fractional
component like monetary values do
(707.07, 0.7, 707.00).

Please note that number is often used as a


data type that includes both int and float
types.
CHARACTER (char)
It is used to store a single letter,
digit, punctuation mark, symbol,
or blank space.
STRING (str or text)
It is a sequence of characters and
the most commonly used data type
to store text. Additionally, a string
can also include digits and
symbols, however, it is always
treated as text.
STRING (str or text)
A phone number is usually
stored as a string (+1-999-666-
3333) but can also be stored as
an integer (9996663333).
BOOLEAN (bool)
It represents the values true and
false. When working with the
boolean data type, it is helpful to
keep in mind that sometimes a
boolean value is also represented as
0 (for false) and 1 (for true).
REFERENCE
DATA TYPES
REFERENCE DATA TYPE
– contain a reference to another
memory location that holds the data.
Example:
String str = “Hello”;
Object obj = 42;
Byte bytes = {1, 2, 3};
POINTER
DATA TYPES
POINTER DATA TYPE
– stores the memory address of
another type
Example:
char* cptr;
int* iptr;
DEEPER
A. CHARACTER
B. CONSTANT
C. INTEGER
D. STRING
E. VARIABLE
1. An expression with a
fixed value that cannot be
changed at runtime
2. A programmer-defined
word that holds the value
of the user.
3. It is the most common
numeric data type used to
store numbers without a
fractional component
4. It is used to store a
single letter, digit,
punctuation mark, symbol,
or blank space.
5. It is a sequence of
characters and the most
commonly used data type
to store text.
INSTRUCTION:
Explain the following
concepts in your own words.
Variables Constants Literals
DO/EVALUATION
INSTRUCTION:
Create a program that will ask the user
to enter the company’s name, address,
phone number, email address, and
website address. Print the information on
the console window. Use variables,
constants or literals and assign the
correct data type.

You might also like