#3.4 Java Tutorial | Nested Loops | Iteration Statement

///#3.4 Java Tutorial | Nested Loops | Iteration Statement

#3.4 Java Tutorial | Nested Loops | Iteration Statement

FavoriteLoadingAdd to favorites

Complete Java for Beginner Playlist :

Java468x60

Github :-

Assignment question at the end of the video

In this video we will see :
– Printing star pattern
– Need for nested loop
– Example of nested loop
– Inner loop
– Assignment for nested loop

Editing Monitors :

Check out our website:

Follow Telusko on Twitter:

Follow on Facebook:
Telusko :
Navin Reddy :

Follow Navin Reddy on Instagram:

Subscribe to our other channel:
Navin Reddy :
Telusko Hindi :

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20

source

By |2020-11-16T10:35:43+00:00November 16th, 2020|Java Video Tutorials|33 Comments

33 Comments

  1. shelton kertich November 16, 2020 at 10:35 am - Reply

    import java.util.ArrayList;
    import java.util.List;

    public class firstclass {

    public static void main(String[] args) {
    List<String> j = new ArrayList<String>();
    for (int i = 1; i <= 6; i++) {
    //j.add(Integer.toString(i));
    System.out.println(j.toString().replace("[", "").replace("]", "").replace(",", ""));

    }

    }
    }

  2. Agnita Saha November 16, 2020 at 10:35 am - Reply

    First Assignment:
    public class NestedLoop3 {

    public static void main(String[] args) {

    for(int i=0;i<6;i++)

    {

    for(int j=1;j<i+2;j++)

    {

    System.out.print(j+" ");

    }

    System.out.println();

    }

    }

    }

  3. ms preethi November 16, 2020 at 10:35 am - Reply

    public class pat2
    {
    public static void main(String args[])
    {
    for(int j=1;j<=3;j++)
    { char a=65;
    for(int i=1;i<=j;i++)
    {System.out.print(a+" ");
    a++;}

    System.out.println();
    }
    }
    }
    o/p:
    A
    A B
    A B C

  4. jetti teja November 16, 2020 at 10:35 am - Reply

    ASSIGNMENT
    1.
    public class nested_loop

    {

    public static void main(String agrs[])

    {

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

    {

    for(int j=1;j<=i;j++)

    {

    System.out.print(j+" ");

    }

    System.out.println();

    }

    }

    }

    2.
    public class assignment_loop

    {

    public static void main(String agrs[])

    {

    for(int i=65;i<=67;i++)

    {

    for(int j=65;j<=i;j++)

    {

    char x=(char)j;

    System.out.print(x+" ");

    }

    System.out.println();

    }

    }

    }

    3.
    public class assign_2_loops

    {

    public static void main(String agrs[])

    {

    for(int i=1;i<=4;i++)

    {

    for(int j=1;j<=4;j++)

    {

    if(i==1 || j==1 || i==4 || j==4)

    System.out.print("$ ");

    else

    System.out.print(" ");

    }

    System.out.println();

    }

    }

    }

  5. aaruni a November 16, 2020 at 10:35 am - Reply

    3)
    for(int i=1; i<=4; i++)

    {

    for (int j=1; j<=4; j++)

    {

    if (i==1 || i ==4)

    {

    System.out.print("$ ");

    }

    if(i==2 || i==3)

    {

    if(j==1)

    {

    System.out.print("$ ");

    }

    if(j==2)

    {

    System.out.print(" ");

    }

    if(j==3)

    {

    System.out.print(" ");

    }

    if(j==4)

    {

    System.out.print("$ ");

    }

    }

    }

    System.out.println();

    }

  6. PB 10 November 16, 2020 at 10:35 am - Reply

    1.
    public class ANo_1

    {

    public static void main(String a[])

    {

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

    {

    for(int j=1;j<=i;j++)

    System.out.print(j+" ");

    System.out.println();

    }

    }

    }

    2.
    public class ANo_2

    {

    public static void main(String a[])

    {

    for(int i=1;i<=3;i++)

    {

    for(int j=0;j<i;j++)

    {

    char b = (char)((int)'A'+j);

    System.out.print(b+" ");

    }

    System.out.println();

    }

    }

    }

    3.
    public class ANo_3

    {

    public static void main(String a[])

    {

    for(int i=0;i<4;i++)

    {

    for(int j=0;j<4;j++)

    {

    if(i==0|i==3|j==0|j==3)

    System.out.print("$ ");

    else

    System.out.print(" ");

    }

    System.out.println();

    }

    }

    }

  7. Yashan Bansal November 16, 2020 at 10:35 am - Reply

    Pattern 3 Code: Put the value in a whatever u want 🙂

    public class Main
    {
    public static void main(String args[])
    {
    int i,j,k,a=4;
    for(i=1;i<=a;i++)
    {
    if(i==1 || i==a)
    {
    for(j=1;j<=a;j++)
    {
    System.out.print("$ ");
    }
    }
    else
    {
    for(k=1;k<=a;k++)
    {
    if(k==1 || k==a)
    {
    System.out.print("$ ");
    }
    else
    {
    System.out.print(" ");
    }
    }
    }
    System.out.println();
    }
    }
    }

  8. Redstone Mason November 16, 2020 at 10:35 am - Reply

    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+" "));

    }

    }

  9. Dulanaka Navojith Hewavitharana November 16, 2020 at 10:35 am - Reply

    public class Operatordemo {

    public static void main(String[] args)

    {

    //Project#1

    int n=1;

    for(int j=1;j<=6;j++)

    {

    for(int i=1;n>=i;i++)

    {

    System.out.print(i);

    }

    n++;

    System.out.println();

    }

    //Project#2

    for (int l=1;l<=3;l++)

    {

    char k ='A';

    for (int p=1;p<=l;p++)

    {

    System.out.print(k+ " ");

    k++;

    }

    System.out.println();

    }

    //Project#3
    (This code correct foe any 'm' value)

    int m=5;

    int q=4;

    int z=m;

    int o=m-q;

    int p=z+o;

    for(int j=1;j<=m;j++)

    {

    if (j%(m-1)==1)

    {

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

    System.out.print("$ ");

    }

    System.out.println();

    }

    else {

    for(int s=1;s<=m;s++) {

    if (s%(m-1)==2) {

    System.out.print("$ ");

    for (int x=1;x<=p;x++) {

    System.out.print(" ");}

    }

    if(m==3)

    {

    if (s-1%(m-1)==1) {

    System.out.print("$ ");

    for (int x=1;x<=p;x++) {

    System.out.print(" ");}

    }

    }

    if (s%(m-1)==0) {

    System.out.print("$ ");

    for (int x=1;x<p;x++) {

    System.out.print(" ");}

    }

    }

    System.out.println();

    }

    }

    }

    }

  10. Anil Dumane November 16, 2020 at 10:35 am - Reply

    SOLUTION for problem 2
    /*package whatever //do not write package name here */

    import java.io.*;

    class GFG {
    public static void main (String[] args) {
    char i,j;
    for(i=65;i<=67;i++)
    {
    for(j=65;j<i;j++){
    System.out.print(""+j);
    }
    System.out.println(""+i);
    }
    }
    }

  11. Anil Dumane November 16, 2020 at 10:35 am - Reply

    /*package whatever //do not write package name here */

    import java.io.*;

    class GFG {
    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(""+i);
    }
    }
    }
    SOLUTION FOR FIRST PROBLEM

  12. Aakash Gowala November 16, 2020 at 10:35 am - Reply

    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)
    System.out.print("$ ");
    else
    System.out.print(" ");
    }
    System.out.println();
    }
    }
    }
    It might not be the most appropriate method. But it will do the job for starters
    Thank You

  13. ch Navin November 16, 2020 at 10:35 am - Reply

    import java.util.Scanner;

    public class program3 {

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    int nums=sc.nextInt();

    int words=sc.nextInt();

    int stars=sc.nextInt();

    num(nums);

    word(words);

    star(stars);

    }

    private static void num(int nums)

    {

    for(int j=1;j<=nums;j++)

    {

    for(int i=1;i<=j;i++)

    {

    System.out.print(i);

    }

    System.out.print("n");

    }

    }

    private static void word(int words)

    {

    for(int i=0;i<words;i++)

    {

    for(int j=65;j<=65+i;j++)

    {

    char a = (char)j;

    System.out.print(a);

    }

    System.out.print("n");

    }

    }

    private static void star(int stars)

    {

    for(int i=0;i<stars;i++)

    {

    System.out.print("* ");

    }

    System.out.print("n");

    for(int j=0;j<stars-2;j++)

    {

    System.out.print("*");

    for(int i=0;i<(stars-2)*2;i++)

    {

    System.out.print(" ");

    }

    System.out.print(" *n");

    }

    for(int i=0;i<stars;i++)

    {

    System.out.print("* ");

    }

    }

    }

  14. Isuru Sandaruwan November 16, 2020 at 10:35 am - Reply

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

  15. Maharshi Soni November 16, 2020 at 10:35 am - Reply

    did he just call us aliens? lmao

  16. Bandhab Kumar Das November 16, 2020 at 10:35 am - Reply

    number pattern and alphabet pattern(use ascii) are cool, but for those stuck at "dollarbox" check out my solution
    public static void main(String args[])
    {

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

  17. Bhagat manas November 16, 2020 at 10:35 am - Reply

    for (int m = 1; m <= 4; m++) {

    System.out.printf("$");

    }

    System.out.println();

    for (int m = 1; m <= 2; m++) {

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

    System.out.println();

    }

    for (int m = 1; m <= 4; m++) {

    System.out.printf("$");

  18. sara n November 16, 2020 at 10:35 am - Reply

    thanks

  19. Abhishek Kumar November 16, 2020 at 10:35 am - Reply

    //for Square box code

    public class NewRough {

    public static void main(String[] args)
    {

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

    }

    System.out.println();
    }

    }

    }

  20. Shubham Bhoite November 16, 2020 at 10:35 am - Reply

    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.

  21. Dalia Sarkar November 16, 2020 at 10:35 am - Reply

    Thankyou sir
    It was a great help to me

  22. Bijaya shrestha November 16, 2020 at 10:35 am - Reply

    any other way to do the first pattern ??

    public class Loop

    {

    public static void main (String[] args)

    {

    for (int b=1;b<=6;b++)

    {

    for (int a=1;a<b;a++)

    {

    System.out.print(a+" ");

    }

    System.out.println();

    }

    }

    }

  23. Rob McGuire November 16, 2020 at 10:35 am - Reply

    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();}

  24. Rob McGuire November 16, 2020 at 10:35 am - Reply

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

    int numberDisplayed = 1;

    for(int innerLoop = 0;innerLoop <= outerLoop; innerLoop ++) {
    System.out.print(numberDisplayed);
    numberDisplayed++;
    }
    System.out.println();

    }//End outer loop

  25. Ravi Kiran November 16, 2020 at 10:35 am - Reply

    for(int j=1;j<=6;j++)

    {

    for(int i=1;i<=j;i++)

    {

    System.out.print(i);

    }
    System.out.println("");

    }
    OUTPUT:
    1
    12
    123
    1234
    12345
    123456

  26. Varadraj Chavan November 16, 2020 at 10:35 am - Reply

    class xxx
    {
    psvm()
    {
    int i;
    int a;
    for(i=1;i<=6;i++)
    {
    for(a=1;a<=6;a++)
    {
    sop(a);
    }
    sopln();
    }
    }
    }

  27. MoHmeT ToHo November 16, 2020 at 10:35 am - Reply

    for(int i=0; i<4; i++){
    System.out.print("$ ");
    }
    System.out.println();
    for(int i=0; i<4-2; i++){
    System.out.print("$");
    for(int j=0; j<4-2; j++){
    System.out.print(" ");
    }
    System.out.println(" $");
    System.out.println();
    }
    for(int i=0; i<4; i++){
    System.out.print("$ ");
    }
    System.out.println();

  28. Oneplus3ut Oneplus November 16, 2020 at 10:35 am - Reply

    – Answer of 1st question that is kind of 1,12,123….
    public class MyClass
    {
    public static void main(String args[])
    {
    for (int o = 1; o <= 6; o++)
    {
    for (int i = 1; i<=o; i++)
    {
    System.out.print( i + " ");
    }
    System.out.println();

    }
    }
    }
    – Answer of 2nd question that is kind of A,AB,ABC.
    public class MyClass
    {
    public static void main(String args[])
    {
    for (int o = 65; o <= 67; o++)
    {
    for (int i = 65; i<=o; i++)
    {
    System.out.print((char) i + " ");
    }
    System.out.println();

    }
    }
    }

    – Answer of 3rd question that is Dollar Box.
    public class MyClass
    {
    public static void main(String args[])
    {
    for(int o = 1; o<=1 ; o++)
    {
    System.out.print("$ $ $ $");
    System.out.println();

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

  29. yaoyao Xiong November 16, 2020 at 10:35 am - Reply

    did he call us idiots?

  30. Raj November 16, 2020 at 10:35 am - Reply

    Did you say Aliens @ 3:51? what you mean to all human subscribers??

  31. Attitude Adjustment November 16, 2020 at 10:35 am - Reply

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

  32. Logesh K November 16, 2020 at 10:35 am - Reply

    public class StateMent {

    public static void main(String[] argd) {

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

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

    System.out.print(i);

    }

    System.out.println("");

    }

    }

    }

  33. hari prasad November 16, 2020 at 10:35 am - Reply

    class ArrayDemo{

    public static void main(String args[]){

    int arr[][]={{1},{1,2},{1,2,3},{1,2,3,4},{1,2,3,4,5},{1,2,3,4,5,6}};

    for(int[]x:arr){

    for(int y : x){

    System.out.print(y+ " ");

    }

    System.out.println();

    }

    }

    }
    sir this is the program for
    class ArrayDemo{

    public static void main(String args[]){

    int arr[][]={{1},{1,2},{1,2,3},{1,2,3,4},{1,2,3,4,5},{1,2,3,4,5,6}};

    for(int[]x:arr){

    for(int y : x){

    System.out.print(y+ " ");

    }

    System.out.println();

    }

    }

    }

    1

    1 2

    1 2 3

    1 2 3 4

    1 2 3 4 5

    1 2 3 4 5 6

Leave A Comment

*