0% found this document useful (0 votes)
4 views14 pages

4. Control Statements

The document provides an overview of control statements in Java, detailing their purpose in controlling the flow of program execution. It categorizes control statements into decision-making, looping, and jump statements, and includes syntax and examples for each type. Additionally, it presents various pattern programs that utilize these control statements to demonstrate their application in Java programming.

Uploaded by

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

4. Control Statements

The document provides an overview of control statements in Java, detailing their purpose in controlling the flow of program execution. It categorizes control statements into decision-making, looping, and jump statements, and includes syntax and examples for each type. Additionally, it presents various pattern programs that utilize these control statements to demonstrate their application in Java programming.

Uploaded by

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

Module 2: Control Statements in Java

What are Control Statements?


Control statements are used to control the flow of execution of a Java
program.
They decide:
• Which statement to execute
• How many times to execute
• When to stop execution
Types of Control Statements
1. Decision Making Statements/Conditional Statements
2. Looping Statements
3. Jump Statements/Branch Statements

1. Decision Making Statements


1.1 if Statement
The if statement executes a block of code only if the condition is true.
Syntax:
if(condition) {
// code to execute
}
Example
int age = 20;
if(age >= 18) {
[Link]("Eligible to vote");
}
Important Points
• Condition must be boolean
• No execution if condition is false

1.2 if-else Statement


The if-else statement executes one block if condition is true, otherwise
executes the else block.
Syntax:
if(condition) {
// true block
} else {
// false block
}
Example
int marks = 45;
if(marks >= 35) {
[Link]("Pass");
} else {
[Link]("Fail");
}
Exam Tip: Used when two conditions are possible.

2. switch Statement
The switch statement selects execution paths based on multiple fixed
values.
Syntax:
switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example
int day = 3;
switch(day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid Day");
}
Key Rules
• break prevents fall-through
• default is optional
• Supports int, char, String

3. Looping Statements
Why Loops?
Loops are used to execute a block of code repeatedly.

3.1 for Loop


Used when number of iterations is known.
Syntax
for(initialization; condition; increment/decrement) {
// loop body
}
Example
for(int i = 1; i <= 5; i++) {
[Link](i);
}
Flow
1. Initialize
2. Check condition
3. Execute block
4. Increment
5. Repeat

3.2 while Loop


Used when iterations are not fixed, condition is checked before
execution.
Syntax:
while(condition) {
// loop body
}
Example
int i = 1;
while(i <= 5) {
[Link](i);
i++;
}
Note
• If condition is false initially → loop never executes

3.3 do-while Loop


Executes loop at least once, condition checked after execution.
Syntax:
do {
// loop body
} while(condition);
Example
int i = 1;
do {
[Link](i);
i++;
} while(i <= 5);
Exam Question: Difference between while and do-while?
4. Jump Statements/Branch Statements
4.1 break Statement
Terminates loop or switch immediately.
Example
for(int i = 1; i <= 10; i++) {
if(i == 5) {
break;
}
[Link](i);
}
Output:
1 2 3 4

4.2 continue Statement


Skips current iteration and continues with next iteration.
Example
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
[Link](i);
}
Output:
1 2 4 5

5. Nested Loops
A loop inside another loop is called nested loop.
Example
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
[Link]("* ");
}
[Link]();
}
Output
* * *
* * *
* * *
Rule
• Outer loop → rows
• Inner loop → columns

6. Pattern Programs in Java


Pattern 1: Square Pattern
* * * *
* * * *
* * * *
* * * *
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= 4; j++) {
[Link]("* ");
}
[Link]();
}

Pattern 2: Right Triangle


*
* *
* * *
* * * *
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}

Pattern 3: Number Pattern


1
1 2
1 2 3
1 2 3 4
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}

Pattern 4: Inverted Triangle


* * * *
* * *
* *
*
for(int i = 4; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}

Summary
• Control Statements control program flow
• if-else → decision making
• switch → multiple fixed options
• for, while, do-while → looping
• break → stop loop
• continue → skip iteration
• Patterns test logic + nested loops
30+ Pattern Programs in Java
STAR PATTERNS
1. Square Star Pattern
* * * *
* * * *
* * * *
* * * *
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
[Link]("* ");
}
[Link]();
}

2. Right Triangle Star Pattern


*
* *
* * *
* * * *
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link]("* ");
}
[Link]();
}

3. Inverted Right Triangle


* * * *
* * *
* *
*
for(int i=4;i>=1;i--){
for(int j=1;j<=i;j++){
[Link]("* ");
}
[Link]();
}

4. Left Triangle
*
* *
* * *
* * * *
for(int i=1;i<=4;i++){
for(int s=4;s>i;s--){
[Link](" ");
}
for(int j=1;j<=i;j++){
[Link]("* ");
}
[Link]();
}

5. Pyramid Pattern
*
* * *
* * * * *
* * * * * * *
for(int i=1;i<=4;i++){
for(int s=4;s>i;s--){
[Link](" ");
}
for(int j=1;j<=2*i-1;j++){
[Link]("* ");
}
[Link]();
}

6. Inverted Pyramid
* * * * * * *
* * * * *
* * *
*
for(int i=4;i>=1;i--){
for(int s=4;s>i;s--){
[Link](" ");
}
for(int j=1;j<=2*i-1;j++){
[Link]("* ");
}
[Link]();
}

7. Diamond Pattern
*
* *
* * *
* * * *
* * *
* *
*
for(int i=1;i<=4;i++){
for(int s=4;s>i;s--) [Link](" ");
for(int j=1;j<=i;j++) [Link]("* ");
[Link]();
}
for(int i=3;i>=1;i--){
for(int s=4;s>i;s--) [Link](" ");
for(int j=1;j<=i;j++) [Link]("* ");
[Link]();
}

NUMBER PATTERNS
8. Number Triangle
1
1 2
1 2 3
1 2 3 4
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link](j+" ");
}
[Link]();
}

9. Same Number Triangle


1
2 2
3 3 3
4 4 4 4
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link](i+" ");
}
[Link]();
}

10. Continuous Numbers


1
2 3
4 5 6
7 8 9 10
int num=1;
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link](num++ +" ");
}
[Link]();
}

11. Reverse Number Triangle


1 2 3 4
1 2 3
1 2
1
for(int i=4;i>=1;i--){
for(int j=1;j<=i;j++){
[Link](j+" ");
}
[Link]();
}

12. Floyd’s Triangle


1
2 3
4 5 6
7 8 9 10
int n=1;
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link](n++ +" ");
}
[Link]();
}

CHARACTER PATTERNS
13. Alphabet Triangle
A
A B
A B C
A B C D
for(char i='A';i<='D';i++){
for(char j='A';j<=i;j++){
[Link](j+" ");
}
[Link]();
}

14. Same Alphabet Pattern


A
B B
C C C
D D D D
char ch='A';
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link](ch+" ");
}
ch++;
[Link]();
}

15. Reverse Alphabet Triangle


D
D C
D C B
D C B A
for(char i='D';i>='A';i--){
for(char j='D';j>=i;j--){
[Link](j+" ");
}
[Link]();
}

SPECIAL PATTERNS
16. Hollow Square
* * * *
* *
* *
* * * *
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(i==1||i==4||j==1||j==4)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}

17. Hollow Triangle


*
* *
* *
* * * *
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
if(j==1||j==i||i==4)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}

18. X Pattern
* *
* *
*
* *
* *
int n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j||i+j==n+1)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}

19. Plus Pattern


*
*
* * *
*
*
int n=3;
for(int i=1;i<=5;i++){
for(int j=1;j<=3;j++){
if(j==2||i==3)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}

20. Binary Triangle


1
0 1
1 0 1
0 1 0 1
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link]((i+j)%2+" ");
}
[Link]();
}

MORE PRACTICE PATTERNS


21. Half Pyramid with Numbers
22. Half Pyramid with Alphabets
23. Inverted Half Pyramid
24. Mirror Right Triangle
25. Zig-Zag Pattern
26. Pascal Triangle
27. Hollow Pyramid
28. Butterfly Pattern
29. Hourglass Pattern
30. Sandglass Pattern

Exam Tip for Students


• Outer loop → Rows
• Inner loop → Columns
• Space logic decides alignment
• Conditions decide hollow / special shapes

You might also like