如果还有其他-只需绝对确定... [英] if and else -just making absolutely sure...

查看:40
本文介绍了如果还有其他-只需绝对确定...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专家,但是老实说我以为我理解ifelse好...但是我现在不确定:〜

因此,请务必确保是否有

I''m no expert, but I honestly thought I understood if and else OK... but I''m not sure now :~

So, just to make absolutely sure, if you have

int a = 0;
int b = 1;
CString strOutput;
if(a == 0)
{
	strOutput = "I am a boy.";
}
else if(b == 1)
{
	strOutput = "I am a girl.";
}



strOutput说什么?

我以为是我是男孩",但是当我说我是男孩"时,我正在通过一些代码进行调试.然后是我是女孩".即执行了两个分支.

诚然,我到处都是乱七八糟的内存访问,所以可能是这样,但是请有人告诉我,这段代码应该执行所有路径,还是只执行一个?我知道括号的数量了吗? (当然,编译器应该已经发现了,但是嘿...)

顺便说一句,部分困惑是我最喜欢的工具Matlab区分了if-elseif-elseif.



what does strOutput say?

I thought it''d be "I am a boy.", but I was debugging through some code when it said "I am a boy." then "I am a girl.", i.e. executed both branches.

Admittedly, I was making a mess of memory access all over the place, so it could have been that, but please, some one tell me, should this piece of code execute all paths, or just one? And have I got the number of brackets right? (The compiler should have spotted that, of course, but hey...)

As an aside, part of the confusion is that my preferred tool that is Matlab distinguishes between if-else and if-elseif.

推荐答案

您的理解是正确的,并且strOutput必须为我是男孩".

-Saurabh
Your understanding is correct and strOutput must be "I am a boy.".

-Saurabh


您的输出必须为"I am a boy."


您的if-else理解正确,并且括号.
Your if - else understanding is correct and also the number of {} brackets.
PaulowniaK写道:
PaulowniaK wrote:

if(a == 0)
{
   strOutput = "I am a boy.";
}
else if(b == 1)
{
   strOutput = "I am a girl.";
}


第二对{}括号属于if,您也可以这样写:


The 2nd pair of {} brackets belong to the if, you could also write:

if(a == 0)
{
   strOutput = "I am a boy.";
}
else 
{
   if(b == 1)
   {
      strOutput = "I am a girl.";
   }
}


但是结果将是相同的.


But the result will be the same.


这篇关于如果还有其他-只需绝对确定...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