SlideShare ist ein Scribd-Unternehmen logo
1 von 35
JAVA LOOP
• In computer programming, loops are used to repeat a block of
code. For example, if you want to show a message 100 times,
then rather than typing the same code .
Types of LOOPS
• in Java, there are three types of loops: for, while, and do-
while. The for loop is used for a known number of iterations, the
while loop is used for an unknown number of iterations based
on a condition, and the do-while loop is similar to the while loop,
but the code block is executed at least onceV
For loop
• Java for loop is used to run a block of code for a certain number of
times. The syntax of for loop is:
for (initialExpression; testExpression; updateExpression) {
// body of the loop
}
For loop
The initial expression initializes
and/or declares variables and
executes only once.
The condition is evaluated. If the
condition is true, the body of the
for loop is executed.
The update Expression updates the
value of initialExpression.
The condition is evaluated again.
The process continues until the
condition is false.
Example 1:
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println("Java is fun");
Output:
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
Example2:
Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
Output:
1
2
3…. 10
Java while loop
• The Java while loop is used to iterate a part of
the program repeatedly until the specified Boolean condition is
true. As soon as the Boolean condition becomes false, the loop
automatically stops.
• The while loop is considered as a repeating if statement. If the
number of iteration is not fixed, it is recommended to use the
while loop.
While syntax
while (condition){
//code to be executed
Increment / decrement statement
}
example1
1. int i=1;
2. while(i<=10){
3. System.out.println(i);
4. i++;
5. }
Output:
1
2
3... 10
Java while loop
Java while loop is used to run a specific code until a certain condition is
met. The syntax of the while loop is:
A while loop evaluates the textExpression inside the parenthesis ().
If the textExpression evaluates to true, the code inside the while loop is
executed.
The textExpression is evaluated again.
This process continues until the textExpression is false.
When the textExpression evaluates to false, the loop stops.
To learn more about the conditions, visit Java relational and logical
operators.
Example 1:
// declare variables
int i = 1, n = 5;
// while loop from 1 to 5
while(i <= n) {
System.out.println(i);
i++;
Output:
1
2
3…5
Syntax
while (condition){
//code to be executed
Increment / decrement statement
}
Example 1:
1. int i=1;
2. while(i<=10){
3. System.out.println(i);
4. i++;
5. }
Java do-while Loop
• The Java do-while loop is used to iterate a part of the program
repeatedly, until the specified condition is true. If the number of
iteration is not fixed and you must have to execute the loop at
least once, it is recommended to use a do-while loop.
• Java do-while loop is called an exit control loop. Therefore,
unlike while loop and for loop, the do-while check the condition
at the end of loop body. The Java do-while loop is executed at
least once because condition is checked after loop body.
Syntax
1.do{
2.//code to be executed / loop body
3.//update statement
4.}while (condition);
1.int i=1;
2. do{
3. System.out.println(i);
4. i++;
5. }while(i<=10);
Output:
1
2
3…10
Java Arrays
• 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:
Array
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
Example 1
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
}
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
Output:
Volvo", "BMW", "Ford", "Mazd
JAVA METHOD
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known as
functions.
• Why use methods? To reuse code: define the code once, and use it many
times.
Create a Method
• A method must be declared within a class. It is defined with the name of
the method, followed by parentheses (). Java provides some pre-defined
methods, such as System.out.println(), but you can also create your own
methods to perform certain actions:
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7
Scanner input = new Scanner (System.in);
double totalprice=0;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i=0; i <cars.length ; i++)
{
System.out.println(cars[i]);
System.out.println("Enter price:");
double price = input.nextDouble();
totalprice=(totalprice + price);
}
System.out.println("TOTAL PRICE:" + totalprice);
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
Example Explained
myMethod() is the name of the method
static means that the method belongs to the
Main class and not an object of the Main class.
You will learn more about objects and how to
access methods through objects later in this
tutorial.
void means that this method does not have a
return value. You will learn more about return
values later in this chapter
Call a Method
To call a method in Java, write the method's name followed by two
parentheses () and a semicolon;
In the following example, myMethod() is used to print a text (the
action), when it is called:
public class Main {
static void myMethod()
{
System.out.println("I just got executed!");
}
public static void main(String[] args)
{
myMethod();
}
}
Inside main, call the myMethod() method:
Creating your own Classes
At the end of the lesson, the student should be able to:
• ● Create their own classes
• ● Declare attributes and methods for their classes
• ● Use the this reference to access instance data
• ● Create and call overloaded methods
• ● Import and create packages
• ● Use access modifiers to control access to class members
•
Java Classes/Objects
• Java is an object-oriented programming language.
• Everything in Java is associated with classes and
objects, along with its attributes and methods. For
example: in real life, a car is an object. The car
has attributes, such as weight and color,
and methods, such as drive and brake.
• A Class is like an object constructor, or a "blueprint" for
creating objects.
A NetBeans project contains the classes needed to build a Java
application. The application uses the main class as the starting point
for the execution of the Java code. In fact, in a new Java application
project created by NetBeans only one class included - the main class
contained within the Main. java file.DELA PEÑA, CARL CUEVAS?
What is the main class in
NetBeans?
get method
The get method returns the value of the variable name . The
set method takes a parameter ( newName ) and assigns it to
the name variable. The this keyword is used to refer to the
current object.
get method
• The get method returns the value of the variable name . The set
method takes a parameter ( newName ) and assigns it to the
name variable. The this keyword is used to refer to the current
object.
• Defining your own classes
• Coding Guidelines
• Declaring Attributes
• Instance Variables
• Coding Guidelines
• Class (static) variables
• Declaring Methods
• Accessor Methods
• Mutator Methods
• Multiple return statements
• Static Methods
• Coding Guidelines
• “this” reference
• Overloading Methods
• Constructors
• Default Constructor
• Overloading Constructors
• Using Constructors
• “this()” constructor call
• Packages
• Importing Packages
• Creating Packages
• Setting the CLASSPATH
• Access Modifiers
• default accessibility
• public accessibility
• protected accessibility
• private accessibility
• Coding Guidelines

