C++的入门——判断语句

Posted SHSID Electronics SL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++的入门——判断语句相关的知识,希望对你有一定的参考价值。

C++的入门(3)——判断语句

由此可见,奥特曼是存在的。


    在图片中,我建立了一个布尔型的变量,因为在此处我只需要“是”或者“否”两种答案。当然你也可以建立其他类型的变量来使得你的判断更加多样化。之后我用上了“if 语句”,也就是用来判断条件的语句。该函数的框架为if()。在if的小括号中写上你想要的执行条件,当满足时,就会触发并运行之后的内容。你想要到达某条件执行某段代码需要被大括号给包含起来,来告诉程序这是当前条件被触发时该运行的内容。如图:

C++的入门(3)——判断语句

    这里,我的代码不止一条,所以需要大括号包含起来。


如果执行的条件不只是一种,而且其内容也不相同,你可以用else if语句或者else来执行。else if 与if类似,区别在于他的优先级更低,且执行过与他对应的if之后,就不会再执行else if里的内容了。Else最大的区别在于它不需要写条件,因为它自身包含的条件就是:除上述所对应的所有if 和else if里的条件之外,也就是我们的否则。所以我们可以把判断语句理解为:如果满足这个条件该怎么样,或者再怎么样,否则又如何?

C++的入门(3)——判断语句

    如图,如果a的值是0的话,那就加一。后面再依次判断是否a满足条件。如果满足,那就跳过之后连接在一起的判断语句。还有就是if的优先级是相等的,所以只要满足某个if的条件,就一定会触发其内容。比如两个连在一起的(连在一起,而不是被另一个所包含),只要满足这两个的条件,就都会触发,或者只满足一个的话就只触发其中一个的内容。多个if的性质都是一样的。另外,if函数还能再包含if,所以你可以打开你的脑洞,去试试看有什么神奇的事情会发生。哦,什么?你还不知道如何写判断条件?这就告诉你!

    判断条件也是有自己的规则的,而且判断条件大多数需要用到符号。里面包含但不限于:

  1. == 与数学的等号相同,不是赋值的意思

  2. && 以及的意思,用来表示当多个条件成立才触发

  3. || 或者的意思, 优先级高于&&

  4. !当某条件为否的时候触发(常用与布尔类型,可以见图1)

举例:

C++的入门(3)——判断语句

    也就是说,这些符号用来处理以及判断这些变量,来检查是否执行后续函数所包括的内容。


    好了,这就是本期的所有内容了。记住,别看判断语句很简单,实际上很复杂,可以让你开发的东西多着呢!所以还不赶紧去研究研究?



Judgment statements


    Do you feel monotonous after reading previous issues of coding? Can't make the program do what it is supposed to do? Then we are going to learn in this issue:もしもボックス!

C++的入门(3)——判断语句

    Ahem, what we really need to learn is the judgment statement, and もしもボックス is very similar to the function, except that the conditions and what to achieve are up to you. For example: if there is no Ultraman, output 0.

C++的入门(3)——判断语句

In the picture, I created a Boolean variable because I only need a “yes” or “no” answer here. Of course, you can create other types of variables to make your judgments more diverse. Then I used the "if statement", which is the statement used to determine the condition. The framework of this function is if(). You write the condition you want to execute in the if parentheses, and when it's met, it triggers and runs. The code you want to execute when you reach a certain condition needs to be enclosed in big brackets to tell the program that this is what to run when the current condition is triggered, as shown in the figure:

C++的入门(3)——判断语句

If there is more than one condition to be executed and the content is different, you can use an “else if” statement or an “else” statement to execute it. "else if" is similar to "if", except that it has a lower priority and the content of the "else if" will not be executed after the execution of the corresponding if. The biggest difference is that it does not need to write conditions, because it contains its own conditions other than the conditions in the if and else ifs, which is our "else". So, we can interpret the judgment statement as: what if this condition is met, or what if it is met, or what if it is not?

C++的入门(3)——判断语句

As shown in the figure, if the value of a is 0, then add one. After that, we will determine if a satisfies the condition in turn. If it does, then skip the judgment statements that are joined together afterwards. There is also the fact that the priority of if is equal, so as long as the condition of a certain if is satisfied, it will definitely trigger its content. For example, if two are joined together, as long as the conditions of both are met, both will be triggered; or if only one is met, only the contents of one will be triggered. The properties of multiple "ifs" are the same. Also, if functions can contain if again. You can try to see what magic can happen under different circumstances. Oh, wait. You still don't know how to write a judgment condition? This will show you!

Judgment conditions also have their own rules, and most of them require the use of symbols. It contains, but is not limited to.

1. == is the same as the mathematical equal sign, not the meaning of the assignment

2. && (and) is used to indicate when more than one condition holds before triggering

3. || (or) means that the priority is higher than &&

4. ! Triggered when a condition is no (commonly used with Boolean types, see Figure 1)

For example:

C++的入门(3)——判断语句

That is, these symbols are used to process as well as judge these variables to check whether to execute what is included in the subsequent function.


Well, that's all for this issue. Remember, the judgment statement looks very simple, but is actually very complex, it can let you develop a lot of things! So go and study it!

C++的入门(3)——判断语句

Author: Henry Y 8-11
Translator & Designer: Audrey Z 8-12

以上是关于C++的入门——判断语句的主要内容,如果未能解决你的问题,请参考以下文章

C++判断语句

c语言入门教程–-5判断语句

C++怎么用switch语句判断输入的字符

C++——分支语句

C++怎么用switch语句判断输入的字符

小白学习C++ 教程三C++用户输入判断语句和switch