Let us learn C in Code <8> decision making

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.

StatementDescription
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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值