SlideShare a Scribd company logo
JAVA SE
(CORE JAVA)
LECTURE-10
Today’s Agenda
 Loop structures and it’s types.
 while and do-while loop.
 for loop.
 Labeled break and continue.
Loop Structure
Types
 Loops in java also are control statements used for repeating
a set of statements multiple times.
 They are broadly categorized to be of 2 types :-
 Entry controlled – Condition is checked when the control enters loop
body. Example, while and for loop.
 Exit controlled – Condition is checked after the flow enters loop
body. Example, do-while loop.
while loop
 Syntax :-
while(test condition)
{
----
----
----
}
* The loop continues until the condition is true, as
soon as the condition goes false flow exits the loop
body.
true
false
Exercise 1
 WAP to accept an integer from the user and print its
factorial. Make sure that your program should print 1 if 0 is
entered ?
 Sample output :-
Solution Ex 1
import java.util.*;
class Factorial
{
public static void main(String[ ]
args)
{
Scanner kb=new
Scanner(System.in);
int n, f=1;
System.out.println("Enter a
no");
n=kb.nextInt();
while(n>=1)
{
f=f*n;
n--;
}
System.out.println("Factorial is
"+f);
}
}
do-while loop
 Syntax :-
do
{
----
----
}while(test condition);
* Since, the condition is tested at exit, so the loop body
will execute at least once irrespective of the
condition.
True
False
Exercise 2
 WAP to accept two integer from the user and
display their sum. Now ask the user whether he/she
wants to continue or not. If the answer is Yes then
again repeat the process otherwise terminate the
program displaying the message “Thank you”
 Sample Output :-
Solution Ex 2
class AddNos
{
public static void main(String []
args)
{
java.util.Scanner kb=new
java.util.Scanner(System.in);
int a,b;
String choice;
do
{
System.out.println("Enter two
integers");
a=kb.nextInt();
b=kb.nextInt();
System.out.println("Sum is” "+
(a+b));
System.out.println("Try
again?(Y/N)");
choice=kb.next( );
}while(choice.equalsIgnoreCase("Y
"));
System.out.println("Thank you");
}
}
Can we replace next() with
nextLine() ???
 What happens when we do so ???
 Can you notice something unusual in this output…
 Instead of asking for further input for Y or N, the further
statements execute. Why does this happen?
 This is because nextLine() method reads, “enter” as an input
from the keyboard’s buffer.
Buffer
 Buffer is a region of a physical memory storage used to
temporarily store data while it is being moved from one
place to another.
 So, in above case the keyboard’s buffer is left with an
“ENTER KEY” which we pressed after inputting the
second integer, which nextLine() accepts as an input.
 How can we solve this???...
 By calling nextLine() before accepting actual input. Since,
this call would clean the buffer.
Solution Ex 2
class AddNos
{
public static void main(String [] args)
{
java.util.Scanner kb=new
java.util.Scanner(System.in);
int a,b;
String choice;
do
{
System.out.println("Enter two integers");
a=kb.nextInt();
b=kb.nextInt();
System.out.println("Sum is "+(a+b));
System.out.println("Try again?(Y/N)");
nextLine( );
choice=kb.nextLine( );
}while(choice.equalsIgnoreCase("Y"));
System.out.println("Thank you");
}
}
for and labeled for
loop
 Syntax :-
for(initialization; test condition; statement)
{
----
----
----
}
* The initialization and statement part can be left
blank.
true
false
break and continue
 To terminate the loop and exit its body providing a condition
before it, in such cases we use the statement break.
 In situations where we want to skip further steps in a loop
and move directly back to the test condition, there we use the
statement continue.
 Let us understand these through an example.
 WAP to accept an integer from user & check
whether it is prime or not ?
Solution
Exercise
 Write a program to continuously accept integers from the
user and as soon as he inputs 0 then display the sum of all the
nos given before 0.
Exercise
 Write a program to accept an integer from the user calculate
and print the sum of it’s digits . For example if input is 75 then
output should be 12.
Write a program to accept an integer from the user calculate
and print the sum of it’s first and last digit only . For example if
input is 21 75 then output should be 7.
Write a program to accept an integer from the user and print
it’s reverse. For example if input is 451 the output should be
154.
Exercise
 Write a program to accept an integer from the user and check
whether it is prime or not.
Write a program to accept an integer from the user and print it’s
table up to 10 terms.
OUTPUT:
Enter a no: 7
Table of 7
7*1=7
7*2=14
7*3=21
.
.
7*10=70
Nested Loop
 Syntax :-
for(init;condition;stmt)
{
for(init;condition;stmt)
{
-----
-----
}
}
* After completing the inner loop, the control moves back to the
statement part of outer loop.
Labeled break and
continue
 Since, the break condition brings us out of the loop body but in case
of nested loop if we want to completely come out of the loop, then
we will use labeled break statement.
 Syntax :-
<label name>:
for(init;condition;stmt)
{
for(init;condition;stmt)
{
-----
break <label name>;
}
}
Exercise
 WAP to accept an int from user and print sum of its digits?
 WAP to accept an int from user and check whether it is
armstrong or not ?
 WAP to accept an int from user & print its reverse ?
 WAP to accept an integer from user and check whether it is
equal to its reverse or not ?
Exercise
 Write a menu driven program in which you will provide 4
choices to the user :-
1.Factorial.
2.Prime
3.Even/Odd.
4.Quit.
The program should perform further actions and according to
choice selected by the user. Program should only terminate
when user choose choice code?
End Of Lecture 10
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533
Agenda for Next Lecture:
1. Arrays in Java
2. Single Dimensional array
3. Using length property of array
4. Multi Dimensional array
5. How JVM handles dynamic blocks

More Related Content

PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPTX
Pi j1.4 loops
PPTX
dizital pods session 5-loops.pptx
PPTX
Java chapter 3
PDF
how to write loops in java explained vividly
PPTX
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
PPTX
Control statements in java
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Pi j1.4 loops
dizital pods session 5-loops.pptx
Java chapter 3
how to write loops in java explained vividly
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
Control statements in java
Unit-02 Selection, Mathematical Functions and loops.pptx

Similar to Java covers syntax, data types, object-oriented concepts, control flow, exception handling, multithreading (20)

PPT
Comp102 lec 6
PPT
Java Programming: Loops
PPTX
PPT
Computer Programming, Loops using Java
PPTX
for loop in java
PPT
Control statements
PDF
Cis 1403 lab5_loops
PPT
Chap05
PPTX
07 flow control
PPTX
Lecture - 5 Control Statement
PDF
DSA 103 Object Oriented Programming :: Week 3
PPTX
130707833146508191
PPTX
Computer programming 2 Lesson 8
PPT
9781439035665 ppt ch05
PPT
Control structures ii
PPT
Ch4_part1.ppt
PPT
C Programming Looping Statements For Students
PPT
Ch4_part1.ppt
PPT
Lecture on repetition statements (loops)
PPT
object orineted programming looping .ppt
Comp102 lec 6
Java Programming: Loops
Computer Programming, Loops using Java
for loop in java
Control statements
Cis 1403 lab5_loops
Chap05
07 flow control
Lecture - 5 Control Statement
DSA 103 Object Oriented Programming :: Week 3
130707833146508191
Computer programming 2 Lesson 8
9781439035665 ppt ch05
Control structures ii
Ch4_part1.ppt
C Programming Looping Statements For Students
Ch4_part1.ppt
Lecture on repetition statements (loops)
object orineted programming looping .ppt
Ad

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Teaching material agriculture food technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Machine Learning_overview_presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Cloud computing and distributed systems.
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Teaching material agriculture food technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
NewMind AI Weekly Chronicles - August'25-Week II
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
A comparative analysis of optical character recognition models for extracting...
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Machine Learning_overview_presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Cloud computing and distributed systems.
Spectroscopy.pptx food analysis technology
Digital-Transformation-Roadmap-for-Companies.pptx
Ad

Java covers syntax, data types, object-oriented concepts, control flow, exception handling, multithreading

  • 2. Today’s Agenda  Loop structures and it’s types.  while and do-while loop.  for loop.  Labeled break and continue.
  • 3. Loop Structure Types  Loops in java also are control statements used for repeating a set of statements multiple times.  They are broadly categorized to be of 2 types :-  Entry controlled – Condition is checked when the control enters loop body. Example, while and for loop.  Exit controlled – Condition is checked after the flow enters loop body. Example, do-while loop.
  • 4. while loop  Syntax :- while(test condition) { ---- ---- ---- } * The loop continues until the condition is true, as soon as the condition goes false flow exits the loop body. true false
  • 5. Exercise 1  WAP to accept an integer from the user and print its factorial. Make sure that your program should print 1 if 0 is entered ?  Sample output :-
  • 6. Solution Ex 1 import java.util.*; class Factorial { public static void main(String[ ] args) { Scanner kb=new Scanner(System.in); int n, f=1; System.out.println("Enter a no"); n=kb.nextInt(); while(n>=1) { f=f*n; n--; } System.out.println("Factorial is "+f); } }
  • 7. do-while loop  Syntax :- do { ---- ---- }while(test condition); * Since, the condition is tested at exit, so the loop body will execute at least once irrespective of the condition. True False
  • 8. Exercise 2  WAP to accept two integer from the user and display their sum. Now ask the user whether he/she wants to continue or not. If the answer is Yes then again repeat the process otherwise terminate the program displaying the message “Thank you”  Sample Output :-
  • 9. Solution Ex 2 class AddNos { public static void main(String [] args) { java.util.Scanner kb=new java.util.Scanner(System.in); int a,b; String choice; do { System.out.println("Enter two integers"); a=kb.nextInt(); b=kb.nextInt(); System.out.println("Sum is” "+ (a+b)); System.out.println("Try again?(Y/N)"); choice=kb.next( ); }while(choice.equalsIgnoreCase("Y ")); System.out.println("Thank you"); } } Can we replace next() with nextLine() ???
  • 10.  What happens when we do so ???  Can you notice something unusual in this output…  Instead of asking for further input for Y or N, the further statements execute. Why does this happen?  This is because nextLine() method reads, “enter” as an input from the keyboard’s buffer.
  • 11. Buffer  Buffer is a region of a physical memory storage used to temporarily store data while it is being moved from one place to another.  So, in above case the keyboard’s buffer is left with an “ENTER KEY” which we pressed after inputting the second integer, which nextLine() accepts as an input.  How can we solve this???...  By calling nextLine() before accepting actual input. Since, this call would clean the buffer.
  • 12. Solution Ex 2 class AddNos { public static void main(String [] args) { java.util.Scanner kb=new java.util.Scanner(System.in); int a,b; String choice; do { System.out.println("Enter two integers"); a=kb.nextInt(); b=kb.nextInt(); System.out.println("Sum is "+(a+b)); System.out.println("Try again?(Y/N)"); nextLine( ); choice=kb.nextLine( ); }while(choice.equalsIgnoreCase("Y")); System.out.println("Thank you"); } }
  • 13. for and labeled for loop  Syntax :- for(initialization; test condition; statement) { ---- ---- ---- } * The initialization and statement part can be left blank. true false
  • 14. break and continue  To terminate the loop and exit its body providing a condition before it, in such cases we use the statement break.  In situations where we want to skip further steps in a loop and move directly back to the test condition, there we use the statement continue.  Let us understand these through an example.  WAP to accept an integer from user & check whether it is prime or not ?
  • 16. Exercise  Write a program to continuously accept integers from the user and as soon as he inputs 0 then display the sum of all the nos given before 0.
  • 17. Exercise  Write a program to accept an integer from the user calculate and print the sum of it’s digits . For example if input is 75 then output should be 12. Write a program to accept an integer from the user calculate and print the sum of it’s first and last digit only . For example if input is 21 75 then output should be 7. Write a program to accept an integer from the user and print it’s reverse. For example if input is 451 the output should be 154.
  • 18. Exercise  Write a program to accept an integer from the user and check whether it is prime or not. Write a program to accept an integer from the user and print it’s table up to 10 terms. OUTPUT: Enter a no: 7 Table of 7 7*1=7 7*2=14 7*3=21 . . 7*10=70
  • 19. Nested Loop  Syntax :- for(init;condition;stmt) { for(init;condition;stmt) { ----- ----- } } * After completing the inner loop, the control moves back to the statement part of outer loop.
  • 20. Labeled break and continue  Since, the break condition brings us out of the loop body but in case of nested loop if we want to completely come out of the loop, then we will use labeled break statement.  Syntax :- <label name>: for(init;condition;stmt) { for(init;condition;stmt) { ----- break <label name>; } }
  • 21. Exercise  WAP to accept an int from user and print sum of its digits?  WAP to accept an int from user and check whether it is armstrong or not ?  WAP to accept an int from user & print its reverse ?  WAP to accept an integer from user and check whether it is equal to its reverse or not ?
  • 22. Exercise  Write a menu driven program in which you will provide 4 choices to the user :- 1.Factorial. 2.Prime 3.Even/Odd. 4.Quit. The program should perform further actions and according to choice selected by the user. Program should only terminate when user choose choice code?
  • 23. End Of Lecture 10 For any queries mail us @: scalive4u@gmail.com Call us @ : 0755-4271659, 7879165533 Agenda for Next Lecture: 1. Arrays in Java 2. Single Dimensional array 3. Using length property of array 4. Multi Dimensional array 5. How JVM handles dynamic blocks