This article describes how to use a set of condition check statements to create programming alternatives. C++ programming uses the { and } symbols for each block of action that means the instruction set in each block is always executed from top to bottom, with one action at a time, and can be executed by creating a condition. In addition, the desired part of the command can also be repeated by loop, which will be discussed in the next article.
If this is the case, do this.
Condition Type 1 is a condition check if the thing you want to check is true, do something, for example, if you are full, show your satisfaction can be written as follows
full=true
if(full==true)
printf(“full”);
In this case the C++ format is
if ( condition )
do something if true
or if there are multiple statements that must be executed when the condition is true
if ( condition ) {
do something if true
}
bool bFull = true;
void setup() {
Serial.begin(115200);
if (bFull)
Serial.println("...So full...");
}
void loop() {
}
Conditions in which if true, I will do one thing, if not, I will do another. For example, if I am full I will cry out. But if I’m not full I will continue to eat. can be written as follows
full=true
if (full==true)
printf(“I will cry out”);
else
printf("I continue to eat");
In the form of C++ is
if ( condition )
do something if true
else
do something if false
bool bFull = true;
void setup() {
Serial.begin(115200);
if (bFull)
Serial.println("I will cry out");
else
Serial.println("I continue to eat");
}
void loop() {
}
If there is more than one action statement, { and } define a block of action.
If true, do this. Not true but like this, do that
If the following conditions are met, if the score is greater than 80, it means very good; if it is more than 60, it means that it is good; if it is more than 30, it means that it is OK; if it is not, it means that you should stop; how to write it?
Let’s look at the first example.
score=80
if(score>80)
printf(“very good”);
if(score>60)
printf(“good”)
if(score>30)
printf(“OK”);
else
printf(“You should stop”);
Try converting to C++
int score = 80;
void setup() {
Serial.begin(115200);
Serial.println("\n\n");
if (score > 80)
Serial.println("..very good..");
if (score > 60)
Serial.println("..good..");
if (score > 30)
Serial.println("...OK...");
else
Serial.println("You should stop");
}
void loop() {
}
ผลลัพธ์เป็นดังนี้
From the results, it can be found that 2 cases are shown, i.e. greater than 60 and greater than 30, but the real requirement is to show the case of OK. Therefore, the code must be changed as follows.
if (condition1)
do something if condition1 is true.
else if (condition2)
do something if condition 2 is true and condition1 was false.
else if (condition3)
do something if condition3 is true and condition2 was false.
…
else if (conditionn)
do something if conditionn is true and previous condition was false.
The case is done when none of the conditions is true, use the following format.
if (condition1)
thing to do if condition1 is true
else if (condition2)
thing to do if condition 2 is true and condition1 was false.
else if (condition3)
thing to do if condition3 is true and condition2 was false.
…
else if (conditionn)
thing to do if conditionn is true and previous condition was false.
else
thing to do if all of previous conditions were false.
So the code should be written as follows:
int score = 80;
void setup() {
Serial.begin(115200);
Serial.println("\n\n");
if (score > 80)
Serial.println("..very good..");
else if (score > 60)
Serial.println("..good..");
else if (score > 30)
Serial.println("...OK...");
else
Serial.println("You should stop");
}
void loop() {
}
There is also a nested if inside an if, also called a nest-if, which is omitted. When there is an example, we will explain again because the formats are not different only if it’s true, there is a nested condition check.
Conclusion
From the form of conditional programming with an if statement, it can be written in abbreviated form as
(condition)?(case of true):(case of false)
There is also another form of the if statement (switch) but can be written with if instead, so we skip this pattern for now. Finally, have fun with programming.
(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-09-29