Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,511 people online right now. Registration is fast and FREE... Join Now!




short-circuit evaluation and switch statement

 
Reply to this topicStart new topic

short-circuit evaluation and switch statement

jayhuang
13 Oct, 2006 - 01:23 PM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2006
Posts: 31


My Contributions
Guys, here is a technical question. sleepy.gif

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";

Another question:

What is wrong with the following switch statement?  B)

   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;
   }


This post has been edited by Dark_Nexus: 31 Oct, 2006 - 10:07 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Short-circuit Evaluation And Switch Statement
13 Oct, 2006 - 02:37 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(jayhuang @ 13 Oct, 2006 - 02:23 PM) *

Guys, here is a technical question. sleepy.gif


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? cool.gif


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
User is offlineProfile CardPM
+Quote Post

jayhuang
RE: Short-circuit Evaluation And Switch Statement
13 Oct, 2006 - 06:05 PM
Post #3

New D.I.C Head
*

Joined: 11 Oct, 2006
Posts: 31


My Contributions
[quote name='gregoryH' date='13 Oct, 2006 - 03:37 PM' post='176845']
[quote name='jayhuang' post='176818' date='13 Oct, 2006 - 02:23 PM']
Guys, here is a technical question. sleepy.gif



[/code]
Without shortcircuits (i'll assume other % myint != 0 is true
0 != 0 && other % 0 != 0

false && true

false


I think you kinda missed this. Without short circuits, the codes wouldn't even compile. Why? cuz you can't divide any number by ZERO. so the second part of the if condition violates the math rule, and without short circuits, it got executed, and error occurs...
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Short-circuit Evaluation And Switch Statement
13 Oct, 2006 - 06:13 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
jay

correct... since the value of myInt is not known at compile time (unless its a const) the error may not show up until you run it.

I would point out that i did say "i'll assume other % myint != 0 is true", remove the assumption and you'll get different response.

This post has been edited by gregoryH: 13 Oct, 2006 - 06:40 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month