栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > R语言

r语言中的循环

R语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

r语言中的循环

r语言中的循环

Sometimes the programmers need to encounter a situation where they need to execute a particular block of code many times. Everyone is acquainted with the fact that usually whenever there is a block of code then the statements in them are executed sequentially. However, if a situation exists where the user needs to run the same block multiple times then it will be difficult to write the same code for those many times as needed. Therefore keeping the advancing requirements and needs of the users in the programming world certain functions are inbuilt in any programming language to ensure the comfortable atmosphere around the programmers. The above-stated functions are nothing but so-called loops in any programming language.

有时,程序员需要遇到需要多次执行特定代码块的情况。 每个人都熟悉这样一个事实,通常只要有代码块,它们中的语句便会顺序执行。 但是,如果存在用户需要多次运行同一块代码的情况,那么将难以根据需要多次编写相同的代码。 因此,为了满足编程世界中用户的不断提高的需求和要求,某些功能内置于任何编程语言中,以确保程序员周围的舒适氛围。 上述功能不过是任何编程语言中的所谓循环。

循环 (Loops)

Coming to the brief description of loops, loops are inbuilt functions that run the same block of code repeatedly. Commonly a loop statement allows the users to execute a statement or a block of statements multiple times. In loops, we generally follow a particular format in which there are several parameters like conditional along with conditional code when the conditions inserted become either true or false. The following is the general chart of the above-mentioned parameters. Coming to the brief description of loops, loops are inbuilt functions that run the same block of code repeatedly. Commonly a loop statement allows the users to execute a statement or a block of statements multiple times. In loops, we generally follow a particular format in which there are several parameters like conditional along with conditional code when the conditions inserted become either true or false. The following is the general chart of the above-mentioned parameters. 

简要介绍一下循环 , 循环是内置函数,可重复运行相同的代码块。 通常, 循环语句允许用户多次执行一条语句或一段语句。 在循环中,我们通常遵循一种特殊的格式,其中当插入的条件变为true或false时,会有多个参数(例如conditional)以及条件代码。 以下是上述参数的一般图表。 简要介绍一下循环,循环是内置函数,可重复运行相同的代码块。 通常,循环语句使用户可以多次执行一条语句或一段语句。 在循环中,我们通常遵循一种特殊的格式,其中当插入的条件变为true或false时,会有多个参数(例如conditional)以及条件代码。 以下是上述参数的一般图表。

The users working with the R will get a chance to acknowledge the different types of loops and loop statements.

使用R的用户将有机会认识到不同类型的循环和循环语句 。

There are mainly three types of loops. They are:

循环主要有三种类型。 他们是:

  1. repeat Loop

    重复循环

  2. while Loop

    while循环

  3. for Loop

    循环

1.重复循环 (1. repeat Loop)

Using the repeat loop statement a particular group of statements can be executed repeatedly. And, on the other hand, it also shortens the code written.

使用重复循环语句 ,可以重复执行特定的一组语句。 并且,另一方面,它也缩短了编写的代码。

Syntax:

句法:

    repeat {   
       //code to be executed   
       if(exit_condition) {  
          break  
       }  
    }

Initially, we ought to initialize some variables which are taken by the repeat loop and thus are processed. Along with the initialization variables, one should also declare a condition in the repeat loop. This specific condition is checked and later the group of statements is executed as per the boolean obtained after checking the condition. Finally, the repeat loop uses a break statement for coming out of the repeat loop.

最初,我们应该初始化一些由重复循环获取并因此被处理的变量。 除初始化变量外,还应在重复循环中声明一个条件。 检查此特定条件,然后按照检查条件后获得的布尔值执行语句组。 最后, 重复循环使用break语句从重复循环中出来 。

Have a look at the following example which depicts the usage of the repeat loop in the R language.

请看以下示例,该示例描述了R语言中重复循环的用法。

Example:

例:

# Initialising the counter and then 
# using it in the repeat loop
i <- 1 
repeat { 
  print("Welcome to includehelp.com") 
  i <- i+1     
  if(i >= 4) { 
     break 
  } 
}

Output

输出量

[1] "Welcome to includehelp.com"
[1] "Welcome to includehelp.com"
[1] "Welcome to includehelp.com"

2. while循环 (2. while Loop)

In the while loop, the block of statements will be repeatedly executed till the entry_condition is true. But prior to that, the while condition is checked and it must turn.

在while循环中 ,语句块将重复执行,直到entry_conditiontrue 。 但在此之前,将检查while条件,并且必须转为条件。

Syntax:

句法:

    while (entry_condition) {  
       //Code to be executed  
    } 

In general, a while Loop is called an "entry controlled loop" in programming languages. This is because prior to the execution of the block of statements the while loop initially checks for the trueness in the condition and then if the condition becomes true the statements are executed sequentially.

通常, while循环在编程语言中称为“ 入口控制循环” 。 这是因为在执行语句块之前, while循环首先检查条件中的真实性,然后如果条件变为true ,则按顺序执行语句。

Example:

例:

i <- 1
while (i <= 10) {
 print(i*i)
 i = i+1
}

Output

输出量

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
[1] 49
[1] 64
[1] 81
[1] 100

3. for循环 (3. for Loop)

The main purpose of the for loop is the same as the while loop, it checks the condition prior to before executing the group of statements. On the other hand, we also have loop control statements along with the different types of loop statements.

for循环的主要目的与while循环相同,它在执行语句组之前先检查条件。 另一方面,我们还具有循环控制语句以及不同类型的循环语句。

Syntax:

句法:

    for (valuetoIterate) {  
        //code to be executed 
    } 

Here the above syntax helps the users in making use of the for loop in R. To support the flow of the while loop now let us consider an example and apply the above concept to it.

在这里,上面的语法帮助用户使用R中的for循环 。现在,为了支持while循环的流程,让我们考虑一个示例并将上述概念应用于该示例。

Example:

例:

A<- c(432, 35, 45, 14)

print("Values of the vector:")
for ( i in A){  
  print(i) 
}

Output

输出量

[1] "Values of the vector:"
[1] 432
[1] 35
[1] 45
[1] 14


翻译自: https://www.includehelp.com/r/loops.aspx

r语言中的循环

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号