如果 if 语句的条件为真,可以停止执行吗?

Posted

技术标签:

【中文标题】如果 if 语句的条件为真,可以停止执行吗?【英文标题】:If the condition of an if statement is true, can you stop execution? 【发布时间】:2018-02-16 15:20:53 【问题描述】:

如果满足某个条件,我希望能够停止程序的执行。这是一个学校项目,所以我不能从 IF 语句中更改它。

    while (peopleInGroup[0] > i) 
        ageOfPeopleInGroup.push(prompt("How old are you? " + i));
        i++;
    
    //If any one of the people in the group is under 13, then all members are prompted the rules of the movie thearter. If statements were choosen because it's possible none of the people in the group are under 13.//
    if (ageOfPeopleInGroup[0]< 13) 
        alert("If someone is under the age of 13, they must be accompanied by someone 18 years of age or older.")

【问题讨论】:

可以使用break语句退出循环,使用return语句退出 只需像上面那样打破循环 那么 if 需要在 while 循环中...... 您必须将 if 条件放在 while 循环中,当满足条件时,只需从循环中中断/返回。 【参考方案1】:

你在这里做错了几件事。

首先,您的循环不会迭代,因为您总是指向第一个元素 (peopleInGroup[0])。您需要一个变量来迭代 (peopleInGroup[i])。但是 while 循环并不是最适合这种情况的,还有更好的数组迭代方法,例如 forforEach 等。

接下来你的 if 条件在循环之外,你可以把它放在里面,如果满足一个条件,你可以使用break 退出循环,例如:

while(i < peopleInGroup.length) 
  if (peopleInGroup[i].age > 13) 
    hasUnderagePeople = true;
    break;
  
  i++;

但是有一些数组方法可以告诉您某个项目是否满足某个条件,例如Array.prototype.some:

const hasSomeUnderagePeople = peopleInGroup.some(person => person.age <= 13);

在旧式 javascript 中相当于:

var hasSomeUnderagePeople = peopleInGroup.some(function(person) 
  return person.age <= 13;
);

然后你的提示:

if (hasSomeUnderagePeople) 
  alert('If someone is under the age of 13, they must be accompanied by someone 18 years of age or older.')

这是假设数组中的每个项目都是具有年龄属性的对象。目前尚不清楚您的物品实际上是由什么组成的。

【讨论】:

【参考方案2】:

我不知道你可以改变多少结构,但这可能是你想要的:

var numOfPeople=parseInt(prompt("How many persons are there?"));
var underaged=false;
var ages=[];
for(var i=0;i<numOfPeople;i++)
   var age=parseInt(prompt("How old are you?"));
   ages.push(age);
   if(age<13)
       underaged=true;


if(underaged)
    alert("Blablabla, 13, blablabla...");

这里的想法是要求所有年龄,如果其中任何一个年龄低于 13 岁,则会出现在变量 underaged 中,并导致仅在末尾显示警报,并且仅显示一次,整个小组。

如果您不想存储所有年龄,那么使用while+break 更有意义:

var numOfPeople=parseInt(prompt("How many persons are there?"));

while(numOfPeople>0)
    if(parseInt(prompt("How old are you?"))<13)
        alert("Blablabla, 13, blablabla...");
        break;
    
    numOfPeople--;

此变体开始询问年龄,如果有未成年人,则显示警报并停止询问。如果所有年龄都很好,它只是结束而不显示任何内容。

【讨论】:

【参考方案3】:

while (peopleInGroup[0] > i) 
  var age = prompt("How old are you? " + i);
  if(age < 13)
    //If any one of the people in the group is under 13, then all members are prompted the rules of the movie thearter. If statements were choosen because it's possible none of the people in the group are under 13.//
    alert("If someone is under the age of 13, they must be accompanied by someone 18 years of age or older.")
    break;
   else 
    ageOfPeopleInGroup.push(age);
  
  i++;

【讨论】:

以上是关于如果 if 语句的条件为真,可以停止执行吗?的主要内容,如果未能解决你的问题,请参考以下文章

第六篇:循环语句 - while和for

if() if() else else if() else怎么运用,请高手详细的解说一下,一定采纳!

python基础:循环语句

条件检查时如何一次停止执行if语句?

while循环和格式化输出

while语句(内有实操)