QUOTE(jayhuang @ 13 Oct, 2006 - 02:23 PM)

Guys, here is a technical question.
If a programming language does not use short-circuit evaluation, what is the output of the following code fragment if the value of myInt is 0?
CODE
int other=3, myInt;
if(myInt !=0 && other % myInt !=0)
cout << "other is odd\n";
else
cout << "other is even\n";
think about a truth table for AND (&&)
CODE
A B | C
- - | -
0 0 | 0
0 1 | 0 << short circuit would stop here
1 0 | 0 << and here
1 1 | 1
Without shortcircuits (i'll assume other % myint != 0 is true
0 != 0 && other % 0 != 0
false && true
false
QUOTE
Another question:
What is wrong with the following switch statement?
CODE
int ans;
cout <<"Type y for yes on n for no\n";
cin >> ans;
switch (ans){
case 'y':
case 'Y': cout << "You said yes" << endl; break;
case 'n':
case 'N': cout << "You said no" << endl; break;
default: cout << "invalid answer" << endl;
}
First glance appears that you have got the switch statement right. However, a subtle bug is in there... ans is of type int, make it a char then your worries will slip away....
This post has been edited by gregoryH: 13 Oct, 2006 - 02:39 PM