For Each Loop
For each loops are really amazing for what they do. They can be very limited in their application, but when applicable they shorten you code tremendously
Flowchart
In code
Here is what the for loop looks like in code:
using System;
namespace Loop
{
class ForEachLoop
{
public static void Main(string[] args)
{
char[] myArray = {'H','e','l','l','o'};
foreach(char ch in myArray)
{
Console.WriteLine(ch);
}
}
}
}
using System;
namespace Loop
{
class ForEachLoop
{
public static void Main(string[] args)
{
char[] myArray = {'H','e','l','l','o'};
foreach(char ch in myArray)
{
Console.WriteLine(ch);
}
}
}
}
Explanation of the code
Assignment
So in this lesson what you are going to create is:
1. a program that has a ForEach loop in it.
2. print out to the console the list of name provided below
Tony Stark, Barry Allen, Steve Rogers, Clark Kent, Natalia Romanov, and Diana Prince
1. a program that has a ForEach loop in it.
2. print out to the console the list of name provided below
Tony Stark, Barry Allen, Steve Rogers, Clark Kent, Natalia Romanov, and Diana Prince
Helpful hints
1. use a string variable and enter each name from above into it
Example
string HeroNames = "Black Adam Aquaman BatMan ";
Example
string HeroNames = "Black Adam Aquaman BatMan ";