0% found this document useful (0 votes)
16 views3 pages

Java Selection and Iteration Constructs

The document outlines a Java programming exercise aimed at demonstrating selection and iteration constructs, including if, switch, for, while, and do-while statements. It provides theoretical explanations and practical code examples for user input-driven decision-making and repetitive execution. The conclusion emphasizes the importance of mastering these constructs for effective Java programming.

Uploaded by

owen9b44
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)
16 views3 pages

Java Selection and Iteration Constructs

The document outlines a Java programming exercise aimed at demonstrating selection and iteration constructs, including if, switch, for, while, and do-while statements. It provides theoretical explanations and practical code examples for user input-driven decision-making and repetitive execution. The conclusion emphasizes the importance of mastering these constructs for effective Java programming.

Uploaded by

owen9b44
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

VIDYAVARDHINI’S COLLEGE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF INFORMATION TECHNOLOGY


K.T. Marg, Vasai Road (W), Dist-Palghar - 401202, Maharashtra

Aim: To write and execute Java programs demonstrating selection and iteration
constructs, including decision-making using if and switch statements, and repetitive
execution using different loop structures.

Objective
Write simple Java programs to demonstrate selection (if, switch) and iteration (for,
while, do-while) constructs using user inputs for dynamic decision-making and
repetitive execution.

Theory
1. General Theory: Selection constructs permit branching based on conditions;
iteration constructs enable repeat actions until conditions change. Mastering
these ensures effective logic building.

2. If/If-Else/If-Else-If: if checks a condition, executing code only when true. if-


else adds an alternative path for false conditions, while if-else-if chains multiple
checks to filter for specific logic branches.

3. Switch: Useful for multi-way branching, switch evaluates a variable against


several constant cases. It's cleaner than many if-else chains for discrete value
comparison.

4. For Loop: Executes a block a fixed number of times. Initialisation, condition, and
increment steps control its behaviour, making it ideal for array traversal or
counting.

5. While Loop: Repeats code while a specified condition is true; more flexible for
cases where the number of repetitions is not predetermined.

6. Do-While Loop: Similar to while, but always executes at least once regardless of
the condition's initial state. Useful for input validation and menu-driven
programs.
VIDYAVARDHINI’S COLLEGE OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
K.T. Marg, Vasai Road (W), Dist-Palghar - 401202, Maharashtra

Code:
import [Link];

public class SelectionIterationDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Selection: if-else
[Link]("Enter your age: ");
int age = [Link]();
if (age >= 18) {
[Link]("Eligible to vote.");
} else {
[Link]("Not eligible to vote.");
}

// Selection: switch
[Link]("Enter a weekday number (1-7): ");
int day = [Link]();
switch(day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
case 4: [Link]("Thursday"); break;
case 5: [Link]("Friday"); break;
case 6: [Link]("Saturday"); break;
case 7: [Link]("Sunday"); break;
default: [Link]("Invalid day.");
}

// Iteration: for loop


[Link]("Print numbers up to: ");
int n = [Link]();
for (int i = 1; i <= n; i++) {
[Link](i + " ");
}
[Link]();

// Iteration: while loop


[Link]("Enter start value for countdown: ");
int count = [Link]();
while (count > 0) {
VIDYAVARDHINI’S COLLEGE OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
K.T. Marg, Vasai Road (W), Dist-Palghar - 401202, Maharashtra

[Link](count);
count--;
}

// Iteration: do-while loop


int num;
do {
[Link]("Enter a number (0 to exit): ");
num = [Link]();
[Link]("You entered: " + num);
} while (num != 0);

[Link]("Demo complete.");
}
}

Output:
C:\Users\thakk\OneDrive\Documents\java notepad>java [Link]
Enter your age: 19
Eligible to vote.
Enter a weekday number (1-7): 7
Sunday
Print numbers up to: 6
123456
Enter start value for countdown: 4
4
3
2
1
Enter a number (0 to exit): 4
You entered: 4
Enter a number (0 to exit): 9
You entered: 9
Enter a number (0 to exit): exit

Conclusion: Learning selection and iteration constructs (if, switch, for, while, do-while)
is vital for real-world Java programming. These constructs enable sophisticated
decision-making and repetitive execution for robust, logical, and user-friendly
applications.

You might also like