+ 6

How do pattern codes work?

Hello guys. As I'm a beginner in programming, I'm having a hard time understanding the logic of code that shows a pattern on the console. I picked up a simple one to study: http://www.c-sharpcorner.com/UploadFile/2072a9/triangle-patterns-in-C-Sharp/ It seems to me to be extremely simple and so it is difficult to find a detailed explanation. Even beginner-oriented tutorials say nothing about. And sorry for my english. :x

17th Feb 2018, 1:53 PM
rfPeixoto
rfPeixoto - avatar
5 Respuestas
+ 17
int val = 5;          int i, j, k ;          for (i = 1; i <= val; i++)          {             for (j = 1; j <= i; j++)             {                Console.Write("");             }             for (k = 1; k <= i; k++)             {                Console.Write("*");             }             Console.WriteLine("");          }   Here there r 2 for loops inside 1 for loop the 1st for loop will run 5 times for values 1 to 5... basically 2nd for loop does nothing... u can remove it if u want third for loops print * --> 1st for loop i = 1, 3rd for loop executes for value of i = 1 3rd for loop runs 1 time as k = 1 & k<=1 so only 1 star is printed -->Console.WriteLine is there to move cursor to next line after printing star* ... -->again it goes to 1st for loop by getting i incremented now i = 2 .... so again the loops inside are executed... this time the for loop having k variable will run twice... and 2 stars * will be printed... https://code.sololearn.com/c6i5RF43Zl5a/?ref=app
17th Feb 2018, 2:39 PM
🌛DT🌜
🌛DT🌜 - avatar
+ 16
int val = 5;          int i, j, k ;          for (i = 1; i <= val; i++)          {                for (k = 1; k <= i; k++)             {                Console.Write("*");             }             Console.WriteLine("");          } this will output the same Console.WriteLine("") plays a very important role... U should use it wisely..
17th Feb 2018, 2:45 PM
🌛DT🌜
🌛DT🌜 - avatar
+ 6
Haha, thank you. :> Well, my problem is not exactly with loops, but with that kind of loop specifically. What, exactly, is each thing doing? As in this example, we have four variables. I know that one represents the boundary of the pattern lines while the others draw the characters, but I can not identify where and how this is being done. Whenever I try to do something like this without reference codes I end up drawing a single character per line even using Console.Write () ;.
17th Feb 2018, 2:42 PM
rfPeixoto
rfPeixoto - avatar
+ 6
Thank you so much, 🌛DT🌜. Now I understand, now it all makes sense, and so on. And thank you too, Alex. o/
17th Feb 2018, 2:48 PM
rfPeixoto
rfPeixoto - avatar
+ 3
*writes question in good understandable english* *apologizes for bad english* Your english is fine :P Now your question: Your problems are the for loops in general or nested loops like in these examples?
17th Feb 2018, 2:09 PM
Alex
Alex - avatar