Java Programming Basics: Variables & Operators
Java Programming Basics: Variables & Operators
Programming
Basic in Java
Session 1:
Introduction to
Java
VARIABLES, OPERATORS AND PRECEDENCE
SEQUENTIAL EXPRESSIONS AND ALGORITHMS
Session Objectives
At the end of this session you will be able to:
1. Determine the minimum components of a program written in Java.
2. Identify and understand the potential initial errors of a program written in Java.
3. Describe and apply the print and println methods.
4. Differentiate Types of Data
5. Declare variables
6. Management of arithmetic, relational, and logical operators
7. Operator precedence
[Link]
[Link]
10. Manipulating String and Character Data Types.
[Link] numbers to strings and from strings to numbers for input.
Session objectives
At the end of this session you will be able to:
12. Describe and apply the Scanner method.
13. Describe the relational operators
14. Design and develop programs that include If-else, simple if-else if-else, and nested conditionals.
15. Design and develop programs that include the use of switch.
16. Design and develop programs that include a while loop
[Link] and develop programs that include a do-while loop
18. Compound assignment operators (operator=)
19. Increment and decrement operators (++, --)
20. Design and develop programs that include the repetitive for loop
INTRODUCTION TO
PROGRAMMING
Variables P
To remember:
A variable always
it must be identified
In Pseudocode
for a name and
Types of variables:
defined by a type.
Entero
2. Real
3. Character
4. Logic
Variable declaration P
Examples:
int(integer) to String:
int integer = 5;
String enteroString = [Link](entero);
String a double:
String DoubleString = “8342342";
double aDouble = [Link](aString);
double a String:
double d = 8342342;
[Link]([Link](d));
EXPRESSIONS AND
OPERATORS
Allocation Operation
Let's remember:
This operation allows assigning value to variables.
Algorithmic representation:
The new:
In Java, the assignment operation is represented by the symbol =
Pseudocode Java
A5 A=5
B A+2 B=A+2
A7 A=7
Expressions - Let's remember
○ Arithmetic
○ Relational
○ Logics
Arithmetic Operators
Operator Name Example Java
+ Sum A+4 +
- Stay A-4 -
* Multiplication B*2 *
/ Division C/3 /
Mod Modulo (remainder of the division 15 mod 2 = 1 15 % 2
whole)
=Div Quotient of the integer division 15 divided by 2 equals 7 In Java, it does not exist; the closure axiom is applied.
among the integers: 15 / 2 = 7
^ Power or B cubed [Link](base, exponent)
[Link](B, 3)
Relational Operators
Operator Name Example Java
> Greater than 4 is greater than
> 2
< Less than 3<10 <
>= Greater than or equal 5>=5 >=
<= Less than or equal to 7 is less than<=
or equal to 9
= Equal 3=2 ==
<> Different 9<>7 !=
Logical Operators
Operador Name Example Java
Where "The result is:" is a message that is desired to appear and R is the variable that
contains a value.
Java:
• [Link](“El resultado es: ”+ R);(Muestra el mensaje entre paréntesis y deja el
cursor on the same line after displaying it
Java:
The [Link] library must be imported; then a variable is defined that will allow
capture the data via keyboard.
Scanner leer = new Scanner([Link]);
Depending on the type of data, these are read using nextTipo( where Tipo can be: Int,
Float, Double, Line) as follows:
a = [Link]();
b=[Link]();
Common mistakes
Forgetting a semicolon at the end of a statement or instruction.
2. Not closing braces of any block of code, method, class, or in any structure of
control.
3. Give the same name to variables with different types.
4. Assign to a variable another with a different data type: for example: a String variable and
se le asigna una variable de tipo int, en este caso el compilador arroja un error de
type conversion.
5. Input of values different from those received by the application.
6. Accessing a position that does not exist in an array.
7. Store strings where numbers should be stored.
8. Divisions by zero.
STRUCTURES
ALGORITHMIC
CONDITIONALS
Simple Conditional Structures
PSEUDOCODE JAVA
if (condition){
If (condition) then
Action(s) actions
End if
}
Double Conditional Structures:
PSEUDOCODE JAVA
if (condition_1) {
If (condition_1) then
Action(s)_1 Action(s)_1
but } else if (condition_2) {
If (condition_2) then Action(s)_2
Action(s)_2 } else if (condition_3) {
but Action(s)_3
} else {
Several conditions
Several conditions
finish
finished
}
Nested Conditional Structures
PSEUDOCODE JAVA
finish }
STRUCTURE
ALGORITHMIC
DEPENDING ON
Depending On (DO) or According To
Pseudocode Java
STRUCTURES
ALGORITHMIC
Cyclic or repetitive
Cyclic structures
Loops with an Indeterminate Number of Iterations Cycle with a Determined Number of
(While and Do Until or repeat until) Iterations (For)
ExitI am not in IF
ExitI am over 15
Nested If example:
package practice;
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
[Link]("I am less than 15");
// Nested if declaration
It will only execute if the previous statement
It's true
if (i < 12)
I am also younger than 12
else
[Link]("I am greater than 15");
}
I am less than 15
I am under 12
Switch-case example:
int i = 9;
switch (i)
{
case 0:
[Link]("i is zero.");
break;
case 1:
i is one.
break;
case 2:
[Link]("i is two.");
break;
default:
[Link]("i is greater than 2.");
}