SlideShare a Scribd company logo
Java Programming
CONDITIONAL STATEMENT, JAVA LOOPING, AND JAVA ARRAY
Conditional Statements
Java Switch
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 The break and default keywords are optional, and will be described later in
this chapter
Java Switch
Syntax:
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Java Switch
The break Keyword
When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need
for more testing.
The default Keyword
The default keyword specifies some code to run if there is no case match.
JAVA If Else
Java Loop
Loops
Loops can execute a block of code as long as a specified condition is
reached.
Loops are handy because they save time, reduce errors, and they make
code more readable.
3 types of Loop in Java
 While Loop
 Do/While Loop
 For Loop
Java While Loop
The while loop loops through a block of code as long as a specified condition is true:
while (condition) {
// code block to be executed
}
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Java Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
do {
// code block to be executed
}
while (condition);
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
Java For Loop
When you know exactly how many times you want to loop through a block of code,
use the for loop instead of a while loop.
 Statement 1 is executed (one time) before the execution of the code block.
 Statement 2 defines the condition for executing the code block.
 Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Java For Loop
When you know exactly how many times you want to loop through a block of code,
use the for loop instead of a while loop.
 Statement 1 is executed (one time) before the execution of the code block.
 Statement 2 defines the condition for executing the code block.
 Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Java Array
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type with square brackets:
We have now declared a variable that holds an array of strings. To insert values to it, we
can use an array literal - place the values in a comma-separated list, inside curly braces:
To create an array of integers, you could write:
String[] cars;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
Java Array
Access the Elements of an Array
You access an array element by referring to the index number. This statement accesses
the value of the first element in cars:
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
Java Array
Change an Array Element
To change the value of a specific element, refer to the index number:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel"
System.out.println(cars[0]);
// Outputs Opel instead of Volvo
Java Array
Array Length
To find out how many elements an array has, use the length property:
Loop Through an Array
You can loop through the array elements with the for loop, and use the length
property to specify how many times the loop should run.
The following example outputs all elements in the cars array:
String[] cars = {"Volvo", "BMW","Ford","Mazda"};
System.out.println(cars.length)
;// Outputs 4
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
JAVA USER INPUT
Scanner
The Scanner class is used to get user input, and it is found in the java.util
package.
To use the Scanner class, create an object of the class and use any of the
available methods found in the Scanner class documentation. In our example,
we will use the nextLine() method, which is used to read Strings:
JAVA USER INPUT
import java.util.Scanner; // Import the Scanner class
class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = input.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}
}
JAVA USER INPUT
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user

More Related Content

PPTX
JAVA LOOP.pptx
PPTX
02 - Prepcode
PPT
Javatut1
PPT
Java tut1 Coderdojo Cahersiveen
PPT
Java tut1
PPT
Java tut1
PPT
Java Tutorial
JAVA LOOP.pptx
02 - Prepcode
Javatut1
Java tut1 Coderdojo Cahersiveen
Java tut1
Java tut1
Java Tutorial

Similar to JAVA LESSON-02.pptx (20)

PPT
Java_Tutorial_Introduction_to_Core_java.ppt
PPSX
Java Tutorial
PPT
Java teaching ppt for the freshers in colleeg.ppt
PPT
Java basic tutorial by sanjeevini india
PPT
Java basic tutorial by sanjeevini india
DOCX
Materials for teachers and students java-en
PPT
cse dse eee btech bsc and more to go on and on.ppt
PPT
Java Tutorial | My Heart
PPTX
Computational Problem Solving 016 (1).pptx
PPTX
Introduction to java
PPTX
Java introduction
ODP
Synapseindia reviews.odp.
PPTX
Introduction of basics loop and array
PPT
Tutorial java
PPT
Java Tutorial
PPT
Java tut1
PPT
Java Tut1
PPT
Unit I Advanced Java Programming Course
PPSX
DITEC - Programming with Java
PPTX
Java Programming
Java_Tutorial_Introduction_to_Core_java.ppt
Java Tutorial
Java teaching ppt for the freshers in colleeg.ppt
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Materials for teachers and students java-en
cse dse eee btech bsc and more to go on and on.ppt
Java Tutorial | My Heart
Computational Problem Solving 016 (1).pptx
Introduction to java
Java introduction
Synapseindia reviews.odp.
Introduction of basics loop and array
Tutorial java
Java Tutorial
Java tut1
Java Tut1
Unit I Advanced Java Programming Course
DITEC - Programming with Java
Java Programming
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Trump Administration's workforce development strategy
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Classroom Observation Tools for Teachers
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
Orientation - ARALprogram of Deped to the Parents.pptx
Cell Types and Its function , kingdom of life
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Trump Administration's workforce development strategy
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
A systematic review of self-coping strategies used by university students to ...
Microbial diseases, their pathogenesis and prophylaxis
Ad

