using System;
class MainClass
{
static void Main()
{
int i;
bool someCondition = false; // bool定义 someCondition 为false假 1 i = 0; // i等于1 if (someCondition & (++i < 100)) //如果someCondition(为false假 1)与 1小于100
Console.WriteLine("这将不会显示."); // 这个为什么不显示?
Console.WriteLine("if statement executed: " + i); // 结果 if statement executed: 1 这是为什么? if (someCondition && (++i < 100)) //如果someCondition(为false假 1)与 1小于100 这里是可选择 && 所以运算(++i < 100) 1小于100
Console.WriteLine("这将不会显示."); // 这个为什么不显示?
Console.WriteLine("if statement executed: " + i);// 结果 if statement executed: 1 这是为什么?
}
}
class MainClass
{
static void Main()
{
int i;
bool someCondition = false; // bool定义 someCondition 为false假 1 i = 0; // i等于1 if (someCondition & (++i < 100)) //如果someCondition(为false假 1)与 1小于100
Console.WriteLine("这将不会显示."); // 这个为什么不显示?
Console.WriteLine("if statement executed: " + i); // 结果 if statement executed: 1 这是为什么? if (someCondition && (++i < 100)) //如果someCondition(为false假 1)与 1小于100 这里是可选择 && 所以运算(++i < 100) 1小于100
Console.WriteLine("这将不会显示."); // 这个为什么不显示?
Console.WriteLine("if statement executed: " + i);// 结果 if statement executed: 1 这是为什么?
}
}
class MainClass
{
static void Main()
{
int i;
bool someCondition = false; // bool定义 someCondition 为false假 1 i = 0; // i等于0 if (someCondition & (++i < 100)) //如果someCondition(为false假 1)与++i应该是1 那就是 1小于100
Console.WriteLine("这将不会显示."); // 这个为什么不显示?
Console.WriteLine("if statement executed: " + i); // 结果 if statement executed: 1 这是为什么? if (someCondition && (++i < 100)) //如果someCondition(为false假 1)与 1小于100 这里是可选择 && 所以运算(++i < 100) 1小于100
Console.WriteLine("这将不会显示."); // 这个为什么不显示?
Console.WriteLine("if statement executed: " + i);// 结果 if statement executed: 1 这是为什么?
}
}
{执行code};你那个++i<100为真, someCondition是false。所以运用&&组合得到一个“条件为假”的表达式,所以不会执行你的语句。楼主你逻辑搞混了吧。这个逻辑才是对地,如下:
if (someCondition==false && (++i<100))
&&逻辑运算符则进行了优化,第一个逻辑表达式为false的时候,将直接判定为false,而不会计算第二个逻辑表达式。所以如果用&的话结果就是1,而如果用&&的话结果就是0
using System;
class MainClass
{
static void Main()
{
int i;
bool someCondition = false; // bool定义 someCondition 为false假 1 i = 0; // i等于0 if ((++i < 100) & someCondition) //当且仅当两个操作数均为 true 时,结果才为 true 因为++i 是1 就是真 someCondition 又是假所以显示假
Console.WriteLine("这将不会显示."); // 但是这个为什么不显示?
Console.WriteLine("if statement executed: " + i); // 结果 if statement executed: 1 表示显示了(++i < 100) 上面不是说了显示假 这是为什么? if ((++i < 100) && someCondition) //我把和上面的代码 顺序调换了下 先是真 后是假 那是不是就表示这里是真?
Console.WriteLine("这将不会显示."); // 非常搞不明白为什么这行就是不显示?
Console.WriteLine("if statement executed: " + i);// 结果 if statement executed: 2 但是为什么这里显示了2 这是为什么?
}
}
if ((++i < 100) && someCondition)
Console.WriteLine("这将不会显示.");
Console.WriteLine("if statement executed: " + i); 进一步的去理解,是不是
(++i < 100) && someCondition
上面红色的显示第一行
蓝色显示第一行
但是好象也不正确
搞不明白?
Console.WriteLine("这将不会显示."); // if判断为false 跳过
Console.WriteLine("if statement executed: " + i); if ((++i < 100) && someCondition) //i=1 someCondition=false 执行++i i=2
Console.WriteLine("这将不会显示."); //if判断为false 继续跳过
Console.WriteLine("if statement executed: " + i);
if ((++i < 100) & someCondition) //i=0 someCondition=false 执行++i i=1
Console.WriteLine("这将不会显示."); // if判断为false 跳过
Console.WriteLine("if statement executed: " + i); if ((++i < 100) && someCondition) //i=1 someCondition=false 执行++i i=2
Console.WriteLine("这将不会显示."); //if判断为false 继续跳过
Console.WriteLine("if statement executed: " + i);