Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- =============================
- static void Main(string[] args)
- Console.Write("How many rows?: ");
- int rows = Convert.ToInt32(Console.ReadLine);
- Console.Write("How many columns?: ");
- int rows = Convert.ToInt32(Console.ReadLine);
- Console.Write("What symbol?: ");
- String symbol = Console.ReadLine;
- for (int i = 0; i < rows; i++);
- {
- for (int j = 0; j < columns; j++);
- {
- Console.Write(symbol);
- }
- }
- Console.ReadKey();
- }
- }
- }
- =============================
- rectangle.c
- #include<stdio.h>
- #include<conio.h>
- int main(){
- int width,height; // dimensions
- int w,l;
- printf("Enter the width of rectangle: ");
- scanf("%d",&width);
- printf("Enter the height of rectangle: ");
- scanf("%d",&height);
- for(w=1; w<=height; w++){
- for(l=1; l<=width ; l++){
- printf("*");
- }
- printf("\n");
- }
- getch();
- return 0;
- }//end main
- =============================
- triangle.c
- #include<stdio.h>
- #include<conio.h>
- int main(){
- int size; //size of half triangle
- int w,i; //loop variables
- printf("Enter the size of the triangle : ");
- scanf("%d",&size);
- //upper half of th triangle
- for( w=1; w<=size; w++){
- for( i=1; i<=w;i++){
- printf("*");
- }
- printf("\n");
- }
- // lower half of the triangle
- for(w=1;w<=size;w++){
- for (i= size-1;i>=w;i--){
- printf("*");
- }
- printf("\n");
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement