有人可以解释什么是“为"吗?循环是最简单的说法吗? [英] Can someone explain what a "for" loop is in the simplest terms possible?

查看:52
本文介绍了有人可以解释什么是“为"吗?循环是最简单的说法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习编码,但是我无法解决"for"循环中发生的事情.

I'm trying to learn coding but I can't wrap my head around what happens in a "for" loop.

if/else if/else语句很容易.

if/else if/else statements are easy.

if (this is true) {
do this.

else [otherwise, but] if (this is true) {
do that.

else
do this.

"while"和"do/while"循环也很容易.

"while" and "do/while" loops are easy too.

while (this is is true) {
do this.

do this {
}

while (this is true).

现在,我知道"for"循环的结构:(开始,结束,递增).我什至可以制作出适用于基本问题的软件.但是,我不知道计算机在想什么.我不能像其他人一样用基本的英语术语来表达它.任何帮助将不胜感激.

Now, I know the structure of a "for" loop: (start, finish, increment). I can even make one that works for basic problems. However, I don't know what the computer is "thinking." I can't put it into basic English terms like I can the others. Any help would be appreciated.

推荐答案

基础

For循环是数字上下文中的简写while循环.

Basics

For loops are short-hand while loops in numerical contexts.

而不是这样写:

x = 0;
while (x < 5) {
  x++;
}

您可以这样写:

for (x = 0; x < 5; x++)

语法

哦,部分如下:

for (start; condition; increment)

它们的含义如下:

  1. 开始或初始化是初始变量.
  2. 条件类似于while语句中括号中的部分;for的主体将继续执行,直到变为假.
  3. 增量或 update 是更改变量的方式;没有它,循环将无限期地运行.
  1. The start or initialization is the initial variable.
  2. The condition is works like the part in parenthesis in a while statement; the for's body will continue to execute until this becomes false.
  3. The increment or update is how the variable is changed; without it, the loop runs indefinitely.

这篇关于有人可以解释什么是“为"吗?循环是最简单的说法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