0% found this document useful (0 votes)
5 views7 pages

Java-CAT Flow - Level 2

The document covers looping statements in programming, including for loops, while loops, and do-while loops, with practical problems for each type. It also discusses nested loops, jumping statements like continue and break, and introduces arrays with analogies and operations. Additionally, it includes practice problems related to arrays and their manipulations.

Uploaded by

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

Java-CAT Flow - Level 2

The document covers looping statements in programming, including for loops, while loops, and do-while loops, with practical problems for each type. It also discusses nested loops, jumping statements like continue and break, and introduces arrays with analogies and operations. Additionally, it includes practice problems related to arrays and their manipulations.

Uploaded by

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

Level 2-Looping statements and Arrays

Looping Statements

[Link] loop = Taking Attendance in a Class


Imagine you are taking attendance for 30 students.
You already know:
Start → Student 1
End → Student 30
Step → Move one by one

Practise Problems for loop:


[Link] sum of first N numbers
Input: 5 → Output: 15

[Link] factorial of a number


Input: 5 → Output: 120

[Link] digits in a number


Input: 12345 → Output: 5

[Link] a number
Input: 123 → Output: 321

[Link] if a number is palindrome


Input: 121 → Output: Palindrome

[Link] loop =Walking Until Condition Changes


 You don’t know exactly how many steps:
You just keep going until something stops you
Condition is checked every time
Analogy
“Walk until it starts raining.”

Practise Problems While loop:

1. Keep taking input until user enters 0


Sample Input:
5
8
3
0
Sample Output:
You entered: 5
You entered: 8
You entered: 3
Program terminated (0 entered)

2. Keep adding numbers until sum exceeds 100


Sample Input:
20
30
25
40
Sample Output:
Current Sum: 20
Current Sum: 50
Current Sum: 75
Current Sum: 115
Sum exceeded 100. Stopping...

3. Find GCD of two numbers using while loop


Sample Input:
Enter first number: 12
Enter second number: 18
Sample Output:
GCD is: 6

4. Fibonacci Series using while loop


Sample Input:
Enter number of terms: 6
Sample Output:
Fibonacci Series:
011235

[Link] while – Restaurant Taste Analogy


Scenario:
You go to a restaurant and see a new dish.
You say:
“Let me taste it once, then I’ll decide whether to continue eating or not.”
What happens?
You taste the dish first
Then you decide:
“Do I like it?”
If yes → you continue eating
If no → you stop
“Try first, decide later” = DO-WHILE

[Link] for loop (classroom analogy)

Scenario:
You are arranging students in a classroom:
There are 3 rows
Each row has 4 students
What do you do?
Go to Row 1
Fill Seat 1, 2, 3, 4
Then go to Row 2
Fill Seat 1, 2, 3, 4
Then go to Row 3
Fill Seat 1, 2, 3, 4

Mapping to Nested Loop:


Outer loop → Rows
Inner loop → Seats in each row

Practise Problem Nested loop:

[Link] Table (Grid)


Sample Case 1
Input:
Enter rows: 3
Enter columns: 3
Output:
123
246
369

Sample Case 2
Input:
Enter rows: 4
Enter columns: 4
Output:
1234
2468
3 6 9 12
4 8 12 16

[Link] Break – School Building Emergency


Scenario:
Imagine a school building:
Floors → Outer loop
Classrooms in each floor → Inner loop
Normally:
You check each classroom on each floor

Situation 1: Small issue (Normal break)


Problem in one classroom
You stop checking that classroom only
Move to next floor
This is normal break

Situation 2: Fire Alarm (Labelled break)


Fire alarm triggers anywhere
You stop checking everything immediately
Exit the entire building
This is labelled break

Mapping to Code
Outer loop → Floors
Inner loop → Classrooms
Labelled break → Exit entire building (all loops)

Jumping Statements

[Link] - Skip and Move On


Analogy: Skipping a Question in Exam
You are writing an exam:
If a question is difficult → you skip it
Move to the next question immediately
You don’t stop the exam, just skip current and continue
[Link] - Searching a Book in Library
Scenario:
You are searching for a specific book in a library shelf.
What do you do?
Check books one by one
As soon as you find the required book
You stop searching immediately

[Link] - Exit Method


Analogy: Leaving Office Early
You are working:
You finish your work early
You leave the office completely
You don’t just stop current task → you exit entire function

Behavior:
Exits method immediately
Returns value (or nothing)

Practise Problems on Jump Statements:

1. Print numbers from 1 to 10, skip multiples of 3


Sample Output:
1 2 4 5 7 8 10

2. Print numbers from 1 to 50, skip numbers divisible by both 2 and 3


(i.e., skip multiples of 6)
Sample Output:
1 2 3 4 5 7 8 9 10 11 ... 49 50

3. Keep taking input until user enters a negative number


Sample Input:
10
25
5
-3
Sample Output:
You entered: 10
You entered: 25
You entered: 5
Negative number entered. Stopping...
4. Find first multiple of 7 between 1 and 50
Sample Output:
First multiple of 7 is: 7

Another Understanding:
If range starts from 10:
First multiple of 7 is: 14

5. Method to return sum of digits of a number


Sample Input:
Enter number: 1234
Sample Output:
Sum of digits: 10

Arrays

1. Ask the question  int a = 10; can int a = 10,20,30;  moving to array
2. ARRAY – Egg Tray Analogy
Scenario:
Think of an egg tray:
It has fixed slots (say 12 slots)
Each slot can hold only one egg
Each slot has a position (index)

Mapping to Array:
Array → Egg tray
Elements → Eggs
Index → Slot number
Size → Total slots (fixed)
3. Contiguous memory allocation, Indexing, Fixed size with an example
(Why starts with 0?  use formula)
4. How to use arrays in Java?
Declaration (Syntax and ex), Array size, Base address, Type address
Initialization
Creation(new keyword)
5. Get the input from the user in runtime(using Scanner class)
6. How to access the array using for loop
7. Array operations
Insert
Update
Traverse
Delete
 Length of Array
[Link] problems [Sum, Reverse, max, min, kth largest, kth smallest, linear Search,
Binary Search, Selection Sort and Insertion Sort]
[2 sum & 3 sum using a pointer, rotate, Target, Palindrome, Prime number, Sliding
window pattern]
9. Two – Dimensional array
Initialization
Declaration(Syntax)
Analogy
10. Practice problems
[Add, Multiplication] passing array using methods and returning array from method
11. Jagged Array

You might also like