Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

variables and operators

The document provides an overview of variables in programming, including their declaration, initialization, and outputting values using examples in Java. It also covers operators such as arithmetic, relational, logical, and assignment operators, explaining their usage and providing sample code. Additionally, it includes an activity for calculating interest on an investment, reinforcing the concepts discussed.

Uploaded by

Senpai Tuazon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

variables and operators

The document provides an overview of variables in programming, including their declaration, initialization, and outputting values using examples in Java. It also covers operators such as arithmetic, relational, logical, and assignment operators, explaining their usage and providing sample code. Additionally, it includes an activity for calculating interest on an investment, reinforcing the concepts discussed.

Uploaded by

Senpai Tuazon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

VARIABLES

ANDREA NICOLE QUERUBIN


VARIABLES
- Variables are used to store values. A variable has a
data type and a name. Follow the naming convention
of variable identifiers.
VARIABLES
VARIABLE DECLARATION AND INITIALIZATION
Syntax:
Variable Declaration:
<data_type> <variable_name>;
int myNum;
myNum = 100;

Variable Declaration and Initialization:


<data_type> <variable_name> = <value>;
int myNum = 100;
VARIABLE DECLARATION
AND INITIALIZATION
EXAMPLE:
Variable Declaration
public class VariableSamples {
public static void main(String[] args) {
byte age;
double price;
char sex;
string name;
}
}
VARIABLE DECLARATION
AND INITIALIZATION
EXAMPLE:
Variable Declaration with Initialization
public class VariableSamples {
public static void main(String[] args) {
byte age= 12;
double price = 55.1;
char sex = ‘M’;
string name = “Juan dela Cruz”;
}
}
OUTPUTTING VARIABLE
DATA/VALUE
In this example, the output command System.out.println() will
be used to display the value of a variable.

Sample Program:
public static VariableSamples {
public static void main(String[] args) {
byte age = 12;
String name = “Juan dela Cruz”;
System.out.println(age);
System.out.println(“My name is” + name + “.”);
}
}
NOTE:
1. It is always good to initialize a variable as it is declared.
2. Use descriptive names for variables. For example, a
variable that would hold the age of a person can be
named as age and not just some random letters.
3. One-line declaration per variable is preferred over
variable group declaration. For example, the variable
declarations,
double exam = 0;
double quiz = 10;
double grade = 10;
is preferred over the declaration,
double exam=0, quiz=10, grade=0;
Group declaration of variables, however, is accepted and
could only be done to variables sharing the same data types.
OPERATORS
SAMPLE PROGRAM:
public static VariableSamples {
public static void main(String[] args) {
int num1 = 10;
int num2 = 3;
int sum = num1 + num2; = 13
int diff = num1 - num2; = 7
int prod = num1 * num2;
int quoInt = num1 / num2;
float quoDec = (float) num1 / (float) num2;
int rem = num1 % num2;
System.out.println(“sum=” + sum
+ “\ndifference=” + diff
+ “\nproduct=” + prod
+ “\nwhole quotient=” + quoInt
+ “\ndecimal quotient=” + quoDec
+ “\nremainder=” + rem);
}
}
NOTE:
1. The used variable quoInt does not hold the decimal place
value of the computed quotient since it was declared as an
integer. Moreover, since the operands are both integers, the
resulting quotient which is float is implicitly converted into
integer by disregarding its decimal place value without
rounding it up/down.
2. The quoDec displays the decimal place value of the
computed quotient since it was declared as a float variable,
however, the operands which are integers need to be
explicitly converted into float for it to return a float resulting
quotient.
3. When an integer and a floating-point number are used as
operands to a single arithmetic operation, the result is a
floating point. The integer is implicitly converted to a floating-
point number before the operation takes place.
OPERATORS
SAMPLE PROGRAM:
public static VariableSamples {
public static void main(String[] args) {
int num1 = 10;
int num2 = 3;
int sum = 0;

System.out.println(num1 + “, ” + num2 + “, ” + sum);


sum = num1++ + num2; //will result to 10 + 3 = 13
System.out.println(num1 + “, ” + num2 + “, ” + sum);

int num1 = 10;


int num2 = 3;
int sum = 0;

System.out.println(num1 + “, ” + num2 + “, ” + sum);


sum = ++num1 + num2; //will result to 11 + 3 = 14
System.out.println(num1 + “, ” + num2 + “, ” + sum);
}
}
OPERATORS
Equality and Relational Operators

