#3.4 Java Tutorial | Nested Loops | Iteration Statement

preview_player
Показать описание
After learning about the loops, now let's learn what are nested loops.
- Instead of writing a statement again and again, we write it once in a loop.
- We can also use a loop inside a loop and that is called nested loop.
- Nested loop means a loop statement inside another loop statement. Thus, it is also called a "loop inside loop".
- The inner loop executes completely whenever the outer loop executes.

Syntax:
for ( initialization; condition; increment ) {
for ( initialization; condition; increment ) {
// statement of inside loop
}
// statement of outer loop
}
- Same can be done with a while loop and do while loop.
- A square or many other patterns can be print with the help of nested loops.

Assignment:
Based on the logic that we have seen in this video. You have to build a cold to print some patterns.
1 A $$$$
12 AB $ $
123 ABC $ $
1234 $$$$
12345
123456
- You can use ASCII values to print the alphabet.
You can also check the video to upload the assignment on the GitHub.

In this video we will see:
- What are nested loops?
- Use of nested loops
- Syntax of nested loops
- Assignment based on nested loops

More Learning :

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Рекомендации по теме
Комментарии
Автор

This channel is a gem for students like me..thank you very much sir ❤

isurusandaruwan
Автор

Question 3 :-
public class Nested {
public static void main(String[] args) {

for (int i = 1; i <=5; i++)
{
if(i==1 || i==5)
for(int j=1; j <=5; j++ )
{
System.out.print("$");
}
else
System.out.print("$ $");

System.out.println();
}
}
}


There is a simple trick. The outer loop decides the row and the inner loop decides the column. If you get what I mean then you can solve any pattern problem.

shubhambhoite
Автор

A
AB
ABC
solution:
public class SecondPattern {
public static void main(String[] args) {
char c;
char d=67;
char a;
for(c=65;c<=d;c++) {
for(a=65;a<=c;a++)
{
System.out.print(a);
}
System.out.println();
}
}

}

joggajasus
Автор

Source code of the 1st assignment :
public class assignment
{
public static void main(String args[])
{
int i, j;
for(i=1; i<=6; i++)
{
for(j=1; j<=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Source code of the 2nd assignment :
public class assignment
{
public static void main(String args[])
{
int i, j;
char m;
for(i=0; i<3; i++)
{
m=65;
for(j=0; j<=i; j++)
{
System.out.print((m++)+" ");
}
System.out.println();
}
}
}

Source code of the 3rd assignment :
public class assignment
{
public static void main(String args[])
{
int i, j;
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(i==0 || j==0 || i==3 || j==3)
");
else
");
}
System.out.println();
}
}
}
It might not be the most appropriate method. But it will do the job for starters
Thank You

aakashgowala
Автор

for the pattern 1
12
123
1234
12345
123456


public class Assignment {


public static void main(String[] args) {


for (int i=1;i<=6;i++) {

for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}

}
}

trishashetty
Автор

Sir, your tutorials are really helpful. In one of your live videos, you had stated that your office also provides internship. Can you please specify the procedure of how to apply for that?

riamitra
Автор

Might be a more elegant way... but, here's mine.

public class NestedLoop34Assign3
{
public static void main(String[] args)
{

for (int row = 1; row<=4; row++)
if (row ==1 || row==4)
System.out.println("$$$$");
else
System.out.println("$ $");
}
}

wabbott
Автор

1)
public static void main(String[] args) {

for(int i = 1; i<=6;i++)
{
for(int j = 1; j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}

}
}

rohitkabadi
Автор

Thanku so much sir😢
U upload this on correct time

himanshusingh
Автор

great that's it!! is nested loops only contains this much concept?

ap
Автор

Thanks sir but you should upload much more videos daily.when spring framework videos will come ??

chinmaydas
Автор

Anna, I'm unable to understand how nested loops works. My bad.

shivagunti
Автор

This is the answer to the Assignment #1, but like he said do it with Asterisk's.


for (int i = 0;i <= 5; i ++) {
System.out.print(" * ");
for(int j = 1;j<=i;j++) {
System.out.print(" * "); }
System.out.println();}
for(int b = 1;b<=5;b++) {
for(int n = 5 ;n >= b;n--) {
System.out.print(" * ");}
System.out.println();}

robmcguire
Автор

Sir ur doing a great job many many thanks

sharmaavinash
Автор

Thankyou sir
It was a great help to me

daliasarkar
Автор

Hello Sir, I wants to improve my programming logic ...
Can you please tell me how can I ....??

jagdishsaner
Автор

System .out .print

" out.printIn

what is the difference?

pschelp
Автор

I used scanner to do this more like a user input based way
EDIT: ADDED "sc.close();"

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.close();
System.out.println("Enter Row and Column one by one");
int row = sc.nextInt();
int col = sc.nextInt();
for (int i = 1; i <=row; i++) {
for (int j = 1; j <= col; j++)
if((i==1 || i==col) || (j==1 || j==col))
System.out.print("*");
else
System.out.print(" ");

System.out.println();
}
}
}

TheSpectreNinja
Автор

The first Pattern can be done in just 2 lines (Not including the Public Static Void and the public class

public class FirstCode {
public static void main(String args[])
{
for(int b=1;b<=4;b++, System.out.println())
for(int a=0;a<b;a++, System.out.print(a+" "));
}
}

redstone_mason
Автор

This is what I did for the dollar box:
for (int i = 0; i < 1; i++) {

System.out.println("$ $ $ $");

for (int j = 0; j < 2; j++) {

System.out.println("$ $");

}

System.out.println("$ $ $ $");

}

aSiaN