0% found this document useful (0 votes)
24 views2 pages

Java Control Structures Example

The document is a Java program demonstrating basic control structures including if-else statements, switch cases, and for loops. It prompts the user for their age to determine voting eligibility and allows them to choose between saying hello or goodbye. Additionally, it counts from 1 to 5 using a for loop.

Uploaded by

ramya.cse
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)
24 views2 pages

Java Control Structures Example

The document is a Java program demonstrating basic control structures including if-else statements, switch cases, and for loops. It prompts the user for their age to determine voting eligibility and allows them to choose between saying hello or goodbye. Additionally, it counts from 1 to 5 using a for loop.

Uploaded by

ramya.cse
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

package controlstructuresdemo;

import [Link];

public class SimpleControlStructure {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

// if-else structure

[Link]("Enter your age: ");

int age = [Link]();

if (age >= 18) {

[Link]("You are eligible to vote.");

} else {

[Link]("You are not eligible to vote.");

// switch structure

[Link]("\nChoose an option:");

[Link]("1. Say Hello");

[Link]("2. Say Goodbye");

int choice = [Link]();

switch (choice) {

case 1:

[Link]("Hello!");

break;

case 2:

[Link]("Goodbye!");
break;

default:

[Link]("Invalid option.");

// for loop

[Link]("\nCounting from 1 to 5:");

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

[Link](i);

[Link]();

You might also like