OPERATOR USE DESCRIPTION


> operand1 > operand2 Operand1 is greater than operand2
>= operand1 >= operand2 Operand1 is greater than or equal to
operand2
< operand1 < operand2 Operand1 is less than operand2
>= operand1 <= operand2 Operand1 is less than or equal to
operand2
== operand1 == operand2 Operand1 is equal to operand2
!= operand1 != operand2 Operand1 is not equal to operand2

Relational operators compare two values and determines the relationship


between those values. The output of evaluation is either of the boolean values true
or false.
SAMPLE PROGRAM:
public static VariableSamples {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
boolean gt = num1 > num2;
boolean gte = num1 >= num2;
boolean lt = num1 < num2;
boolean lte = num1 <= num2;
boolean e = num1 == num2;
boolean ne = num1 ! = num2;
System.out.println(gt);
System.out.println(gte);
System.out.println(lt);
System.out.print1n(lte);
System.out.print1n(e);
System.out.println(ne);
}
}
OPERATORS
Logical Operators
You can also test for true or false values with logical operators.

OPERATOR DESCRIPTION

&& Logical AND

& Boolean logical AND

|| Logical OR

| Boolean logical inclusive OR

^ Boolean logical exclusive OR

! Logical not

Logical operators have one or two boolean operands that yield a boolean result.
TRUTH TABLE FOR &
AND &&
OPERAND1 OPERAND2 RESULT
True True True
True False False
False True False
False False False

The basic difference between && and & operators is that &&
supports short circuit evaluations (or partial evaluations), while & doesn’t.

Given an expression,
exp1 && exp2
&& will evaluate the expression exp1, and immediately returns a
false value if exp1 is false. If exp1 is false, the operator never evaluates
exp2 because the result of the operator will be false regardless of the value
of exp2. In contrast, the & operator always evaluates both exp1 and exp2
before returning an answer.
TRUTH TABLE FOR |
AND ||
OPERAND1 OPERAND2 RESULT
True True True
True False True
False True True
False False False

Similar to & and &&, the basic difference between | and ||


operators is that || supports short-circuit evaluations (or partial
evaluations), while | doesn’t.
TRUTH TABLE FOR ^
OPERAND1 OPERAND2 RESULT
True True False
True False True
False True True
False False False

The result of an exclusive OR operation is TRUE, if and only if


one operand is true and the other is false.
TRUTH TABLE FOR !
OPERAND1 RESULT
True False
False True

The logical NOT takes in one argument and negates its value,
wherein that argument can be an expression, variable or a literal.
ASSIGNMENT OPERATORS
OPERATOR USE DESCRIPTION
= operand1 = operand2 The value of operand2 is assigned to
operand1
+= operand1 += operand2 operand1 = operand1 + operand2
-= operand1 -= operand2 operand1 = operand1 - operand2
*= operand1 *= operand2 operand1 = operand1 * operand2
/= operand1 /= operand2 operand1 = operand1 / operand2
%= operand1 %= operand2 operand1 = operand1 % operand2

Assignment operators are used to assign, place, or give a value to a


variable. Operand1 must be a variable, operand2 can either be a literal, variable,
or an expression.
ACTIVITY
Create a program that will compute the amount of
interest that is earned on P17,000 invested at an interest rate
of 0.07 for one year. The interest and the value of the
investment after one year are printed to standard output.

Output:
The interest earned is P________.
The value of the investment after one year is P_______.

You might also like