首页 > 试题广场 >

for(int x = 0 , y = 0; !x && y

[单选题]
for(int x = 0 , y = 0; !x && y <= 5 ; y++)
语句执行循环的次数是多少次?
  • 0
  • 5
  • 6
  • 无数次
推荐
如果循环体中X得值不改变,!x一直为true,则只需要考虑y的值就可以了,一共6次。
编辑于 2015-02-07 16:45:00 回复(0)
算数运算符>关系运算符>&&>||>赋值运算符
y = 0; !x && y <= 5 就是 (!x) && (y <= 5)
发表于 2017-04-17 21:25:42 回复(0)
这里有个坑,x是不会变的,始终为0
发表于 2017-06-15 09:45:01 回复(3)
选C. 关键在于判断表达式的执行顺序: (!x)&&(y<=5)。(!x)一直为true,相当于只有(y<=5)这一个条件。
发表于 2018-12-12 14:24:22 回复(0)
Precedence Operator Description Example Associativity
1 ()
[]
->
.
::
++
--
Grouping operator
Array access
Member access from a pointer
Member access from an object
Scoping operator
Post-increment
Post-decrement
(a + b) / 4;
array[4] = 2;
ptr->age = 34;
obj.age = 34;
Class::age = 2;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...
left to right
2 !
~
++
--
-
+
*
&
(type)
sizeof
Logical negation
Bitwise complement
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Cast to a given type
Return size in bytes
if( !done ) ...
flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
address = &obj;
int i = (int) floatNum;
int size = sizeof(floatNum);
right to left
3 ->*
.*
Member pointer selector
Member pointer selector
ptr->*var = 24;
obj.*var = 24;
left to right
4 *
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float f = 10 / 3;
int rem = 4 % 3;
left to right
5 +
-
Addition
Subtraction
int i = 2 + 3;
int i = 5 - 1;
left to right
6 <<
>>
Bitwise shift left
Bitwise shift right
int flags = 33 << 1;
int flags = 33 >> 1;
left to right
7 <
<=
>
>=
Comparison less-than
Comparison less-than-or-equal-to
Comparison greater-than
Comparison geater-than-or-equal-to
if( i < 42 ) ...
if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...
left to right
8 ==
!=
Comparison equal-to
Comparison not-equal-to
if( i == 42 ) ...
if( i != 42 ) ...
left to right
9 & Bitwise AND flags = flags & 42; left to right
10 ^ Bitwise exclusive OR flags = flags ^ 42; left to right
11 | Bitwise inclusive (normal) OR flags = flags | 42; left to right
12 && Logical AND if( conditionA && conditionB ) ... left to right
13 || Logical OR if( conditionA || conditionB ) ... left to right
14 ? : Ternary conditional (if-then-else) int i = (a > b) ? a : b; right to left
15 =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise shift left and assign
Bitwise shift right and assign
int a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;
flags ^= new_flags;
flags |= new_flags;
flags <<= 2;
flags >>= 2;
right to left
16 , Sequential evaluation operator for( i = 0, j = 0; i < 10; i++, j++ ) ... left to right

发表于 2016-02-28 21:27:44 回复(0)
小于等于,会多增加一次
发表于 2021-03-23 21:57:28 回复(0)
比较运算符优先级高于逻辑运算符
发表于 2023-03-15 16:53:07 回复(0)
1&&5=5
发表于 2022-09-26 00:52:10 回复(0)
想差了,想成考察||短路特性了
&&的短路特性是前一个要为假,后一个才不会运行的吧
编辑于 2016-08-12 06:51:16 回复(2)
这里考察的是逻辑运算的短路特性,
  • 对于逻辑与运算,当左操作表达式为假时,将会短路 &&  右侧的表达式,即 && 右侧的表达式不会被求值;
  • 对于逻辑或运算,当左操作表达式为真时,将会短路 || 右侧的表达式,即 || 右侧的表达式不会被求值;
发表于 2015-09-25 21:05:04 回复(0)
y <= 5的优先级要比&&高,所以相当于(!x )&& (y <= 5 )
发表于 2024-08-30 21:25:49 回复(0)
这里考察的是运算符的优先级
发表于 2022-05-10 11:32:41 回复(0)
非0且<5才执行?x=0
发表于 2018-11-03 21:09:23 回复(1)

循环执行了6次,答案错了

发表于 2018-08-15 09:49:34 回复(0)
又倒在运算符优先级……&&比条件运算符低
发表于 2018-04-03 15:51:23 回复(0)
&& 前真,后判断
发表于 2017-12-09 08:27:30 回复(0)
说的是语句循环执行的次数,不是循环了多少次
发表于 2017-09-16 17:12:50 回复(0)
每一次执行的时候y不会重新变为0吗
发表于 2017-06-18 22:22:46 回复(0)
这里x为0,为什么循环会执行呢?
发表于 2017-04-05 22:58:31 回复(1)
这题for后边加个;
发表于 2017-04-05 13:09:06 回复(0)
虽然&&之前为真,但是y++执行吖……
发表于 2017-03-28 11:18:21 回复(0)