| | |
please need help .. with swith and conditional operator?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
hi ..
please can anyone help me with this .. i need it today
i wanna to do swith with this .. can i or not?
if ( x>y )
cout<< " x is bigger ";
else
cout<< " y is bigger ";
--
and i wanna to do conditional operator with more than 2 expressions .. can i do that?
and is that write or wrong?
grade == 'A' ? "ex" : grade == 'B' ? "v.g" : grade == 'c' ? "g" : undefined letter
thanx
please can anyone help me with this .. i need it today
i wanna to do swith with this .. can i or not?
if ( x>y )
cout<< " x is bigger ";
else
cout<< " y is bigger ";
--
and i wanna to do conditional operator with more than 2 expressions .. can i do that?
and is that write or wrong?
grade == 'A' ? "ex" : grade == 'B' ? "v.g" : grade == 'c' ? "g" : undefined letter
thanx
Both should work.
Personally, I prefer to use lots of parentheses to make it obvious (both to myself and to the compiler) what exactly I mean to say:
Hope this helps.
Personally, I prefer to use lots of parentheses to make it obvious (both to myself and to the compiler) what exactly I mean to say:
C++ Syntax (Toggle Plain Text)
cout << ( (grade == 'A') ? "excellent" : ( (grade == 'B') ? "very good" : ( (grade == 'c') ? "good" : "not so good at all" ))) << endl;
Sure. Boolean is an ordinal type.
[EDIT] man, too slow again...
No prob DREAMER546 :-)
C++ Syntax (Toggle Plain Text)
switch (x < y) { case true: // hmm, x < y break; case false: // wait, x >= y }
[EDIT] man, too slow again...
No prob DREAMER546 :-)
Last edited by Duoas; Oct 29th, 2007 at 3:51 pm.
Yoinks, iamthwee, at least she is asking to learn...
This is an expression:
as long as something evaluates to an integral expression then the switch will work just fine.
Nobody uses switch for boolean expressions because it is faster and easier to read just to use an if. The if, btw, also takes an integral expression. If zero, it is understood as false. If non-zero, it is understood as true.
Hope this helps.
This is an expression:
x > y. It evaluates to an integral (aka ordinal) value (specifically, a bool), which is what the switch statement takes. So when you sayswitch (something) {as long as something evaluates to an integral expression then the switch will work just fine.
Nobody uses switch for boolean expressions because it is faster and easier to read just to use an if. The if, btw, also takes an integral expression. If zero, it is understood as false. If non-zero, it is understood as true.
Hope this helps.
![]() |
Similar Threads
- c++ functions to compare numbers (C++)
- usin the ?: operator (C++)
- Pseudocode (Computer Science)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Eliminate Left Recursive Grammar (Computer Science)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
- TO CHAINSAW - or anybody else who knows C++ (C++)
- probem with code of conditional operators (C)
Other Threads in the C++ Forum
- Previous Thread: how do I have txt file be read by the compiler?
- Next Thread: Problem with vectors
Views: 1124 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct studio temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






