Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 帮忙解释两个for循环

帮忙解释两个for循环

Jun 23, 2016 pm 01:50 PM
cycle explain

各位大大,这是一个用*输出倒直角三角形的例子,这两个for循环我不是很白,特别是里面的那个,能解释下么,谢谢啦~

    for($i=1;$i<15;$i++){        for($j=$i;$j<15;$j++){            echo "*";        }       echo "<br>";    }
Copy after login


回复讨论(解决方案)

为什么还要有个变量$j去等于外层的变量$i,再次循环一遍呢?

你首先理解下单个发for循环含义!如果里面不再做一次循环就不会出现3角了,而是竖着的14个*

$j = $i;//是让内层循环的起始值等于外层的值//每次都少输出一个*//外层第一次循环,内层输出14个*//外层第二次循环,内层输出13个*//以此类推只到循环结束
Copy after login

$j = $i;
关于这两个变量,我这样说看你能不能理解。
$i代表的是行数,即第一次循环代表第一行,依次类推,一共十五行。
$j代表的是列数,即每一行有多少列。

把代码稍改动下,输出一个矩形,帮助你理解。改动的地儿:$j=1。

<?php for($i=1;$i<15;$i++){        for($j=1;$j<15;$j++){             echo "*";        }       echo "<br>";    }?> 
Copy after login

理解for循环
其实可以输出四种直角三角形

$i 可以看做是行, $j可以看做是列
首先看 $i =1;的时候$j =1; j<15,也就是里面的for会循环14次,第一行就会出现14个*,然后里面的for结束开始 $i=2;的循环
然后看 $i =2;的时候$j=2;j<15,里面的for会循环13次,出现13个*,后,开始执行$i=3;
…………
一次循环,最后得到第一行14个*,共14行,最后一行一个*,不知是否清楚.

多练练九九乘法表的输出和菱形的输出

这个我都能理解唉,i=1的时候 第二个for循环1<15 是不是循环14个星 i=2的时候 循环13 以此类推

$i控制执行的行数。
$j控制每一行输出的*数。
很好理解啊。

因为从第一行开始,你的星号个数就要递减,知道了规律,你再结合代码就容易理解了。其实代码还可以这样写。

for($i=1;$i<15;$i++){        for($j=0;$j<15-$i;$j++){             echo "*";        }       echo "<br>";    }
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the analysis of 2.8k screen? What is the analysis of 2.8k screen? Jan 02, 2024 pm 12:21 PM

We often see the introduction of how many K screens we have when buying TVs, computers or mobile phones, such as 2.8K screens. At this time, there will be friends who don’t know much about electronic devices and will be curious about what this 2.8K screen means and what the resolution is. What does 2.8k screen mean? Answer: 2.8k screen means that the screen resolution is 2880*18002K, which means the number of horizontal pixels is greater than 2000. For the same size screen, the higher the resolution, the better the picture quality. Introduction to resolution 1. Since the points, lines and surfaces on the screen are all composed of pixels, the more pixels the monitor can display, the finer the picture, and the more information can be displayed in the same screen area. 2. The higher the resolution, the greater the number of pixels, and the sharper the sensed image.

Lambda expression breaks out of loop Lambda expression breaks out of loop Feb 20, 2024 am 08:47 AM

Lambda expression breaks out of the loop, specific code examples are needed. In programming, the loop structure is an important syntax that is often used. However, in certain circumstances, we may want to break out of the entire loop when a certain condition is met within the loop body, rather than just terminating the current loop iteration. At this time, the characteristics of lambda expressions can help us achieve the goal of jumping out of the loop. Lambda expression is a way to declare an anonymous function, which can define simple function logic internally. It is different from an ordinary function declaration,

A comparative study of loops and recursion in Go language A comparative study of loops and recursion in Go language Jun 01, 2023 am 09:23 AM

Note: This article compares loops and recursion from the perspective of Go language. When writing programs, you often encounter situations where a series of data or operations need to be processed repeatedly. To achieve this we need to use loops or recursion. Loops and recursions are both commonly used processing methods, but in practical applications, they each have advantages and disadvantages, so the actual situation needs to be considered when choosing which method to use. This article will conduct a comparative study of loops and recursion in the Go language. 1. Loops A loop is a mechanism that repeatedly executes a certain piece of code. There are three main types of Go language

PHP returns all the values ​​in the array to form an array PHP returns all the values ​​in the array to form an array Mar 21, 2024 am 09:06 AM

This article will explain in detail how PHP returns all the values ​​of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values ​​in an array. It does not preserve the keys of the original array. $array=[&quot;foo&quot;=&gt;&quot;bar&quot;,&quot;baz&quot;=&gt;&quot;qux&quot;];$values=array_values($array);//$values ​​will be [&quot;bar&quot;,&quot;qux&quot;]Using a loop can Use a loop to manually get all the values ​​of the array and add them to a new

Java Iterator vs. Iterable: A step into writing elegant code Java Iterator vs. Iterable: A step into writing elegant code Feb 19, 2024 pm 02:54 PM

Iterator interface The Iterator interface is an interface used to traverse collections. It provides several methods, including hasNext(), next() and remove(). The hasNext() method returns a Boolean value indicating whether there is a next element in the collection. The next() method returns the next element in the collection and removes it from the collection. The remove() method removes the current element from the collection. The following code example demonstrates how to use the Iterator interface to iterate over a collection: Listnames=Arrays.asList("John","Mary","Bob");Iterator

What are the alternatives to recursive calls in Java functions? What are the alternatives to recursive calls in Java functions? May 05, 2024 am 10:42 AM

Replacement of recursive calls in Java functions with iteration In Java, recursion is a powerful tool used to solve various problems. However, in some cases, using iteration may be a better option because it is more efficient and less prone to stack overflows. Here are the advantages of iteration: More efficient since it does not require the creation of a new stack frame for each recursive call. Stack overflows are less likely to occur because stack space usage is limited. Iterative methods as an alternative to recursive calls: There are several methods in Java to convert recursive functions into iterative functions. 1. Use the stack Using the stack is the easiest way to convert a recursive function into an iterative function. The stack is a last-in-first-out (LIFO) data structure, similar to a function call stack. publicintfa

Using vectorization to replace loops in python Using vectorization to replace loops in python Apr 14, 2023 pm 07:07 PM

All programming languages ​​are inseparable from loops. So, by default, we start executing a loop whenever there is a repeating operation. But when we are dealing with large number of iterations (millions/billions of rows), using loops is a crime. You might be stuck for a few hours, only to realize later that it doesn't work. This is where implementing vectorization in python becomes very critical. What is vectorization? Vectorization is a technique for implementing (NumPy) array operations on data sets. Behind the scenes, it applies the operation to all elements of the array or series at once (unlike a "for" loop that operates one row at a time). Next we use some use cases to demonstrate what vectorization is. Find the sum of numbers##Use the loop importtimestart

How to handle PHP loop nesting errors and generate corresponding error messages How to handle PHP loop nesting errors and generate corresponding error messages Aug 07, 2023 pm 01:33 PM

How to handle PHP loop nesting errors and generate corresponding error messages. During development, we often use loop statements to handle repeated tasks, such as traversing arrays and processing database query results. However, when using loop nesting, you sometimes encounter errors, such as infinite loops or too many nesting levels. This problem can cause server performance to degrade or even crash. In order to better handle such errors and generate corresponding error messages, this article will introduce some common processing methods and give corresponding code examples. 1. Use counters to

See all articles