Weitere ähnliche Inhalte

Was ist angesagt?

Data types in java
Data types in javaData types in java
Data types in javaHarshitaAshwani
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP FunctionJalpesh Vasa
 
Introduction to vb.net
Introduction to vb.netIntroduction to vb.net
Introduction to vb.netJaya Kumari
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCFAKHRUN NISHA
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Php Simple Xml
Php Simple XmlPhp Simple Xml
Php Simple Xmlmussawir20
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019Samir Mohanty
 
Html 5 tutorial - By Bally Chohan
Html 5 tutorial - By Bally ChohanHtml 5 tutorial - By Bally Chohan
Html 5 tutorial - By Bally Chohanballychohanuk
 
Java script array
Java script arrayJava script array
Java script arraychauhankapil
 
Python Generators
Python GeneratorsPython Generators
Python GeneratorsAkshar Raaj
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming conceptsrahuld115
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaEdureka!
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 

Was ist angesagt? (20)

Data types in java
Data types in javaData types in java
Data types in java
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Introduction to vb.net
Introduction to vb.netIntroduction to vb.net
Introduction to vb.net
 
Html forms
Html formsHtml forms
Html forms
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
Python functions
Python functionsPython functions
Python functions
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Java loops
Java loopsJava loops
Java loops
 
Php Simple Xml
Php Simple XmlPhp Simple Xml
Php Simple Xml
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Html 5 tutorial - By Bally Chohan
Html 5 tutorial - By Bally ChohanHtml 5 tutorial - By Bally Chohan
Html 5 tutorial - By Bally Chohan
 
Java script array
Java script arrayJava script array
Java script array
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python Generators
Python GeneratorsPython Generators
Python Generators
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 
Variables in python
Variables in pythonVariables in python
Variables in python
 

Ähnlich wie JAVA LOOP.pptx

Ähnlich wie JAVA LOOP.pptx (20)

javaloop understanding what is java.pptx
javaloop understanding what is java.pptxjavaloop understanding what is java.pptx
javaloop understanding what is java.pptx
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
JAVA LESSON-02.pptx
JAVA LESSON-02.pptxJAVA LESSON-02.pptx
JAVA LESSON-02.pptx
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx
 

KĂźrzlich hochgeladen

Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLimonikaupta
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)Damian Radcliffe
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445ruhi
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxellan12
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.soniya singh
 
Networking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGNetworking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGAPNIC
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...Neha Pandey
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebJames Anderson
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024APNIC
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Servicegwenoracqe6
 
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$kojalkojal131
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Delhi Call girls
 

KĂźrzlich hochgeladen (20)

@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Networking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGNetworking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOG
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 

