KEMBAR78
JAVA - Basic - Part 5 - Looping | PDF | Control Flow | Programming Paradigms
0% found this document useful (0 votes)
6 views33 pages

JAVA - Basic - Part 5 - Looping

This lesson plan covers basic Java programming focusing on looping statements, including while, do-while, for, and enhanced for loops. It also addresses digit manipulation techniques, nested loops, and various pattern printing methods. Additionally, it provides practice problems and links to further resources for students to enhance their understanding of these concepts.

Uploaded by

Yadhavan PS-1
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)
6 views33 pages

JAVA - Basic - Part 5 - Looping

This lesson plan covers basic Java programming focusing on looping statements, including while, do-while, for, and enhanced for loops. It also addresses digit manipulation techniques, nested loops, and various pattern printing methods. Additionally, it provides practice problems and links to further resources for students to enhance their understanding of these concepts.

Uploaded by

Yadhavan PS-1
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
You are on page 1/ 33

Basic Java Programming

Looping Statements
Lesson Plan

Subject/Course Basic Java Programming


Lesson Title Introduction to Programming

Lesson Objectives
Looping Statements

Digit Manipulation, Nested Loops, Pattern


Looping Statements

A loop statement allows us to execute a statement or group of statements


multiple times.

Like conditional statements, they are controlled by Boolean expressions

Java has three kinds of repetition statements:

• the while loop

• the do loop

• the for loop


while Loop Statement

Used when the number of iterations is not known in advance. It checks the
condition before the loop body.
Syntax:
while(condition){
//code to be executed
}
while Loop Statement-example

public class WhileExample {


public static void main(String[] args) {
int i=1;
while(i<5){
System.out.println(i);
i++;
}
}
}
dowhile Loop Statement
Similar to while, but it checks the condition after the loop body. It
executes at least once.
Syntax:
do
{
statement;
}
while ( condition )
dowhile Loop Statement-example

public class DoWhileExample {


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}
for Loop Statement

Used when the number of iterations is known

Syntax:
for(initialization; condition; update) {
// code to be executed
}
for Loop Statement-example

public class ForExample {

public static void main(String[] args) {

for(int i=1;i<=10;i++){

System.out.println(i);

}
Enhanced for Loop Statement

Used to iterate over arrays or collections.

Syntax:
for(type variable : arrayOrCollection) {
// code to be executed
}
Enhanced for Loop Statement-example

public class ForEachExample {

public static void main(String[] args) {

int[] numbers = {1,2,3,4,5,6,7,8,9,10};

for(int num : numbers) {

System.out.println(num);

}
Nested loops

A nested loop means placing one loop inside another. The inner loop runs
completely every time the outer loop runs once.
Syntax:
for(initialization; condition; update) {
for(initialization; condition; update) {
// Inner loop body
}
// Outer loop body
}
Nested loops-Example

public class NestedLoopExample {


public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 10; j++) {
System.out.print(i * j + "\t");
}
System.out.println();
}
}
}
Digit Manipulation

Definition: Operations performed on individual digits of a number.


Common use-cases: Validations, logic building, pattern matching, numerical
puzzles, etc.
Basic operations on digits
• Extracting Digits using % and / operators
% 10 → gets last digit
/ 10 → removes last digit
• Counting Digits
Loop until the number becomes 0, increment a counter.
• Summing Digits
Add each extracted digit to a running sum.
• Product of Digits
Multiply extracted digits.
• Reversing the Number
Append digits from end to a new number in reverse order.
Count of the digits

public class CountDigits {


public static void main(String[] args) {
int num = 12345;
int count = 0;

while(num > 0) {
count++;
num /= 10;
}

System.out.println("Total digits: " + count);


}
}
Sum of the digits

public class SumDigits {


public static void main(String[] args) {
int num = 1234, sum = 0;

while(num > 0) {
int digit = num % 10;
sum += digit;
num /= 10;
}

System.out.println("Sum of digits: " + sum);


}
}
Practise problems

•Reverse number
•Palindrome
•Armstrong number
•Count even and odd digits
•Check if digits are in increasing order
•Remove specific digit
•Find largest and smallest digit
Pattern

Outer loop → controls rows


Inner loop → controls columns (characters printed in each row)
Pattern Types
Star patterns (▲, ▼, □)
Number patterns (1, 12, 123...)
Alphabet patterns (A, AB, ABC...)
Mixed patterns (number-star combo, palindromic)
Space Management
Use spaces for alignment (especially in pyramids)
" " helps in centering pyramids
Pattern example- Right angled Triangle

public class StarPattern {


public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
CharacterPattern
public class CharacterPattern {
public static void main(String[] args) {
char ch = 'A'; // starting character
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
}
ch++; // move to next character
System.out.println();
}
}}
NumberPattern
public class NumberPattern {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Problem Statement 1 - Basic
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level:0
JAVA_LOOP_L0_1.Numbers
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/8fed3bde-f5de-
42cc-9adf-4ae4c4ddf6b4
Problem Statement 2 - Basic
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level: 0
JAVA_LOOP_L0_3.Sum of even
numbers
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/8fed3bde-f5de-
Problem Statement 3 - Basic
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level:0
JAVA_LOOP_L0_4.Sum of First N
odd number
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/8fed3bde-f5de-
Problem Statement 4 - Basic
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level: 0
JAVA_LOOP_L0_6.Sum divisible by
3 or 5 in range
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/8fed3bde-f5de-
Problem Statement 5 - Basic
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level: 0
JAVA_LOOP_L0_8.Power Problem
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/8fed3bde-f5de-
42cc-9adf-4ae4c4ddf6b4
Problem Statement 6 - Intermediate
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level:1
JAVA_LOOP_L1_1.Perfect Square
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/fa0b00b0-c69b-
4437-a217-f7da1d7ab9d7
Problem Statement 7 - Intermediate
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level: 1
JAVA_LOOP_L1_2.perfect cube
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/fa0b00b0-c69b-
4437-a217-f7da1d7ab9d7
Problem Statement 8 - Intermediate
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level:1
JAVA_LOOP_L1_6.Multiplication
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/fa0b00b0-c69b-
4437-a217-f7da1d7ab9d7
Problem Statement 9 - Complex
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level: 2
JAVA_LOOP_L2_3.mul_div_by_2_seri
es
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/bc53d57a-04b3-
Problem Statement 10 - Complex
Problem Statement Path:
Java Technical Course
Basic Java
Looping
Looping_Statement_Level:2
JAVA_LOOP_L2_8.Completed Days
Link
:https://lms.talentely.com/test/tes
t/6581c2c2-2126-4181-b63b-
2f57b2d1c8c3/bc53d57a-04b3-
46e4-a130-e3904caa6d07
Thank You

You might also like