Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested loop construction

This is part of my homework. All I need is a bit of advice. I need to write some nested loop constructs, to print the following:

"122333444455555"

"+**+++****+++++"

"--***++++-----******+++++++"

Here is my code to print the first set of symbols

public static void main (String[] args)
{
    int i,j;
    for(i=1;i<6;++i)
    {
        for(j=1;j<i+1;++j)
        {
            System.out.print(i);
        }
    }
}

This works perfectly fine. I'm just having trouble figuring out the second and third set of symbols.
Apologies for my lack of experience, I'm fairly new to Java.

like image 662
Hustl3r28 Avatar asked Dec 04 '13 11:12

Hustl3r28


People also ask

What is nested loop with example?

If a loop exists inside the body of another loop, it's called a nested loop. Here's an example of the nested for loop. // outer loop for (int i = 1; i <= 5; ++i) { // codes // inner loop for(int j = 1; j <=2; ++j) { // codes } .. } Here, we are using a for loop inside another for loop.

What is the general structure of a nested loop?

Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as “loop inside loop“. Syntax for Nested Do-While loop: do{ do{ // statement of inside loop }while(condition); // statement of outer loop }while(condition);

How do you create a nested loop?

A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes.

What is a nested loop in coding?

A nested loop is a loop inside another loop. Although all kinds of loops can be nested, the most common nested loop involves for loops. These loops are particularly useful when displaying multidimensional data. When using these loops, the first iteration of the first loop will initialize, followed by the second loop.


Video Answer


2 Answers

One solution is:

final String[] arr = {"*", "+"};

And in your inner loop:

System.out.print(arr[i % 2]);

The % (Modulo) operator is responsible of the switches between * and + symbols:

For even i it'll be *, otherwise it'll be +.


Output: "+**+++****+++++".

(Regarding the second output, I'll not show you the solution, but it's very similar to this one once you understand it).

like image 81
Maroun Avatar answered Sep 20 '22 19:09

Maroun


public static void main(String[] args) throws IOException {

    int i, j;

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

    System.out.println();

    for (i = 1; i < 6; i++) {
        if (i % 2 == 1) {
            for (j = 1; j < i + 1; ++j){
            System.out.print("+");
            }
        } else {
            for (j = 1; j < i + 1; ++j){
                System.out.print("*");
            }
        }
    }

    System.out.println();

    for (i = 2; i < 8; i++) {
        if (i % 3 == 1) {
            for (j = 1; j <= i; ++j){
                System.out.print("+");
            }
        } else if (i % 3 == 2) {
            for (j = 1; j <= i; ++j){
                System.out.print("-");
            }
        } else {
            for (j = 1; j <= i; ++j){
                System.out.print("*");
            }
        }
    }
}

Cycle #1: You have to print out numbers from one to five and each number N has to be printed out N times.

for (i = 1; i < 6; ++i) { // this would set `i` to numbers from 1-5

for (j = 1; j < i + 1; ++j) { // for each cycle (next number) it prints 
//it out N times where N is the cycle number. 1 is the first cycle,
//2 is the second and so on.

Cycle #2: Same problem but instead of printing out number of the cycle you have to print out + or * based on if the cycle number is odd or even.

To check if the number is even you can use:

int number = 1;
if(number % 2 == 0){ // is true if the number is even

This checks whats the remainder from the division of number by two.

Cycle #3: Same as #2 but you start from the second cycle, not from the first and you check for the remainder after division by 3.

like image 38
Dropout Avatar answered Sep 23 '22 19:09

Dropout