0% found this document useful (0 votes)
0 views

Copy of Arrays and Jump

The document provides an overview of arrays in Java, including their declaration, creation, indexing, and initialization, as well as the concept of multidimensional arrays. It also covers various types of operators in Java, such as arithmetic, relational, logical, and bitwise operators, along with expressions and control statements like selection, iteration, and jump statements. The content emphasizes the structure and functionality of these programming concepts essential for Java development.

Uploaded by

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

Copy of Arrays and Jump

The document provides an overview of arrays in Java, including their declaration, creation, indexing, and initialization, as well as the concept of multidimensional arrays. It also covers various types of operators in Java, such as arithmetic, relational, logical, and bitwise operators, along with expressions and control statements like selection, iteration, and jump statements. The content emphasizes the structure and functionality of these programming concepts essential for Java development.

Uploaded by

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

Arrays

⚫ An array is a group of liked-typed variables referred to by


a common
⚫ name, with individual variables accessed by their index.
⚫ Arrays are:
1) declared
2) created
3) initialized
4) used
⚫ Also, arrays can have one or several dimensions.
Array Declaration

⚫Array declaration involves:


1) declaring an array identifier
2) declaring the number of dimensions
3) declaring the data type of the array elements
⚫Two styles of array declaration:
type array-variable[];
or
type [] array-variable;

L 2.8
Array Creation
⚫After declaration, no array actually exists.
⚫In order to create an array, we use the new
operator:
type array-variable[];
array-variable = new type[size];
⚫This creates a new array to hold size elements of
type type, which reference will be kept in the
variable array-variable.
Array Indexing

⚫ Later we can refer to the elements of this array through


their indexes:
⚫ array-variable[index]
⚫ The array index always starts with zero!
⚫ The Java run-time system makes sure that all array
indexes are in the correct range, otherwise raises a run-
time error.
Array Initialization

⚫ Arrays can be initialized when they are declared:


⚫ int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
⚫ Note:
1) there is no need to use the new operator
2)the array is created large enough to hold all specified
elements
Multidimensional Arrays

⚫ Multidimensional arrays are arrays of arrays:


1) declaration: int array[][];
2) creation: int array = new int[2][3];
3) initialization
int array[][] = { {1, 2, 3}, {4, 5, 6} };
Operators Types

⚫Java operators are used to build value expressions.


⚫Java provides a rich set of operators:
1) assignment
2) arithmetic
3) relational
4) logical
5) bitwise

L 2.13
Arithmetic assignments
+= v += expr; v = v + expr ;

-= v -=expr; v = v - expr ;

*= v *= expr; v = v * expr ;

/= v /= expr; v = v / expr ;

%= v %= expr; v = v % expr ;
Basic Arithmetic Operators
+ op1 + op2 ADD

- op1 - op2 SUBSTRACT

* op1 * op2 MULTIPLY

/ op1 / op2 DIVISION

% op1 % op2 REMAINDER

L 2.15
Relational operator
== Equals to Apply to any type

!= Not equals to Apply to any type

> Greater than Apply to numerical type

< Less than Apply to numerical type

>= Greater than or equal Apply to numerical type

<= Less than or equal Apply to numerical type


Logical operators
& op1 & op2 Logical AND

| op1 | op2 Logical OR

&& op1 && op2 Short-circuit


AND
|| op1 || op2 Short-circuit OR

! ! op Logical NOT

^ op1 ^ op2 Logical XOR


L 2.17
Bit wise operators
~ ~op Inverts all bits

& op1 & op2 Produces 1 bit if both operands are 1

| op1 |op2 Produces 1 bit if either operand is 1

^ op1 ^ op2 Produces 1 bit if exactly one operand is 1

>> op1 >> op2 Shifts all bits in op1 right by the value of
op2
<< op1 << op2 Shifts all bits in op1 left by the value of
op2
Expressions
⚫An expression is a construct made up of variables,
operators, and method invocations, which are
constructed according to the syntax of the language, that
evaluates to a single value.
⚫Examples of expressions are in bold below:
int number = 0;
anArray[0] = 100;
System.out.println ("Element 1 at index 0: " +
anArray[0]);
int result = 1 + 2; // result is now 3 if(value1 ==
value2)
System.out.println("value1 == value2");
L 2.19
Expressions
⚫ The data type of the value returned by an expression depends on
the elements used in the expression.
⚫ The expression number = 0 returns an int because the
assignment operator returns a value of the same data type as its
left-hand operand; in this case, number is an int.
⚫ As you can see from the other expressions, an expression can
return other types of values as well, such as boolean or String.
The Java programming language allows you to construct
compound expressions from various smaller expressions as long
as the data type required by one part of the expression matches
the data type of the other.
⚫ Here's an example of a compound expression: 1*2*3
Control Statements

⚫ Java control statements cause the f low of execution to


advance and branch based on the changes to the state
of the program.
⚫ Control statements are divided into three groups:
⚫ 1) selection statements allow the program to choose
different parts of the execution based on the outcome
of an expression
⚫ 2) iteration statements enable program execution to
repeat one or more statements
⚫ 3) jump statements enable your program to execute in
a non-linear fashion
L 3.1
Selection Statements

⚫Java selection statements allow to control the flow


of program’s execution based upon conditions
known only during run-time.
⚫Java provides four selection statements:
1) if
2) if-else
3) if-else-if
4) switch
Iteration Statements
⚫ Java iteration statements enable repeated execution of
part of a program until a certain termination condition
becomes true.
⚫ Java provides three iteration statements:
1) while
2) do-while
3) for

L 3.3
Jump Statements

⚫ Java jump statements enable transfer of control to


other parts of program.
⚫ Java provides three jump statements:
1) break
2) continue
3) return
⚫ In addition, Java supports exception handling that can
also alter the control f low of a program.

You might also like