Understanding Program Control Structures: Sequence, Selection, and Loop with Java Examples
This article explains the three basic program structures—sequential, selection, and loop—illustrates each with clear Java code examples, discusses syntax, common pitfalls, and provides practice exercises to help beginners master control flow in programming.
The program structure is generally divided into three types: sequential, selection, and loop.
1. Sequential Structure follows a top‑down order, similar to writing an article from start to finish. The example below reads a name and age from the console and prints a self‑introduction, demonstrating that statements are executed in the order they appear.
public static void main(String[] args) {
// Create input object
Scanner input = new Scanner(System.in);
String name; // name
int age; // age
System.out.print("请输入姓名:");
name = input.next();
System.out.print("请输入年龄:");
age = input.nextInt();
System.out.println("大家好,我叫"+name+",今年"+age+"岁,请多关照。");
}2. Selection Structure chooses a code path based on a condition. Four forms are shown, starting with a simple if that adds a "rich" title when a deposit exceeds 5000, then extending to else, else if, and a multi‑branch selection for various deposit ranges.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
int deposit;
System.out.print("请输入姓名:");
name = input.next();
System.out.print("请输入存款:");
deposit = input.nextInt();
System.out.print("大家好,我叫"+name);
if (deposit > 5000) {
System.out.print("(壕)");
}
System.out.println("。");
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
int deposit;
System.out.print("请输入姓名:");
name = input.next();
System.out.print("请输入存款:");
deposit = input.nextInt();
System.out.print("大家好,我叫"+name);
if (deposit > 5000) {
System.out.print("(壕)");
} else {
System.out.print("(穷13)");
}
System.out.println("。");
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
int deposit;
System.out.print("请输入姓名:");
name = input.next();
System.out.print("请输入存款:");
deposit = input.nextInt();
System.out.print("大家好,我叫"+name);
if (deposit > 50000) {
System.out.print("(神壕)");
} else if (deposit > 30000) {
System.out.print("(金壕)");
} else if (deposit > 10000) {
System.out.print("(壕)");
} else {
System.out.print("(穷13)");
}
System.out.println("。");
}3. Loop Structure includes while, do...while, for, and later foreach. The article demonstrates each by calculating the sum of odd numbers up to 100, showing initialization, condition, step, and body.
public static void main(String[] args) {
int i = 1; // initial value
int s = 0; // accumulator
while (i <= 100) { // condition
s += i; // body
i += 2; // step
}
System.out.println("while循环结果:"+s);
// do...while
i = 1; s = 0;
do {
s += i;
i += 2;
} while (i <= 100);
System.out.println("do...while循环结果:"+s);
// for
s = 0;
for (i = 1; i <= 100; i += 2) {
s += i;
}
System.out.println("for循环结果:"+s);
}The article also explains the use of continue and break inside loops, showing how continue skips the remaining statements of the current iteration, while break exits the loop entirely.
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.print(i+",");
}
System.out.println("
=======================");
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.print(i+",");
}
}A classic login example is presented, first using a do...while loop to repeatedly ask for username and password until they match predefined values, then showing equivalent implementations with while and for loops, highlighting code duplication when not using do...while.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inLoginID, inLoginPWD;
String loginID = "liergou";
String loginPWD = "haha250";
do {
System.out.print("请输入账号:");
inLoginID = input.next();
System.out.print("请输入密码:");
inLoginPWD = input.next();
if (inLoginID.equals(loginID) && inLoginPWD.equals(loginPWD)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("账号和密码不匹配,请重新输入!");
}
} while (true);
}Finally, the article lists several practice exercises (e.g., printing all three‑digit narcissistic numbers, prime factorization, free‑fall rebound calculations, digit counting, and simple interactive prompts) for readers to apply the learned control‑flow concepts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
