什么是标志变量? [英] What is a flag variable?

查看:1119
本文介绍了什么是标志变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我刚刚遇到这个术语,我不知道它的用途是什么?

Recently I just came across this term and I had no idea as to what purpose it serves?

我不确定何时使用标志变量以及如何使用要解决这个问题吗?

I am little unsure about when to use a flag variable and how to go about it?

我用Google搜索了它,但没有任何与之相关的具体示例(在JavaScript的上下文中)。

I Googled it but there were not any specific examples related to it (in the context of JavaScript).

推荐答案

标志变量定义和使用说:


标志变量,最简单的形式,是一个变量,你定义为一个值,直到某些条件为真,在这种情况下,您更改变量的值。它是一个变量,可用于控制函数或语句的流程,允许您在函数进行时检查某些条件。

A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value. It is a variable you can use to control the flow of a function or statement, allowing you to check for certain conditions while your function progresses.

作为一个例子:

// errors is the flag var
var errors = 0;

for(var i = 0; i < 10; i++)
  if(i == 6) // your error condition
    errors++;

if(errors)  // is the flag "up"? (i.e. > 0)
  alert("There was a problem!");

这篇关于什么是标志变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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