首页  >  文章  >  Java  >  Java 编程中的循环

Java 编程中的循环

WBOY
WBOY原创
2024-08-30 15:24:311137浏览

简单定义中的“编码”意味着告诉计算机做什么的方法;然而,这并不像看起来那么容易,但到目前为止,我们不会关注后面的部分(意味着简单或困难)。在本主题中,我们将学习 Java 编程中的循环。

广告 该类别中的热门课程 编程语言 - 专业化 | 54 课程系列 | 4 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

计算机只能理解“ON”和“OFF”类型的数据,即俗称的“二进制”值。二进制代码由(0 和 1)组成,全世界的计算机都能理解。但问题是我们不能仅仅为了让计算机理解我们要求它们计算的内容而写入数万亿个 0 和 1。这就是编程语言或编码发挥作用的地方。

至此,我们已经成功地将我们的理解过滤到了编码级别,现在,既然我们知道了“编码”的作用以及我们为什么编码,我们就必须进一步深入到“循环”级别,这就是讨论的标题是?

我们身边有几个 PL;其中许多用于 Web 开发,其他用于桌面应用程序开发,有些称为高级 PL,有些称为低级 PL。所有这些编程语言都有一些共同点,即“循环”。

更深入地讨论,循环几乎存在于所有编程语言中;让我们看看他们对开发商有什么优势 –

  • 这些是“可重复使用的”
  • 它们缩小了“编码”的大小。
  • 他们使“控制”变得简单。
  • 它们往往会降低“复杂性”。

JAVA 编程中的循环旨在解决代码复杂性,可用且可供开发人员根据需求重用代码。

Java 中的循环类型

java中循环的类型如下:

在JAVA中,循环是迭代语句。这些语句帮助开发人员(或用户)迭代程序代码,或一组代码运行多次(根据需要)。

在JAVA中,循环主要有3大类,分别是

  • FOR 循环
  • While 循环
  • DO-While 循环

在深入研究这些循环之前,我们希望读者了解一件事(这对所有三个循环都有价值)。无论是 FOR、WHILE 还是 DO WHILE,都有起始、主体和最后的目的地。不用多说,让我们一一看看它们 –

1. For 循环

如果您是一名开发人员,并且希望在获得最终结果(或结果)之前执行或运行程序的一部分特定次数,那么您将使用 FOR 循环。请记住,只有当您清楚地知道要执行语句的次数时,您才会使用“For 循环”。 FOR 循环将重复自身,直到其值等于“TRUE”。

让我们看一下它的流程图,以便更好更清楚地理解 –

Java 编程中的循环

语法

for(initialization condition; testing condition; increment/decrement)
{
statement(s)
}

说明

所有 3 个参数(即初始化、条件和递增/递减)都保留在 FOR 循环中的一个语句中。初始化意味着提供变量的初始值。条件表示我们要在程序中检查的值。增量/减量表示您希望在循环中拥有的值;该值将相应增加或减少。循环体以大括号开始,以大括号 (}) 结束,并包含将使用循环执行的语句。

示例

我们的目的是打印 1 到 100 之间的所有偶数。

class Test
{
public Static Void Main (String ar [])
{
int no; for (no = 2; no <=100; no = no+2)
{
System.out. print ln(no);
}
}
}

该程序的输出将为 2,4,6,8,10,12…………………..96,98,100

2. While 循环

只有当需要重复执行一定数量的语句直到条件满足时,我们才需要?这里,与 FOR 循环不同,在执行语句之前首先检查条件。

让我们看一下它的流程图,以便更好更清楚地理解 –

Java 编程中的循环

语法

while (boolean condition)
{
loop statements
}

说明

虽然循环以放置在括号内的应用条件语句开始,但它们也将循环语句保留在大括号内。正如我们所说,WHILE 循环将一直运行,直到该值保持为真值为止。

Example 

We want to print all the odd numbers between 1 to 100.

class Test
{
public static void main (String ar[])
{
int no = 1;
while (no<100)
{
System.out.print (no);
No = no +2;
} } }

Output for this program will be 1,3,5,7,9,11………………………………………..97,99

3. Do While

There is not much difference between the WHILE and DO WHILE loops; the difference lies in their statement validation. In DO WHILE, the condition is checked after execution of the block of statements; hence we can say in DO WHILE, the statements are at least executed once.

Let us see the flowchart of it for better and clear understanding –

Java 编程中的循环

Syntax 

do
{
statements..
}
while (condition);

Explanation 

There is no condition checking in the DO WHILE loop for the first time; later, the condition is checked for TRUE or FALSE. If it is TRUE, then the next iteration of loops start; otherwise, the loop terminates itself.

Example

class Test
{
public Static Void Main (String ar[])
{
int no = 2;
do
{
System.out.print (no);
No = no + 2;
}
while (no<=100);
}}}

Output for this program will be – 2,4,6,8,10…………….98,100

Comparison Between Different Types of Loops

The comparison between different types of loops are as follows:

1) Declaration

For Loop

for (initialization; condition; iteration){
//body of 'for' loop
}

While Loop

Statements; //body of loop
}

Do While

do
{
Statements
}
while (condition);

2) We use FOR loop if the user knows the time of iteration, whereas the WHILE and DO WHILE loop is preferred when the number of iteration is not known.

3) Regarding the conditional statement in FOR loop, it will operate infinite time, whereas for WHILE and DO WHILE the absence of conditional statement will give ‘Compilation Error’.

Conclusion

The loops concept is crucial for any users who belong to development; if they are freshers and prepare for exams or interviews, they must be well-rehearsed with the Loops concepts. We have covered all the crucial aspects of Loops, and this is how things work in Loops. These 3 Loops are the most crucial ones, and the rest are improvised on them. If you have a good grip over these, then the rest will be quite easier to understand.

以上是Java 编程中的循环的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn