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

Arrays, Opp, Statements and Loops

The document provides an overview of arrays, control statements, operators, and loops in Java, detailing how to declare, initialize, and use arrays, as well as various types of operators including arithmetic, relational, logical, assignment, unary, ternary, and bitwise operators. It also explains control statements such as if, if-else, switch, break, and continue, with code examples for each concept. The document serves as a foundational guide for understanding basic programming constructs in Java.

Uploaded by

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

Arrays, Opp, Statements and Loops

The document provides an overview of arrays, control statements, operators, and loops in Java, detailing how to declare, initialize, and use arrays, as well as various types of operators including arithmetic, relational, logical, assignment, unary, ternary, and bitwise operators. It also explains control statements such as if, if-else, switch, break, and continue, with code examples for each concept. The document serves as a foundational guide for understanding basic programming constructs in Java.

Uploaded by

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

ARRAYS, CONTROL

STATEMENTS, OPERATORS
AND INTRO. TO LOOPS IN
JAVA
BY SYED HASSAN MUJTABA ZAIDI
ARRAYS
 AN ARRAY IN JAVA IS A COLLECTION OF ELEMENTS OF THE SAME TYPE, STORED

IN A CONTIGUOUS MEMORY LOCATION. ARRAYS ALLOW YOU TO STORE MULTIPLE

VALUES IN A SINGLE VARIABLE, MAKING IT EASIER TO MANAGE RELATED DATA.

KEY POINTS:

1. ARRAYS HAVE A FIXED SIZE, DEFINED WHEN THEY ARE CREATED.