JAVA LOOP.pptx

  • 1. JAVA LOOP • In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code .
  • 2. Types of LOOPS • in Java, there are three types of loops: for, while, and do- while. The for loop is used for a known number of iterations, the while loop is used for an unknown number of iterations based on a condition, and the do-while loop is similar to the while loop, but the code block is executed at least onceV
  • 3. For loop • Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is: for (initialExpression; testExpression; updateExpression) { // body of the loop }
  • 4. For loop The initial expression initializes and/or declares variables and executes only once. The condition is evaluated. If the condition is true, the body of the for loop is executed. The update Expression updates the value of initialExpression. The condition is evaluated again. The process continues until the condition is false.
  • 5. Example 1: int n = 5; // for loop for (int i = 1; i <= n; ++i) { System.out.println("Java is fun"); Output: Java is fun Java is fun Java is fun Java is fun Java is fun
  • 6. Example2: Java for loop for(int i=1;i<=10;i++){ System.out.println(i); } Output: 1 2 3…. 10
  • 7. Java while loop • The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops. • The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while loop.
  • 8. While syntax while (condition){ //code to be executed Increment / decrement statement }
  • 9. example1 1. int i=1; 2. while(i<=10){ 3. System.out.println(i); 4. i++; 5. } Output: 1 2 3... 10
  • 10. Java while loop Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is:
  • 11. A while loop evaluates the textExpression inside the parenthesis (). If the textExpression evaluates to true, the code inside the while loop is executed. The textExpression is evaluated again. This process continues until the textExpression is false. When the textExpression evaluates to false, the loop stops. To learn more about the conditions, visit Java relational and logical operators.
  • 12. Example 1: // declare variables int i = 1, n = 5; // while loop from 1 to 5 while(i <= n) { System.out.println(i); i++; Output: 1 2 3…5
  • 13. Syntax while (condition){ //code to be executed Increment / decrement statement }
  • 14. Example 1: 1. int i=1; 2. while(i<=10){ 3. System.out.println(i); 4. i++; 5. }
  • 15. Java do-while Loop • The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop. • Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the do-while check the condition at the end of loop body. The Java do-while loop is executed at least once because condition is checked after loop body.
  • 16. Syntax 1.do{ 2.//code to be executed / loop body 3.//update statement 4.}while (condition);
  • 17. 1.int i=1; 2. do{ 3. System.out.println(i); 4. i++; 5. }while(i<=10); Output: 1 2 3…10
  • 18. Java Arrays • 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:
  • 19. Array String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; To create an array of integers, you could write: int[] myNum = {10, 20, 30, 40};
  • 20. Example 1 public static void main(String[] args) { String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars[0]); }
  • 21. String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; cars[0] = "Opel"; System.out.println(cars[0]);
  • 22. String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < cars.length; i++) { System.out.println(cars[i]); Output: Volvo", "BMW", "Ford", "Mazd
  • 23. JAVA METHOD • A method is a block of code which only runs when it is called. • You can pass data, known as parameters, into a method. • Methods are used to perform certain actions, and they are also known as functions. • Why use methods? To reuse code: define the code once, and use it many times. Create a Method • A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also create your own methods to perform certain actions:
  • 24. int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; System.out.println(myNumbers[1][2]); // Outputs 7
  • 25. Scanner input = new Scanner (System.in); double totalprice=0; String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i=0; i <cars.length ; i++) { System.out.println(cars[i]); System.out.println("Enter price:"); double price = input.nextDouble(); totalprice=(totalprice + price); } System.out.println("TOTAL PRICE:" + totalprice);
  • 26. int[] age = {12, 4, 5, 2, 5}; // access each array elements System.out.println("Accessing Elements of Array:"); System.out.println("First Element: " + age[0]); System.out.println("Second Element: " + age[1]); System.out.println("Third Element: " + age[2]); System.out.println("Fourth Element: " + age[3]); System.out.println("Fifth Element: " + age[4]); }
  • 27. Example Explained myMethod() is the name of the method static means that the method belongs to the Main class and not an object of the Main class. You will learn more about objects and how to access methods through objects later in this tutorial. void means that this method does not have a return value. You will learn more about return values later in this chapter
  • 28. Call a Method To call a method in Java, write the method's name followed by two parentheses () and a semicolon; In the following example, myMethod() is used to print a text (the action), when it is called:
  • 29. public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } Inside main, call the myMethod() method:
  • 30. Creating your own Classes At the end of the lesson, the student should be able to: • ● Create their own classes • ● Declare attributes and methods for their classes • ● Use the this reference to access instance data • ● Create and call overloaded methods • ● Import and create packages • ● Use access modifiers to control access to class members •
  • 31. Java Classes/Objects • Java is an object-oriented programming language. • Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. • A Class is like an object constructor, or a "blueprint" for creating objects.
  • 32. A NetBeans project contains the classes needed to build a Java application. The application uses the main class as the starting point for the execution of the Java code. In fact, in a new Java application project created by NetBeans only one class included - the main class contained within the Main. java file.DELA PEÑA, CARL CUEVAS? What is the main class in NetBeans?
  • 33. get method The get method returns the value of the variable name . The set method takes a parameter ( newName ) and assigns it to the name variable. The this keyword is used to refer to the current object.
  • 34. get method • The get method returns the value of the variable name . The set method takes a parameter ( newName ) and assigns it to the name variable. The this keyword is used to refer to the current object.
  • 35. • Defining your own classes • Coding Guidelines • Declaring Attributes • Instance Variables • Coding Guidelines • Class (static) variables • Declaring Methods • Accessor Methods • Mutator Methods • Multiple return statements • Static Methods • Coding Guidelines • “this” reference • Overloading Methods • Constructors • Default Constructor • Overloading Constructors • Using Constructors • “this()” constructor call • Packages • Importing Packages • Creating Packages • Setting the CLASSPATH • Access Modifiers • default accessibility • public accessibility • protected accessibility • private accessibility • Coding Guidelines