Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# loops
0:00 Next thing in C# I want to look at
0:01 and then create in Python
0:03 is loops, different kinds of loops
0:05 and iterations and things like that.
0:07 So here we have a simple C# program
0:10 and it's going to start having a loop here.
0:13 It's going to come through and say
0:15 while true, it's going to ask the user, what is your name
0:18 and it's going to greet them as long as they enter a name.
0:21 If they don't, it's going to break out.
0:23 I guess we could really shorten this if we wanted
0:27 as Visual Studio was suggesting, there we go.
0:31 I just want to read it in, check it, and break out.
0:34 We're also then going to take an integer array here.
0:38 We have a pre-defined nums, it contains a bunch of integers.
0:41 1, 5, 8, 10 and so on.
0:43 And we're going to use one of the nicest features
0:45 of C# in terms of iteration stuff, I believe
0:48 is the foreach loop, right?
0:49 So much better than the for loop, which is down here.
0:54 So in the foreach loop, we're just going to round.
0:55 The next number is this.
0:56 But it has some shortcomings
0:58 like maybe I want to say the first number is this
1:00 the second number is that, the third number is that.
1:03 We're going to fall back to our for loop
1:05 and get that number, but also have the index say
1:07 the first, second, and so on.
1:09 Let's just run this program real quick.
1:10 I changed program over here CS in this project
1:13 to run that one. We run it.
1:16 It says, what is your name?
1:17 Remember, it's going to ask me my name
1:18 and keep greeting me as long as I say a name
1:20 so I'll say my name is Michael.
1:22 My name is Zoe.
1:23 And if I'm done, I hit Enter.
1:25 It's going to go to the next section
1:26 and just work with the foreach loops.
1:28 So here it loops through those numbers.
1:30 The next number is 1, 5, 8, 10, 7, 2, great.
1:34 The next, next, next, how great is that?
1:36 We said no, no, no.
1:37 We're going to use a for loop
1:38 and we're going to have the first number as 1
1:40 the second number is 5
1:42 now obviously it should be first, not firth
1:44 or however you pronounce that right there.
1:46 But we're not going to sweat the details.
1:48 We could obviously add that adjustment
1:51 for the suffix of these letters
1:52 but first is 1, second is 5, third, 8, and so on.
1:58 This project in C# works with most
2:01 of the structures of looping that we have
2:03 in the language.