Print using for-loop

python for loop
python for loop range
for loop example
python for loop index
for loop python 3
python for loop list
for loop java
while loop

Trying to import the shape (Pattern 23) from Java (source https://www.topjavatutorial.com/java/java-programs/25-java-programs-printing-number-character-patterns/ and print it in console:

I only manged to print the left shape using:

for (let i = 1; i <= 8; i++) {
    let shape = '';

    for (var j = 1; j <= i; j++) {
        shape += '*';
    }
    console.log(shape);
}

Any ideas on how I should try to solve this will be appreciated.

Here is one way to do it, building off the code you wrote:

function reverseString(str) {
    return str.split( '' ).reverse( ).join( '' );
}

// Build first 8 characters of each line.
lines = []
for (let i = 1; i <= 8; i++) {
    lines.push('')

    for (var j = 1; j <= i; j++) {
        lines[lines.length-1] += "*";
    }
    for (; j <= 8; j++) {
        lines[lines.length-1] += " ";
    }
}


// Append the vertical mirror to get the first 16 characters of each line.
lines_2 = []
for (let i = 1; i <= 8; i++) {
    lines_2.push(lines[i - 1] + lines[8-i]);
}

// Append the horizontal mirror to get the entire 32 characters of each line.
lines_3 = []
for (let i = 1; i <= 8; i++) {
    lines_3.push(lines_2[i-1] + reverseString(lines_2[i-1]));
}

// Output
for(let i = 0; i < 8; i++) {
    console.log(lines_3[i]);
}

Result:

*       ****************       *
**      *******  *******      **
***     ******    ******     ***
****    *****      *****    ****
*****   ****        ****   *****
******  ***          ***  ******
******* **            ** *******
*********              *********

This isn't necessarily the most concise way to do it. But I hope you can see how we can arrive here by building, one step at a time, on the partial solution you started with.

Java for Loop (With Examples), You need to use loops. With loops, we can simply write the print statement one time and run it for any number of times. Printing of patterns using loops is a common question asked to a beginner and is a good practice to make strong grip over loops. This post contains the codes of various patterns. Read the comments in the code to understand the logic.

One option is:

for (var i = 0; i < 8; i++) {
  let stars = "*".repeat(9 - i).padEnd(9, ' ');
  let leftSide = "*".repeat(i + 1).padEnd(8, ' ') + stars;
  let rightSide = leftSide.split("").reverse().join("");
  console.log(leftSide + rightSide);
}

Result:

*       ******************       *
**      ********  ********      **
***     *******    *******     ***
****    ******      ******    ****
*****   *****        *****   *****
******  ****          ****  ******
******* ***            *** *******
**********              **********

Note that the result is slightly different from your original shape. The middle section of this shape has 18 lines but the middle section of your shape has 17 lines.

Python for Loop, For example: For loop from 0 to 2, therefore running 3 times. for x in range(0, 3): print("We're on time %d" % (x)). While loop from 1 to infinity,� Python For Loops. A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).. This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

Finally, a pure for-loop of * and spaces answer:

for (let i = 1; i <= 8; i++) {
    let shape = '';

    for (var j = 1; j <= i; j++) {
        shape += '*';
    }
    for (var j = 8; j > i; j--) {
        shape += ' ';
    }
    for (var j = 9; j >= i; j--) {
        shape += '*';
    }
    for (var j = 2; j <= i; j++) {
        shape += ' ';
    }
    for (var j = 2; j <= i; j++) {
        shape += ' ';
    }
    for (var j = 9; j >= i; j--) {
        shape += '*';
    }
    for (var j = 8; j > i; j--) {
        shape += ' ';
    }
    for (var j = 1; j <= i; j++) {
        shape += '*';
    }
    console.log(shape);
}

ForLoop, If you have to use a loop, do so in a list comprehension: print('The lists are:', '\n'. join([str(lst) for lst in L])). This'll omit the newline after 'The lists� for i in names: print(i + 'other_str') # i is a str In order to randomly access elements on a list , you need to specify their index , which needs to be an int . If you want to get the correspondent indices of the elements, while you are iterating over them, you can use Python's enumerate :

Python: for loop inside print(), Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In this tutorial we will learn how to use “for loop” in Java. of a number using loops � Java Program to print Fibonacci Series using for loop . The example also shows various ways to print the ArrayList using a loop, Arrays class, and Java 8 Stream. How to print ArrayList in Java? There are several ways using which you can print ArrayList in Java as given below. 1) Using for loop. You can print ArrayList using for loop in Java just like an array.

For loop in Java with example, The following code prints the numbers from 1 to 100 in increments of 1. for ( int x = 1; x <= 100 ; x++ ) { printf("%d\n",x); }. Usually this is accomplished by using the "length" function on an array. Print the odd numbers from 1 to 1001. Search a list (array) of numbers for the biggest grade. Again, the computer "knows" how many grades there are, so a for loop is appropriate. Design Pattern Quick Reference:

C for loop, Instead of printing just the same "hello world" , over and over, we've managed to tell the computer to print something different with each iteration. Note that we� Here I submitted code snippets with description that prints stars in different shapes. By just using for loops in different ways we can obtain the desired result. Objective. To improve the logical thinking using For loops. In this part I am using only two for loops to print the different shapes. Using the code

Comments
  • here's a hint, include ALL from top to bottom
  • @JaromandaX he's talking about the 1st column-like shape
  • Pretty solution.
  • Thanks Edper. This was an interesting challenge.