From af8db98f4f0355f8fe43d29fd810d5203dc32913 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Apr 2020 15:05:22 -0400 Subject: [PATCH 1/5] Add nested loops The beginner loops tutorial does not contain any examples of nested loops. Add a beginner example to both the interactive and local versions. Fixes #17208 --- .../branches-and-loops-local.md | 36 +++++++++++++++++ .../intro-to-csharp/branches-and-loops.yml | 40 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md index a489d95525ef9..c0a7a5ea63341 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md @@ -276,6 +276,42 @@ Experiment with these yourself. Try each of the following: When you're done, let's move on to write some code yourself to use what you've learned. +## Created nested loops + +A `while`, `do` or `for` loop can be nested inside another loop to create a matrix using the combination of each item in the outer loop with each item in the inner loop. Let's do that to build a set of alpha-numeric pairs to represent rows and columns. + +One `for` loop can generate the rows: + +```csharp +for (int row = 1; number < 11; number++) +{ + Console.WriteLine($"The row is {row}"); +} +``` + +Another loop can generate the columns: + +```csharp +for (char column = 'a'; column < 'k'; column++) +{ + Console.WriteLine($"The column is {column}"); +} +``` + +You can nest one loop inside the other to form pairs: + +```csharp +for (int row = 1; number < 11; number++) +{ + for (char column = 'a'; column < 'k'; column++) + { + Console.WriteLine($"The cell is ({row}, {column})"); + } +} +``` + +You can see that the outer out loop increments once for each iteration of the inner loop. Reverse the row and column nesting, and see the changes for yourself. + ## Combine branches and loops Now that you've seen the `if` statement and the looping diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml index a107753d23efa..44d0d7deb4521 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml @@ -237,6 +237,46 @@ items: > [!NOTE] > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). +- title: Created nested loops + durationInMinutes: 10 + content: | + A `while`, `do` or `for` loop can be nested inside another loop to create a matrix + using the combination of each item in the outer loop with each item in the inner + loop. Let's do that to build a set of alpha-numeric pairs to represent rows and columns. + + One `for` loop can generate the rows: + + ```csharp + for (int row = 1; number < 11; number++) + { + Console.WriteLine($"The row is {row}"); + } + ``` + + Another loop can generate the columns: + + ```csharp + for (char column = 'a'; column < 'k'; column++) + { + Console.WriteLine($"The column is {column}"); + } + ``` + + You can nest one loop inside the other to form pairs: + + ```csharp + for (int row = 1; number < 11; number++) + { + for (char column = 'a'; column < 'k'; column++) + { + Console.WriteLine($"The cell is ({row}, {column})"); + } + } + ``` + + You can see that the outer out loop increments once for each iteration of the + inner loop. Reverse the row and column nesting, and see the changes for yourself. + - title: Combine branches and loops durationInMinutes: 12 content: | From d5f9b0b2f90bf01776a779254490d5f08ae75e15 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Apr 2020 15:11:00 -0400 Subject: [PATCH 2/5] fix markdown lint error --- .../tutorials/intro-to-csharp/branches-and-loops-local.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md index c0a7a5ea63341..dcf2c92caebe8 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md @@ -299,7 +299,7 @@ for (char column = 'a'; column < 'k'; column++) ``` You can nest one loop inside the other to form pairs: - + ```csharp for (int row = 1; number < 11; number++) { From 03c9dd25f7d6fcfa43b76d6a4c4718d9def41477 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Apr 2020 15:13:58 -0400 Subject: [PATCH 3/5] fix variable name --- .../tutorials/intro-to-csharp/branches-and-loops-local.md | 4 ++-- docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md index dcf2c92caebe8..6168430c6b477 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md @@ -283,7 +283,7 @@ A `while`, `do` or `for` loop can be nested inside another loop to create a matr One `for` loop can generate the rows: ```csharp -for (int row = 1; number < 11; number++) +for (int row = 1; row < 11; row++) { Console.WriteLine($"The row is {row}"); } @@ -301,7 +301,7 @@ for (char column = 'a'; column < 'k'; column++) You can nest one loop inside the other to form pairs: ```csharp -for (int row = 1; number < 11; number++) +for (int row = 1; row < 11; row++) { for (char column = 'a'; column < 'k'; column++) { diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml index 44d0d7deb4521..bae86c1852c09 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml @@ -247,7 +247,7 @@ items: One `for` loop can generate the rows: ```csharp - for (int row = 1; number < 11; number++) + for (int row = 1; row < 11; row++) { Console.WriteLine($"The row is {row}"); } @@ -265,7 +265,7 @@ items: You can nest one loop inside the other to form pairs: ```csharp - for (int row = 1; number < 11; number++) + for (int row = 1; row < 11; row++) { for (char column = 'a'; column < 'k'; column++) { From 6712c0ffee2c53f7973e88460a90ad382a2d1605 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Apr 2020 15:48:21 -0400 Subject: [PATCH 4/5] Apply suggestions from code review Co-Authored-By: Petr Kulikov --- .../tutorials/intro-to-csharp/branches-and-loops-local.md | 2 +- docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md index 6168430c6b477..4a5182a66285f 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md @@ -310,7 +310,7 @@ for (int row = 1; row < 11; row++) } ``` -You can see that the outer out loop increments once for each iteration of the inner loop. Reverse the row and column nesting, and see the changes for yourself. +You can see that the outer loop increments once for each full run of the inner loop. Reverse the row and column nesting, and see the changes for yourself. ## Combine branches and loops diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml index bae86c1852c09..298b4c3057c71 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml @@ -274,7 +274,7 @@ items: } ``` - You can see that the outer out loop increments once for each iteration of the + You can see that the outer loop increments once for each full run of the inner loop. Reverse the row and column nesting, and see the changes for yourself. - title: Combine branches and loops From fde144e012b3183db1c5ff3813ae3c26e60b21b4 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Apr 2020 16:36:04 -0400 Subject: [PATCH 5/5] Apply suggestions from code review Co-Authored-By: Tom Dykstra --- .../tutorials/intro-to-csharp/branches-and-loops-local.md | 2 +- docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md index 4a5182a66285f..3bf2b130268b1 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops-local.md @@ -278,7 +278,7 @@ use what you've learned. ## Created nested loops -A `while`, `do` or `for` loop can be nested inside another loop to create a matrix using the combination of each item in the outer loop with each item in the inner loop. Let's do that to build a set of alpha-numeric pairs to represent rows and columns. +A `while`, `do` or `for` loop can be nested inside another loop to create a matrix using the combination of each item in the outer loop with each item in the inner loop. Let's do that to build a set of alphanumeric pairs to represent rows and columns. One `for` loop can generate the rows: diff --git a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml index 298b4c3057c71..93a48cab84e15 100644 --- a/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml +++ b/docs/csharp/tutorials/intro-to-csharp/branches-and-loops.yml @@ -242,7 +242,7 @@ items: content: | A `while`, `do` or `for` loop can be nested inside another loop to create a matrix using the combination of each item in the outer loop with each item in the inner - loop. Let's do that to build a set of alpha-numeric pairs to represent rows and columns. + loop. Let's do that to build a set of alphanumeric pairs to represent rows and columns. One `for` loop can generate the rows: