Anthony J
Anthony J

Reputation: 581

For loop within a for loop

So I'm working on for loops and nested for loops. So I need to stick to these as my main functions.

I've gotten stuck on one question. I can think of the logic of how I'd solve it...but I can't figure how I'd do it with for loops/nested for loops.

I've got to print Ascii codes in rows of 10

Example:

XXXXXXXXX

XXXXXXXXX

XXXXXXXXX

(from 32-122)

Here's my code so far:

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

        for( int j = 32; j < 122; j++){

            System.out.print((char)j);

//Once above loop is performed 10*...execute a new line..using a for loop..??               
                System.out.println();
                    }
                }
            }    

Upvotes: 0

Views: 153

Answers (6)

DaoWen
DaoWen

Reputation: 33019

The syntax for a for loop is:

for ( initialization ; termination condition ; increment ) body

However, each of those items in italics above is optional. The initialization and increment parts can have multiple components, separated by a comma.

Since you know the total number of characters is a multiple of 10, here's what I would do:

public static void main(String[]args){
    for( int j = 32; j < 122; /* don't increment j here */){
        // print 10 chars per line
        for (int col=0; col<10; col++, j++ /* increment j here instead */) {
            System.out.print((char)j);
        }
        System.out.println();
    }
}

Note that if the total number of characters wasn't a multiple of 10, then the inner loop might print some extras, since the outer loop's condition is only checked once every 10 characters.

Upvotes: 2

Greg Pelcha
Greg Pelcha

Reputation: 66

A straight forward approach could be to use an if statement as QuakeCore suggested. the code would come to look something like the following:

public static void main(String[] args) {
    for (int j = 32; j < 122; j++) {
        System.out.print((char)j);
        if (j % 10 == 1) {
            System.out.println();
        }
    }
}

This leaves for some ugly code when working with the Modulus function in the if condition. The reason for it is that we are starting with the number 32 and incrementing from their, thus we get j % 10 == 1 instead of something nicer such as j % 10 == 0.

But, your question states you wish to solve it with For loop inside a for loop, making me think it's a school task. This can be solved by looking at for loops as functions to be performed within the 2D space. Such that the first for loop is handling the rows, while the inner for loop is handling columns (or y and x space respectively). From this we can solve the problem as follows:

public static void main(String[] args) {
    // Increment row/counter by 10
    for (int row = 32; row < 122; row += 10) {
        for (int col = 0; col < 10; col++) {
            System.out.print((char)(row + col));
        }
        System.out.println();
    }
}

Upvotes: 2

user479288
user479288

Reputation:

If you must use nested loops, then maybe something like this would work:

int lineLength = 10;
int lineCount = (122 - 32) / lineLength;
for (int line = 0; line < lineCount; line++) {
    for (int column = 0; column < lineLength; column++) {
        int index = (line * lineLength) + column;
        System.out.print((char)(32 + index) + " ");
    }
    System.out.println();
}

Upvotes: 0

ValarDohaeris
ValarDohaeris

Reputation: 649

do it with for loops/nested for loops

Try this-

public static void main(String[] args) {

        for (int j = 32; j < 122; j++) {

            System.out.print((char) j);
            if((j-32)%10==0){
                System.out.println();
            }               
        }
    }

Here the inner condition will take care of changing line when you have printed 10 values

Upvotes: 0

chenchuk
chenchuk

Reputation: 5742

This will print each char 10 times in a row ( with nested for loops ):

public static void main(String[] args) {
        for (int j = 32; j < 122; j++) {

            // print 10 times the same char in the same line
            for (int i=0;i<=10;i++){
                System.out.print((char) j);
            }
            // after 10 char : goto next line
            System.out.println();
        }
    }

Upvotes: 0

D. Ben Knoble
D. Ben Knoble

Reputation: 4673

You're outer loop should control what row you're on, and the inner loop what column. Thus, just the outer loop looks like this (there are 9 rows):

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

This will print 9 newlines, giving you the 9 rows.

Now, your column logic goes inside, but before the println.

for (int i = 0; i < 9; i++)
{
    for (int j = 0; j < 10; j++)
    {
        char print = (char)((i * 10) + 32 + j);
        System.out.print(print);
    }
    System.out.println("");
}

This utilizes a small math trick to generate the numbers of the sequence. Row 1 = 32 to 41, 2 = 42 to 51, etc.

Note also that this is slightly more verbose than other possible answers because I used nested loops like you asked.

Upvotes: 4

Related Questions