2. INDEXES IN ARRAYS START AT 0.
3. ARRAY ELEMENTS CAN BE ACCESSED, UPDATED, AND ITERATED.
DECLARING, INITIALIZING, AND
USING AN ARRAY
 PUBLIC CLASS ARRAYEXAMPLE {
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
// STEP 1: DECLARE AN ARRAY OF
INTEGERS INT[] NUMBERS;
// STEP 2: INITIALIZE THE ARRAY WITH A SIZE OF 5
NUMBERS = NEW INT[5];
// STEP 3: ASSIGN VALUES TO THE ARRAY
NUMBERS[0] = 10;
NUMBERS[1] = 20;
NUMBERS[2] = 30;
NUMBERS[3] = 40;
NUMBERS[4] = 50;
DECLARING, INITIALIZING, AND USING AN
ARRAY
// STEP 4: ACCESS AND PRINT ARRAY ELEMENTS

SYSTEM.OUT.PRINTLN("FIRST ELEMENT: " + NUMBERS[0]);


SYSTEM.OUT.PRINTLN("SECOND ELEMENT: " + NUMBERS[1]);

// STEP 5: LOOP THROUGH THE ARRAY USING A FOR LOOP

SYSTEM.OUT.PRINTLN("ALL ELEMENTS IN THE ARRAY:");


FOR (INT I = 0; I < NUMBERS.LENGTH; I++) {
SYSTEM.OUT.PRINTLN("ELEMENT AT INDEX " + I + ": " + NUMBERS[I]); }
// STEP 6: ENHANCED FOR LOOP (FOR-EACH LOOP) FOR SIMPLER ITERATION
SYSTEM.OUT.PRINTLN("USING FOR-EACH LOOP:");
FOR (INT NUM : NUMBERS) { SYSTEM.OUT.PRINTLN(NUM);

}
CODE
 Declaration:
int[] numbers; creates a reference to an array of integers.

 Initialization:
new int[5] creates an array with 5 slots, each initialized to 0 by default.

 Assignment:
Values are assigned using the syntax arrayName[index] = value.
 Accessing Elements:
Use arrayName[index] to retrieve the value at the specified index.
 Iteration:
 for loop: Access each element by its index.
 for-each loop: Simplifies iteration when you only need to read elements.
OUTPUT
 FIRST ELEMENT: 10
 SECOND ELEMENT: 20
 ALL ELEMENTS IN THE ARRAY:
 ELEMENT AT INDEX 0: 10
 ELEMENT AT INDEX 1: 20
 ELEMENT AT INDEX 2: 30
 ELEMENT AT INDEX 3: 40
 ELEMENT AT INDEX 4: 50
 USING FOR-EACH LOOP: 10
 20
 30
 40
 50
ARITHMETIC OPERATORS
ARITHMETIC OPERATORS ARE USED FOR BASIC MATHEMATICAL CALCULATIONS LIKE
ADDITION, SUBTRACTION, MULTIPLICATION, ETC.
PUBLIC CLASS ARITHMETICOPERATORS {

PUBLIC STATIC VOID MAIN(STRING[] ARGS) {

INT A = 10;

INT B = 5;

SYSTEM.OUT.PRINTLN("ADDITION: " + (A + B)); // OUTPUT: 15

SYSTEM.OUT.PRINTLN("SUBTRACTION: " + (A - B)); // OUTPUT: 5

SYSTEM.OUT.PRINTLN("MULTIPLICATION: " + (A * B)); // OUTPUT: 50

SYSTEM.OUT.PRINTLN("DIVISION: " + (A / B)); // OUTPUT: 2

SYSTEM.OUT.PRINTLN("MODULUS: " + (A % B)); // OUTPUT: 0

}
RELATIONAL (COMPARISON) OPERATORS
Relational operators are used to compare two values. They return true or false.

public class RelationalOperators {


public static void main(String[] args) {
int x = 10;
int y = 5;

System.out.println("x > y: " + (x > y)); //


true
System.out.println("x < y: " + (x < y)); //
false
System.out.println("x == y: " + (x == y)); //
false
System.out.println("x != y: " + (x != y)); //
true
System.out.println("x >= y: " + (x >= y)); //
true
System.out.println("x <= y: " + (x <= y)); //
false
LOGICAL OPERATORS
LOGICAL OPERATORS ARE USED TO PERFORM LOGICAL OPERATIONS LIKE AND, OR, AND NOT.

public class LogicalOperators {


public static void main(String[] args) {
boolean a = true;
boolean b = false;

System.out.println("a && b: " + (a && b)); //


false (AND)
System.out.println("a || b: " + (a || b)); // true
(OR)
System.out.println("!a: " + (!a)); // false
(NOT)
}
}
ASSIGNMENT OPERATORS
ASSIGNMENT OPERATORS ARE USED TO ASSIGN VALUES TO VARIABLES. THEY CAN
ALSO COMBINE WITH ARITHMETIC OPERATORS.
public class AssignmentOperators {
public static void main(String[] args) {
int num = 10;

System.out.println("Initial value: " + num); // 10

num += 5; // num = num + 5


System.out.println("After += 5: " + num); // 15

num -= 3; // num = num - 3


System.out.println("After -= 3: " + num); // 12

num *= 2; // num = num * 2


System.out.println("After *= 2: " + num); // 24

num /= 4; // num = num / 4


System.out.println("After /= 4: " + num); // 6
}
}
UNARY OPERATORS
Unary operators work with only one operand, such as increment ++ and decrement
--.
public class UnaryOperators {
public static void main(String[] args) {
int num = 5;

System.out.println("Initial value: " + num); //


5
System.out.println("After ++num: " + (+
+num)); // Increment before use: 6
System.out.println("After num++: " + (num+
+)); // Use, then increment: 6
System.out.println("After num--: " +
(num--)); // Use, then decrement: 7
System.out.println("After --num: " + (--
num)); // Decrement before use: 5
}
}
CONDITIONAL (TERNARY) OPERATOR
The ternary operator is a shorthand for if-else statements.

public class TernaryOperator {


public static void main(String[] args) {
int a = 10, b = 20;

String result = (a > b) ? "a is greater" : "b is


greater";
System.out.println(result); // Output: b is
greater
}
}
BITWISE OPERATORS
BITWISE OPERATORS PERFORM OPERATIONS ON BINARY DIGITS.

public class BitwiseOperators {


public static void main(String[] args) {
int x = 5; // Binary: 0101
int y = 3; // Binary: 0011

System.out.println("x & y: " + (x & y)); // AND:


1
System.out.println("x | y: " + (x | y)); // OR: 7
System.out.println("x ^ y: " + (x ^ y)); // XOR:
6
System.out.println("~x: " + (~x)); // NOT:
-6
}
}
CONTROL STATEMENTS
CONTROL STATEMENTS DETERMINE THE FLOW OF EXECUTION IN A PROGRAM. THEY

INCLUDE DECISION-MAKING STATEMENTS, LOOPING STATEMENTS, AND JUMP

STATEMENTS.
DECISION-MAKING STATEMENTS (IF, IF-ELSE, SWITCH)
a) if Statement
The if statement runs a block of code only if a condition is true.

public class IfStatement {


public static void main(String[] args) {
int age = 18;

if (age >= 18) {


System.out.println("You are eligible to
vote.");
}
}
}
b) if-else
Statement
The if-else statement provides an alternate block of code to execute if the
condition is false.
public class IfElseStatement {
public static void main(String[] args) {
int number = 5;

if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
}
}
c) switch
Statement
The switch statement selects one block of code to execute from
multiple options.
public class SwitchStatement {
public static void main(String[] args) {
int day = 3;

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
}
}
}
JUMP STATEMENTS (BREAK, CONTINUE)
a) break Statement
The break statement stops the loop immediately.

public class BreakStatement {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("Count: " + i);
}
}
}
b) continue
Statement
The continue statement skips the current iteration and
moves to the next one.

public class ContinueStatement {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("Count: " + i);
}
}
}
QUESTIONS?

Thank you

You might also like