c#foreach的使用_在PHP,JavaScript,Java,C#,Python编程语言中使用Foreach的示例

c#foreach的使用

c#foreach的使用

Loops are an important part of the programming languages. For and While loops are used for decades in different programming languages. Foreach provides similar functionality in a more elegant way by iterating over the given list. In this tutorial, we will examine foreach in different programming languages like PHP, JavaScript, Java, C# and Python.

循环是编程语言的重要组成部分。 ForWhile循环在不同的编程语言中使用了数十年。 通过遍历给定列表, Foreach以更优雅的方式提供了类似的功能。 在本教程中,我们将研究使用不同编程语言(例如PHP,JavaScript,Java,C#和Python)的foreach

PHP的Foreach的 (PHP Foreach)

PHP provides foreach in order to iterate over given arrays. There is two main usage syntax about PHP foreach.

PHP提供了foreach以便迭代给定的数组。 有关PHP foreach的主要用法有两种。

值语法 (Value Syntax)

In value syntax, we will provide just the array which is named ARRAY_EXPRESSION and the VALUE. We will write operations to the STATEMENT

用值语法,我们将只提供名为ARRAY_EXPRESSION的数组和VALUE。 我们将操作写入STATEMENT

foreach (ARRAY_EXPRESSION as $VALUE)
    STATEMENT

In this example, we will print array values to the standard output.

在此示例中,我们将数组值打印到标准输出。

<?php
$arr = array(1, 2, 3, 4);

foreach ($arr as $value) {
    echo $value;
}
?>

键,值语法 (Key, Value Syntax)

In Key and Value syntax will also get the key from the array and use it in the foreach.

在“键和值”中,语法还将从数组中获取键,并在foreach中使用它。

foreach (ARRAY_EXPRESSION as $KEY=>$VALUE)
    STATEMENT

In this example, we will print key and values into standard output

在此示例中,我们将键和值打印到标准输出中

<?php
$arr = array(1, 2, 3, 4); 
foreach ($arr as $key=>$value) {
    echo $value;
}
?>

JavaScript Foreach (JavaScript Foreach)

JavaScript is a very extensible language where most of the features are provided by objects. foreach is also provided by the object which is an array. It has the following syntax.

JavaScript是一种非常可扩展的语言,其中的大多数功能都是由对象提供的。 foreach也由数组对象提供。 它具有以下语法。

JavaScript Foreach语法 (JavaScript Foreach Syntax)

ARRAY.forEach(function(VALUE) {
  STATEMENT;
});

In this example, we will iterate over array named arr and print to the console.

在此示例中,我们将遍历名为arr数组并打印到控制台。

var arr = ['a', 'b', 'c','d','e']

arr.forEach(function(element) {
  console.log(element);
});

Java Foreach的 (Java Foreach)

Java programming language actually does not provides native foreach keyword. But we can use other elements of Java like List, ArrayList and for.

Java编程语言实际上不提供本机的foreach关键字。 但是我们可以使用Java的其他元素,例如ListArrayListfor

对于语法 (For Syntax)

We will use List and for to create foreach mechanism. TYPE is VALUE type and we will provide LIST.

我们将使用Listfor创建foreach机制。 TYPE是VALUE类型,我们将提供LIST。

for(TYPE VALUE:LIST){
  STATEMENT
}

We will iterate over string list and we will print to the standard output.

我们将遍历字符串列表,并打印到标准输出。

List<String> items = new ArrayList<>("A","B","C","D");

for(String item : items){
    System.out.println(item);
}

Foreach语法 (Foreach Syntax)

After Java 8 List type provides forEach() function. So we can create a LIST and then iterate over elements with forEach() function. We will also use a lambda expression.

Java 8之后, List类型提供了forEach()函数。 因此,我们可以创建一个LIST,然后使用forEach()函数遍历元素。 我们还将使用lambda表达式。

List<String> items = new ArrayList<>("A","B","C","D");
items.forEac(item->System.out.println(item));

C#的Foreach (C# Foreach)

C # provides the foreach statement in a native way.  We will provide the LIST with the in keyword and set ELEMENT with the given TYPE.

C#以本机方式提供了foreach语句。 我们将为LIST提供in关键字,并为ELEMENT设置TYPE。

foreach (TYPE ELEMENT in LIST){
  STATEMENT
}

In this example, we will iterate over an integer list named lst.

在此示例中,我们将遍历一个名为lst的整数列表。

var lst= new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };

foreach (int element in lst) {

   Console.WriteLine($"Element {element}");

}

Python Foreach (Python Foreach)

Python programming language does not provide the foreach keyword but the actual implementation of the for in Python is hte same as the foreach. We can iterate over List, Array, Dictionary type in python with for like below.

Python编程语言不提供foreach关键字,但是for在Python中的实际实现与foreach相同。 我们可以使用下面的for迭代python中的ListArrayDictionary类型。

for ELEMENT in LIST:
   STATEMENT

In this example, we will iterate over list named lst.

在此示例中,我们将遍历名为lst列表。

lst=[1,2,3,4,5]

for element in lst:
   print element
LEARN MORE  Linux Bash While Loop Tutorial with Examples
了解更多带有示例Linux Bash While循环教程

翻译自: https://www.poftut.com/foreach-usage-in-php-javascript-java-c-python-programming-languages-with-examples/

c#foreach的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值