Java 中的控制语句是有助于确定从 Java 中的一个语句到另一语句的控制流的语句。 Java 中的控制语句有多种类型。在本文中,我们将观察控制语句的不同方面及其一些示例。不同类型的控制语句是:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
决策声明:
重复/循环语句:
跳跃语句:
这些是Java中用作流程控制语句的上述语句,它们分为决策语句、重复或循环语句和跳转语句。
在决策语句中,我们将看到 if-else 和嵌套的 if-else 语句以及 Switch case 语句。我们还将看到显示此类语句的有效性和执行情况的编码示例和示例输出。
if-else 语句以条件方式工作。语法如下:
语法:
if(condition) Statement 1 else Statement 2
示例:
在第一个 if-else 示例中,我们将查看用户输入的数字是否大于 100。如果数字大于 100,则会相应地显示输出。
代码:
import java.io.*; public class Example1 { public static void main(String args[])throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER A NUMBER"); int n= Integer.parseInt(br.readLine()); if (n>100) System.out.println("NUMBER ENTERED IS GREATER THAN 100"); else System.out.println("NUMBER ENTERED IS LESS THAN 100"); } }
输出:
在下面的程序中,我们输入两个数字。每当我们输入 250 作为数字时,程序显示它大于 100,每当我们输入 65 作为数字时,程序显示该数字小于 100。
嵌套的if-else语句中,有多个if条件,最后有一个print语句。语法如下:
语法:
if (condition1) if(condition2) if (condition3) Statement 1
示例:
在嵌套的 if 语句中,我们使用两个或三个 if-else 语句检查条件,然后最终得出结论。我们进一步检查该数量是否大于200;如果大于,我们将其打印为大于 200。
代码:
import java.io.*; public class Example2 { public static void main(String args[])throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER A NUMBER"); int n= Integer.parseInt(br.readLine()); if (n>100) { if(n>200) System.out.println("NUMBER ENTERED IS GREATER THAN 200"); } } }
输出:
我们输入两个都大于200的数字,我们发现正确的输出是两个数字都大于200。
在switch case中,有多个case,从中选择一个。语法如下:
语法:
switch(Variable) case 1: case 2: case 3: case n:
示例:
在这个例子中,我们将输入一个数字,程序将返回用户返回的数字。这是在 BlueJ 编程接口中运行的 switch case 语句的简单示例。
代码:
import java.io.*; public class Example3 { public static void main(String args[])throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER A NUMBER BETWEEN 1 TO 5"); int n= Integer.parseInt(br.readLine()); switch(n) { case 1: System.out.println("NUMBER ENTERED IS 1"); break; case 2: System.out.println("NUMBER ENTERED IS 2"); break; case 3: System.out.println("NUMBER ENTERED IS 3"); break; case 4: System.out.println("NUMBER ENTERED IS 4"); break; case 5: System.out.println("NUMBER ENTERED IS 5"); break; } } }
输出:
在上面的代码中,我们输入数字为4,程序返回输入的数字是4。
下面是 Java 中的重复/循环子句:
在for循环中,循环按照用户初始化的次数进行。以下是语法。
语法:
for(initialization, condition, update) Statement 1
示例:
在 for 循环示例中,我们将打印从 3 到 10 的奇数。我们对相应的程序使用 for 循环。
代码:
import java.io.*; public class Example4 { public static void main(String args[])throws IOException { System.out.println("Odd numbers from 3 to 10 are as follows"); for(int i=3; i<10; i+=2) { System.out.println(i); } } }
输出:
在上面的程序中,我们看到从3到10开始的奇数,打印出来的数字是3,5,7,9。
在while循环中,当条件为真时执行语句。语法如下:
语法:
while(Condition) Statement 1
示例:
使用 while 循环,我们现在要找到一个数字的逆数。这个程序很强大,可以找到任何整数的倒数。
代码:
import java.io.*; public class Example5 { public static void main(String args[])throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER A NUMBER"); int n= Integer.parseInt(br.readLine()); int digit, rev=0; while(n>0) { digit= n%10; rev= (rev*10) +digit; n=n/10; } System.out.println("Reverse number is " +rev); } }
Output:
In the above program, we find the reverse of a particular number. The number entered is 635, and the reverse of the number is 536, as displayed on the output screen.
The jumping statements in java are explained below.
There can be in the for loop in the break statement while loop or in switch case. Following is the syntax.
Syntax:
for(Statements) break; while(Statements) break;
Example:
In this example, we will see a menu-driven program, and we see the break statement’s application.
Code:
import java.io.*; public class Example6 { public static void main(String args[])throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER A NUMBER BETWEEN 1 TO 5"); int n= Integer.parseInt(br.readLine()); switch(n) { case 1: System.out.println("NUMBER ENTERED IS 1"); break; case 2: System.out.println("NUMBER ENTERED IS 2"); break; case 3: System.out.println("NUMBER ENTERED IS 3"); break; case 4: System.out.println("NUMBER ENTERED IS 4"); break; case 5: System.out.println("NUMBER ENTERED IS 5"); break; default: System.out.println("Number entered is not between 1 to 5"); break; } } }
Output:
The above code is very similar to the code used in the switch case statements. The break statement is generally used in the switch case statement. The break statement is also used in the if-else condition where the if-else statements need to be terminated. The above program asks for the number entered between 1 to 5. If the number is not between 1 to 5, then there is a default print that the number entered is not between 1 to 5. In the above case, we enter the number as 65, and it prints accordingly that the number entered is not between 1 to 5.
In this article, we come across the control statements in Java that are present. We take a look at the Looping statements, the Conditional statements, and others that are present. We also look at the programming aspects of the statements and how the statements are used inside the code. Control statements are quite used in Java, and they are present in every other programming language. They are used significantly throughout all programs for smooth execution.
以上是Java中的控制语句的详细内容。更多信息请关注PHP中文网其他相关文章!