Hi, everyone. Today is April 1. who maybe the April one at this funny day. This chapter we'll learn the decision making structures. What 's the decision making? As it means , we must making decision . In our daily life, we make decision every time when we meet the choices? Suppose this situation, this weekend we will have a tour if we have a good weather that must be sunshine and gentle breeze. Having a tour is the result and the weather is condition. The result depends on the condition heavily.
In code, there are many conditions to be tested or evaluated by the C program. When the condition is true, some statements must be executed, if not, other statements can be executed, but it depends. We have learnt the relational operators likeequal"==" operator or not equal "!=" at previous chapter "Let us learn C in Code <7>" .Here we use both of them in our program lately.
C program provides several making decision statement as below table.
Statement | Description |
if(boolean)... | if the boolean expression is true and the alongside statements are executed. |
if(boolean) ...else.. | if the boolean expression is true and the alongside statements are excecuted. if the boolean expression is false, the else statements are executed.Here only one group of statements if or else is executed |
if(boolean) ... elseif(boolean) ...else... |
this is the nested if statement, here can be more "elseif" .sure two or more condition need to be check. |
The basic structure is if(boolean){ ... } or if(boolean){ ...}else{....} or if(boolean){...}elseif(boolean){...}elseif(boolean)....else{...}*..And the red color part is not essential.
So let's solve this problem. declare two integer variables making addictive if the result is the true one output "TRUE" hint, if not please output the "FALSE" hint.
Ok, let's code it
main()
{
int addone = 10;
int addtwo = 20;
int addthree = 0;
addthree = addone + addtwo;
if(addthree == 30)
{
printf("TRUE\n");
}
else
{
printf("FALSE\n");
}
}
In this code, if addthree equals 30 ,the expression is true and the alongside statements between the braces"{}" are executed. if addthree is not 30, the expression is false and the else part is executed , so outputs "FALSE" hint.
As here, we have learnt data types, constants,variables, operators and decision making. If you have understood these basic rules, we can write our program well.But there are more important part we have not learnt is how to design a program correctly. Before writing a program, we should analyze the a problem and make a clear the conditions and the result . Surely some time we must use algorithm to make your program more effective. All the things are need to be concerned not just program_self.
Next chapter we will introduce two method to solve a problem one called pseudocode and the other is flowchart. It's really the good habit to write the pseudocode or draw a flowchart before writing a program.
Ok ! See you next chapter, have a nice day!