From the course: Grasshopper and Rhino: C# Scripting
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Foreach looping
- [Instructor] The for loop in C# allows us to loop until a certain condition is met. Sometimes however, we may want to just loop over a collection of items, such as a list or an array, and to do that, we can use the foreach loop. The foreach loop works differently than the for loop in that we don't define the condition to end the loop. That is done for us, which is when we have completely looped through a collection of items. This means that the format is a little bit different and looks like this. We start with the keyword foreach followed by open and close parentheses. Within these parentheses, we include three parts. The first is declaring a temporary variable, which means we specify the type of object that we're looping over and the variable name that suits it. This will be updated with each item as we loop through the collection. Next, we have the keyword in, which is used to identify the collection we are…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Commenting2m 27s
-
Variables5m 1s
-
Numbers6m 36s
-
Strings5m 37s
-
Collections5m 34s
-
Arrays5m 32s
-
Comparing with operators5m 3s
-
Conditionals3m 30s
-
Logical operators6m 26s
-
Looping with the for loop5m 23s
-
Foreach looping4m 29s
-
Nested loops5m 12s
-
Methods6m 5s
-
Reference vs. value types5m 21s
-
Try and catch4m 14s
-
-
-