Advertisement
alien_fx_fiend

C# Nested For Loops —Bro Code (YT Viddy)

Sep 13th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. hey what's going on everybody it's you bro hope you're doing well and in this video we're going to use nested loops to create a program to draw a rectangle for us made out of a certain symbol that we choose so sit back relax and enjoy the show all right welcome back so nested loops a nested loop is a loop that's inside of another loop there is an outer loop and an inner loop and both of these combined are nested loops so the uses they vary they're used a lot in sorting algorithms so i thought let's create a program where we will draw a rectangle we'll have the outer loop in charge of counting the rows and the inner loop in charge of counting the columns and we will let the user choose a symbol that they would like to create their rectangle of so we'll need to accept some user input so console.writeline let's ask how many rows how many rows actually let's make this a write statement okay and then we will store this within int rows equals then we will need to convert some user input convert 2 and 32 because user input's always a string then within the parenthesis console.read line okay let's ask for columns so let's copy this paste it how many columns int columns then let's ask the user for a symbol to use what symbol or character and let's use a string i guess you can use a char as well string symbol and we do not need to convert this because it's already a string okay we'll have an outer loop in charge of counting the rows and the inner loop is in charge of counting the columns so for then we need our index by the way it doesn't matter if you use a while loop or a for loop it's just the concept of having a loop inside of another loop i just so happen to be using a for loop for the outer loop and a for loop for the inner loop so int i equals zero we will continue this as long as i is less than our rows because we're letting the user choose how many rows then we will increment i by one during each iteration now we need an inner for loop and let's just copy this now one naming convention with the index of the inner for loop since i is already taken what people usually use for an index for the inner loop is j because j comes after i i guess so into j equals zero j is less than columns then j plus plus okay now what we'll do within the inner for loop is display our symbol so console dot right make sure you use right and not right line we will display our symbol so this inner loop is in charge of the columns the outer loop is a charge of rows basically how this is going to work is that once we enter our outer for loop we will immediately enter our inner for loop in order to complete one iteration of the outer for loop we have to finish all iterations of the inner for loop and once we finish all iterations of the inner for loop we can complete one iteration of the outer for loop and during the next iteration we have to do it all over again so on the next iteration we would have to complete all the iterations for the inner for loop so this will display a rectangle made out of a certain symbol however there is one thing missing and i'll show you how many rows let's say four how many columns five and what symbol uh what about the i don't know that sign all right so this ended up in one long row so after we complete our iterations for the inner for loop we should probably move down to the next row kind of like a typewriter we finish one line then we move down to the next row so let's use an empty writeline statement just to move down to the next line so if we try this again okay four rows five columns let's use an at sign there we go there's a rectangle we have one two three four five columns and one two three four four rows so basically a nested loop is really just a loop that's inside of another loop when you'll encounter it it really varies but they're used a lot in sorting algorithms and this was a small program just to practice working with nested loops all right so if you want a copy of this code i will post this to the comment section down below if you liked this video give it a thumbs up drop a random comment down below and that is an example of nested loops in c sharp https://youtu.be/WFzLcZk137s Bro Code https://www.sourcecodester.com/book/7601/nested-loops.html
  2.  
  3. =============================
  4.  
  5. static void Main(string[] args)
  6. Console.Write("How many rows?: ");
  7. int rows = Convert.ToInt32(Console.ReadLine);
  8.  
  9. Console.Write("How many columns?: ");
  10. int rows = Convert.ToInt32(Console.ReadLine);
  11.  
  12. Console.Write("What symbol?: ");
  13. String symbol = Console.ReadLine;
  14.  
  15. for (int i = 0; i < rows; i++);
  16. {
  17. for (int j = 0; j < columns; j++);
  18. {
  19. Console.Write(symbol);
  20. }
  21. }
  22. Console.ReadKey();
  23. }
  24. }
  25. }
  26.  
  27. =============================
  28.  
  29. rectangle.c
  30. #include<stdio.h>
  31. #include<conio.h>
  32.  
  33. int main(){
  34.  
  35. int width,height; // dimensions
  36. int w,l;
  37.  
  38. printf("Enter the width of rectangle: ");
  39. scanf("%d",&width);
  40.  
  41. printf("Enter the height of rectangle: ");
  42. scanf("%d",&height);
  43.  
  44. for(w=1; w<=height; w++){
  45. for(l=1; l<=width ; l++){
  46. printf("*");
  47. }
  48. printf("\n");
  49. }
  50. getch();
  51. return 0;
  52. }//end main
  53.  
  54. =============================
  55.  
  56. triangle.c
  57. #include<stdio.h>
  58. #include<conio.h>
  59.  
  60. int main(){
  61.  
  62. int size; //size of half triangle
  63. int w,i; //loop variables
  64.  
  65. printf("Enter the size of the triangle : ");
  66. scanf("%d",&size);
  67.  
  68. //upper half of th triangle
  69. for( w=1; w<=size; w++){
  70. for( i=1; i<=w;i++){
  71. printf("*");
  72.  
  73. }
  74. printf("\n");
  75. }
  76.  
  77. // lower half of the triangle
  78. for(w=1;w<=size;w++){
  79. for (i= size-1;i>=w;i--){
  80. printf("*");
  81. }
  82. printf("\n");
  83. }
  84. getch();
  85. return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement