0% found this document useful (0 votes)
18 views54 pages

Java Loops and Control Statements Guide

The document provides an overview of loops in Java, detailing their types including for, while, and do-while loops, as well as nested and infinite loops. It explains loop control statements like break and continue, and includes examples of various programming tasks such as calculating GCD, displaying Fibonacci series, and identifying palindrome and Armstrong numbers. Additionally, it presents different patterns that can be generated using loops.

Uploaded by

cfcl365
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views54 pages

Java Loops and Control Statements Guide

The document provides an overview of loops in Java, detailing their types including for, while, and do-while loops, as well as nested and infinite loops. It explains loop control statements like break and continue, and includes examples of various programming tasks such as calculating GCD, displaying Fibonacci series, and identifying palindrome and Armstrong numbers. Additionally, it presents different patterns that can be generated using loops.

Uploaded by

cfcl365
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Loops

Rakib Mahmud

07/01/2025 Rakib Mahmud 1


Introduction
• 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.

07/01/2025 Rakib Mahmud 2


Introduction (Cont’d)

07/01/2025 Rakib Mahmud 3


Types of Loops
• In Java, there are three types of Loops, which are listed
below:
• for loop
• while loop
• do-while loop

07/01/2025 Rakib Mahmud 4


For loop
• The for loop is used when we know the number of
iterations (we know how many times we want to repeat
a task).
Flowchart

07/01/2025 Rakib Mahmud 5


For loop (Cont’d)

07/01/2025 Rakib Mahmud 6


For loop (Ex:1) (Cont’d)

07/01/2025 Rakib Mahmud 7


For loop (Ex:2) (Cont’d)

07/01/2025 Rakib Mahmud 8


While loop
• The while loop loops through a block of code as long as
a specified condition is true.

Flowchart

07/01/2025 Rakib Mahmud 9


While loop (Cont’d)

07/01/2025 Rakib Mahmud 10


While loop (Ex:1) (Cont’d)

07/01/2025 Rakib Mahmud 11


do-while Loop
• The do-while loop ensures that the code block
executes at least once before checking the condition.

Flowchart

07/01/2025 Rakib Mahmud 12


do-while Loop (Cont’d)

07/01/2025 Rakib Mahmud 13


do-while Loop (Ex:1) (Cont’d)

07/01/2025 Rakib Mahmud 14


do-while Loop (Ex:2) (Cont’d)

07/01/2025 Rakib Mahmud 15


Printing (1 to 100) with Various
Loops
for while do while

07/01/2025 Rakib Mahmud 16


Nested loops
• It is also possible to place a loop inside another loop.
This is called a nested loop.
• The "inner loop" will be executed one time for each
iteration of the "outer loop”.

07/01/2025 Rakib Mahmud 17


Nested loops (Cont’d)

07/01/2025 Rakib Mahmud 18


Infinite Loop
• An infinite loop is executed when the test expression
never becomes false and the body of the loop is
executed repeatedly.
• A program is stuck in an Infinite loop when the condition
is always true.

07/01/2025 Rakib Mahmud 19


Infinite Loop (Ex:1) (Cont’d)

07/01/2025 Rakib Mahmud 20


Infinite Loop (Ex:2) (Cont’d)

07/01/2025 Rakib Mahmud 21


Loop Control Statements
Name Description

break statement The break statement is used to


terminate the switch and loop
statement. (loop terminate)

continue statement This statement skips the rest of


the loop statement and starts
the next iteration of the loop to
take place. (bypass)

07/01/2025 Rakib Mahmud 22


break Statement

07/01/2025 Rakib Mahmud 23


continue Statement

07/01/2025 Rakib Mahmud 24


Display Mulitiplication Table

07/01/2025 Rakib Mahmud 25


Find Sum of n Numbers ( 1+ 2 + 3 +
…….+ n)

07/01/2025 Rakib Mahmud 26


Factorial of a Number (1 * 2 * 3 * …..
* n)

07/01/2025 Rakib Mahmud 27


Display Fibonacci Series ( 0 1 1 2 3
……n )

07/01/2025 Rakib Mahmud 28


Count Digits of a Number

07/01/2025 Rakib Mahmud 29


Find Sum of the Digits of a Number

07/01/2025 Rakib Mahmud 30


Reverse a Number

07/01/2025 Rakib Mahmud 31


Palindrome Number
• A palindrome number is a number that remains the
same when its digits are reversed.
• For example: 525, 12321

07/01/2025 Rakib Mahmud 32


Check a Number is Palindrome or
Not

07/01/2025 Rakib Mahmud 33


Armstrong Number
• An armstrong number (also known as a Narcissistic
number) for a given number of digits is a number that is
equal to the sum of its own digits each raised to the
power of the number of digits.
• For example:
• 153 is an Armstrong number because 153 = 13+53+33.
• 1634 is an Armstrong number because 1634 = 14+64+34+44 = 1 + 1296
+ 81 + 256

07/01/2025 Rakib Mahmud 34


Finding a Number is Armstrong or
Not

07/01/2025 Rakib Mahmud 35


Display a Number is Words Even
with Tailing 0

07/01/2025 Rakib Mahmud 36


GCD and LCM
• GCD = Greatest common divisor
• LCM = Least common multiple
• num1= 60 = 2 * 2 * 3 * 5
n1 n2 n1%n2
• num2 = 24 = 2 * 2 * 2 *3
60 24 12
• GCD ( 60, 24 ) = 12 24 12 0
• LCM (60, 24) = = = 120 12 0

07/01/2025 Rakib Mahmud 37


Java Program to find GCD and LCM
of Two Integers

07/01/2025 Rakib Mahmud 38


Flowchart to Check If a Number Is
Prime, Co-prime or Not
Star
t
Input
num Yes Print co
num==1
No prime
num==2 || num Yes Print
==3 prime
No
i=2, count=0
No
Yes
Count=
i<=(num/2)
=0
Yes
Num
No %i==0 No
Yes
Print not Print
count=1 prime
prime

i= i+1
End
07/01/2025 Rakib Mahmud 39
Java Program to Check If a Number
Is Prime, Co-prime or Not

07/01/2025 Rakib Mahmud 40


Pattern Type-1 (Right Angle Triangle)
1 1 A *
12 10 AB **
123 101 ABC ***
1234 1010 ABCD ****
Pattern 1 (N =4) Pattern 2 (N =4) Pattern 3 (N =4) Pattern 4 (N =4)

1 1 A #
22 00 BB ##
333 111 CCC ###
4444 0000 DDDD ####
Pattern 5 (N =4) Pattern 6 (N =4) Pattern 7 (N =4) Pattern 8 (N =4)

07/01/2025 Rakib Mahmud 41


Pattern1 from Pattern Type1

1
12
123
1234 col1
row1 1 col2
row2 1 2 col3
row3 1 2 3 col4
row4 1 2 3 4

07/01/2025 Rakib Mahmud 42


Java Program for Pattern1 from
Pattern Type1

07/01/2025 Rakib Mahmud 43


Java Program for Pattern 5 from
Pattern Type1

07/01/2025 Rakib Mahmud 44


Java Program for Pattern 2 from
Pattern Type1

07/01/2025 Rakib Mahmud 45


Java Program for Pattern 6 from
Pattern Type1

07/01/2025 Rakib Mahmud 46


Java Program for Pattern 3 from
Pattern Type1

07/01/2025 Rakib Mahmud 47


Java Program for Pattern 7 from
Pattern Type1

07/01/2025 Rakib Mahmud 48


Java Program for Pattern 4 from
Pattern Type1

07/01/2025 Rakib Mahmud 49


Java Program for Pattern 8 from
Pattern Type1

07/01/2025 Rakib Mahmud 50


Pattern Type 2

1 1 A *
1 1 A **
2 0 B ***
123 101 ABC ****
121
Pattern 3 (N
4 =4) 101
Pattern 20
(N =4) ABC
Pattern 3D
(N =4) Pattern 4 (N =4)

#
1 #
1 A
22 #
00 BB
333 ##
111 CCC
4444 #
0000 DDDD
Pattern 5 (N =4) Pattern 6 (N =4) Pattern 7 (N =4) ###
Pattern 8 (N =4)
#

07/01/2025 Rakib Mahmud 51


Pattern1 from Pattern Type2

1
12
123 col1 col2 col3 col4
1234 row1 1
row2 1 2
row3 1 2 3
row4 1 2 3 4

Space = n-row

07/01/2025 Rakib Mahmud 52


Java Program for Pattern 1 from
Pattern Type2

07/01/2025 Rakib Mahmud 53


Thank You

07/01/2025 Rakib Mahmud 54

You might also like