JAVA LESSON-02.pptx

  • 1. Java Programming CONDITIONAL STATEMENT, JAVA LOOPING, AND JAVA ARRAY
  • 3. Java Switch  The switch expression is evaluated once.  The value of the expression is compared with the values of each case.  If there is a match, the associated block of code is executed.  The break and default keywords are optional, and will be described later in this chapter
  • 4. Java Switch Syntax: int day = 4; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; } switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
  • 5. Java Switch The break Keyword When Java reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing. The default Keyword The default keyword specifies some code to run if there is no case match.
  • 7. Java Loop Loops Loops can execute a block of code as long as a specified condition is reached. Loops are handy because they save time, reduce errors, and they make code more readable. 3 types of Loop in Java  While Loop  Do/While Loop  For Loop
  • 8. Java While Loop The while loop loops through a block of code as long as a specified condition is true: while (condition) { // code block to be executed } int i = 0; while (i < 5) { System.out.println(i); i++; }
  • 9. Java Do/While Loop The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. do { // code block to be executed } while (condition); int i = 0; do { System.out.println(i); i++; } while (i < 5);
  • 10. Java For Loop When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.  Statement 1 is executed (one time) before the execution of the code block.  Statement 2 defines the condition for executing the code block.  Statement 3 is executed (every time) after the code block has been executed. The example below will print the numbers 0 to 4: for (statement 1; statement 2; statement 3) { // code block to be executed } for (int i = 0; i < 5; i++) { System.out.println(i); }
  • 11. Java For Loop When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.  Statement 1 is executed (one time) before the execution of the code block.  Statement 2 defines the condition for executing the code block.  Statement 3 is executed (every time) after the code block has been executed. The example below will print the numbers 0 to 4: for (statement 1; statement 2; statement 3) { // code block to be executed } for (int i = 0; i < 5; i++) { System.out.println(i); }
  • 12. Java Array Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets: We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces: To create an array of integers, you could write: String[] cars; String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; int[] myNum = {10, 20, 30, 40};
  • 13. Java Array Access the Elements of an Array You access an array element by referring to the index number. This statement accesses the value of the first element in cars: Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc. String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars[0]); // Outputs Volvo
  • 14. Java Array Change an Array Element To change the value of a specific element, refer to the index number: String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; cars[0] = "Opel" System.out.println(cars[0]); // Outputs Opel instead of Volvo
  • 15. Java Array Array Length To find out how many elements an array has, use the length property: Loop Through an Array You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. The following example outputs all elements in the cars array: String[] cars = {"Volvo", "BMW","Ford","Mazda"}; System.out.println(cars.length) ;// Outputs 4 String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < cars.length; i++) { System.out.println(cars[i]); }
  • 16. JAVA USER INPUT Scanner The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:
  • 17. JAVA USER INPUT import java.util.Scanner; // Import the Scanner class class MyClass { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = input.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input } }
  • 18. JAVA USER INPUT Method Description nextBoolean() Reads a boolean value from the user nextByte() Reads a byte value from the user nextDouble() Reads a double value from the user nextFloat() Reads a float value from the user nextInt() Reads a int value from the user nextLine() Reads a String value from the user nextLong() Reads a long value from the user nextShort() Reads a short value from the user