0% found this document useful (0 votes)
7 views39 pages

Java Programming Basics: Variables & Operators

This document serves as an introductory guide to programming in Java, covering fundamental concepts such as variables, data types, operators, and control structures. It outlines session objectives, common programming errors, and provides examples of conditional and loop structures. Additionally, it includes exercises for practical application of the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views39 pages

Java Programming Basics: Variables & Operators

This document serves as an introductory guide to programming in Java, covering fundamental concepts such as variables, data types, operators, and control structures. It outlines session objectives, common programming errors, and provides examples of conditional and loop structures. Additionally, it includes exercises for practical application of the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CICLO II:

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

A variable in programming is a unit of data that can


change value. It is the simplest form of storage,
And in Java...
representing an area ofmemorywhere is a stored 1. Enter int
data element. 2. Real float, double
3. Character
String
4. Boolean Logic

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

● data type: types of data that can be stored


in this variable.
● variable_name: name given to the variable.
● value: it is the initial value stored in the variable.

Examples:

float simpleInterest; //Declaring float variable


int time = 10, speed = 20; //Declaring and Initializing the variable
integer
char var = 'h'; // Declaring and Initializing the character variable
Convert String to Number or
Number to String
String (string) to int (integer):
String enteroString = "5";
int integer = [Link](integerString);

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

● An expression consists of operators and operands.


● Depending on the type of data they manipulate, they are classified into:

○ 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

And, & Y (3>5) & (4<10) &&


Or, v, | O (3<5) | (4<10) ||
NO Denial ~(6=6) !
P Q P or Q P
Q
V V
V V
V F
V F
V F
F V
F F
F F
Priority of all
the operators
Output Structure -Writing
Pseudocode:
Write 'The result is:', R

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

• [Link]("The result is: " + R);


position the cursor on a new line
Input Structures - Reading
Pseudocode:
Read a, b;
Where "a" and "b" are the integer type variables that will receive the values.

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) then if (condition){


Action(s)_1 actions 1
yes or no
else {
Action(s)_2 actions 2
End
}
Multiple 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

If (condition_1) then if (condition_1) {


If (condition_2) then if (condition_2) {
Action(s)_1 Action(s)_1
if not } else {
Action(s)_2 Action(s)_2
End }
but } else {
Several conditions Various conditions

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)

To do for c0 VI to VF with inc I make


While (condition) do Action1 Action1
Action1
Action2 Action2 Action2
. . .
.
. .
Action Where:
. c: variable control
End-while . VI: Initial Value
Action ActionN Final Value
I: Increase
End-for
Until (condition)
Cyclic structures in Java

While Make While For

while (condition) { do{ for (c= vi; c<= vf; c=c+i) {


Action 1 Action 1
Action 2 Action 2 Action 1
. . Action 2
. . .
.
} }while (condition);
}
Compound assignment operators
Increment and decrement operators
Exercise

Design an algorithm that calculates the


average grades of the first term
a course of N students.
Solution
Java coding of structures
algorithmic.
Example If: If-else example:
package practice;
int i = 10;
if (i > 15)
int i = 20;
10 is less than 15
// This statement will execute if (i < 15)
as if considered a statement for I am less than 15
defect else
[Link]("I am not in IF"); I am over 15

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.");
}

Exiti is greater than 2.


Example while-if-for:
Scanner input = new Scanner([Link]);
int magic_square[][]=new int[3][3];
int counter = 0;
while(true){
[Link]("Enter the Magic Number: ");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
[Link]("Number " +(i+1)*(j+1));
magic_square[i][j] = [Link]();
if (magic_square[i][j] < 1 || magic_square[i][j] > 9)
ERROR! Input range is invalid.
else
counter++;
}
}

Outputi is greater than 2.


Exercises for
to practice

You might also like