Let us learn C in Code <8> decision making

本文介绍了C语言中的决策结构,包括if语句、if-else语句及if-elseif-else语句等,并通过实例展示了如何根据条件判断结果来执行不同的代码块。

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!

你遇到的情况是由于你使用的 `changeObjectToXml` 函数在处理数组时,会递归地遍历数组中的每个元素。但问题在于:**它将字符串当作数组来处理了**。 --- ### 你的输入对象是: ```typescript const obj = { bmidConditionSet: ['1000', '19688CD852603000'] }; ``` 其中 `bmidConditionSet` 是一个数组,数组中的每个元素是一个字符串(如 `'1000'`)。 --- ### 原函数的处理逻辑: ```ts for (var subObjKey in obj[key]) { resultString = resultString + Object2Xml.changeObjectToXml(obj[key][subObjKey]); } ``` 这里用了 `for...in` 遍历数组,这在 JavaScript/TypeScript 中是不推荐的,因为 `for...in` 实际上是遍历对象的键(包括数组索引,但也会遍历其他可枚举属性)。 **更严重的问题是:当处理字符串时,`for...in` 会把字符串当作类数组对象处理,遍历的是字符索引。** 例如: ```js for (let i in "1000") { console.log(i, "1000"[i]); } ``` 输出: ``` 0 "1" 1 "0" 2 "0" 3 "0" ``` 所以函数会把字符串 `'1000'` 拆分成一个个字符,然后递归调用 `changeObjectToXml`,将每个字符作为独立节点输出。 --- ### 所以你看到的输出是这样的: ```xml <bmidConditionSet> <0>1</0> <1>0</1> <2>0</2> <3>0</3> <0>1</0> <1>9</1> <2>6</2> ... </bmidConditionSet> ``` 这是函数错误地把字符串当作数组处理,将每个字符作为数组元素来生成 XML。 --- ### 正确的输出应该是: ```xml <bmidConditionSet> <item>1000</item> <item>19688CD852603000</item> </bmidConditionSet> ``` --- ### ✅ 正确的修复方法 你需要在函数中区分 **数组** 和 **字符串**,确保字符串不会被当作数组处理。 --- ### ✅ 修复后的代码如下: ```ts static changeObjectToXml(obj: any): string { let resultString = ''; if (Array.isArray(obj)) { // 如果是数组,则统一用 <item> 标签包裹每个元素 for (let i = 0; i < obj.length; i++) { resultString += `<item>${obj[i]}</item>`; } } else if (typeof obj === 'object' && obj !== null) { // 如果是对象 for (let key in obj) { if (obj.hasOwnProperty(key)) { const value = obj[key]; resultString += `<${key}>`; if (Array.isArray(value)) { for (let i = 0; i < value.length; i++) { resultString += `<item>${value[i]}</item>`; } } else if (typeof value === 'object' && value !== null) { resultString += Object2Xml.changeObjectToXml(value); } else { resultString += value != null ? String(value) : ''; } resultString += `</${key}>`; } } } else { // 基本类型直接输出 resultString += obj; } return resultString; } ``` --- ### ✅ 修复后的输出 输入: ```ts const obj = { bmidConditionSet: ['1000', '19688CD852603000'] }; ``` 输出: ```xml <bmidConditionSet> <item>1000</item> <item>19688CD852603000</item> </bmidConditionSet> ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值