need clarification on 2 simple things

Started by
15 comments, last by graveyard filla 20 years, 1 month ago
can you not use a switch statement unless your evaluating one variable? IE it has to be switch (variable) case whatever: blah and NOT switch (variable - x) case whatever: blah??? and do all of an arrays elements automatically initialize to 0 when the array is declared? IE if i do int array[5]; will array[0] through array[4] be automatically initialized to 0?? the reson im asking is, because my professer (first semester CIS) is telling me that you can only use a switch if your evaluating a single variable. and he also told me that an arrays elements initialize to 0 automatically, but im not sure if hes right about either of these. he also didnt know what a recursive function was, he uses iostream.h instead of using namespace std; (i tried telling him that there is a difference but he doesnt believe me) , and he told my class that main should return void because main will never have to return anything. basically, hes an idiot and i dont trust anything he teaches me. ive honestly learned MUCH more from reading this board then i learned the whole semester with this jerk. thanks for any help! [edited by - graveyard filla on February 20, 2004 4:24:20 PM]
FTA, my 2D futuristic action MMORPG
Advertisement
quote:Original post by graveyard filla
can you not use a switch statement unless your evaluating one variable?

No, it may be any expression. Try it.

quote:
and do all of an arrays elements automatically initialize to 0 when the array is declared?


No, they won''t.
Visit our homepage: www.rarebyte.de.stGA
1) The syntax for switch is
switch(exspression) { case constant: break; }
So yes, variable-x would be perfectly valid.

2) Iffy. An array of a class will be default constructed.
int array[5] = { 0 }; supposably initializes them all to 0.

3) int main() {}. Main must always return an int. It doesn''t actually need a return statement though.
I would write up a very detailed and commented code that proves both of those points wrong in a clearly understandable way, and present it to the class.

Of course, proving your teacher wrong like this can have repurcussions...


I''m not kidding when I say this...

I corrected a teacher reading from a science book in 5th grade who pronounced Socrates as "SO CRATES"
WyrmSlayer RPG - In Early Development
thanks for the clarification. so, i could actually write ANY expression inside an case statement? such as

switch (a < b && c < d)


case ??: //<-----------what goes here?

how does this work???

also, it angers me very much to even think about how stupid my teacher is. it sickens me, actually. to think im paying thousands of dollars for school, just to learn bad programming practises. this isnt the first teacher ive had, either, whos taught bad programming. im coming up to the third semester now, and i guess im gunna stick with it. but if my new programming class has another idiotic teacher, im going to seriously consider switching schools. i just dont know if my credits will be transferrable or not, and that would suck to lose a years worth of school for nothing. i wish i did good in high school, so i could go to a good college. sigh..........


FTA, my 2D futuristic action MMORPG
I'm assuming this is all C++?

quote:Original post by graveyard filla
so, i could actually write ANY expression inside an case statement? such as

switch (a < b && c < d)


case ??: //<-----------what goes here?

how does this work???


Yes, you can do that. But you should try to answer your own question about "what goes here?"...

What type does the expression (a < b && c < d) evaluate to? Once you understand that, it should be obvious what Case statements you can write (hint, there is only two!).

Your teacher probably meant that a switch can only be used to evaluate a single expression, but it does sound like he doesn't know what he's talking about (especially on the array thing). I would try to foster the impression that you want to help correct for the benefit of the entire class rather than make him look bad...

quote:
also, it angers me very much to even think about how stupid my teacher is. it sickens me, actually. to think im paying thousands of dollars for school, just to learn bad programming practises. this isnt the first teacher ive had, either, whos taught bad programming. im coming up to the third semester now, and i guess im gunna stick with it. but if my new programming class has another idiotic teacher, im going to seriously consider switching schools. i just dont know if my credits will be transferrable or not, and that would suck to lose a years worth of school for nothing. i wish i did good in high school, so i could go to a good college. sigh..........


Teachers are just people. Some people are brilliant, some people are smart, some people are average and some people are dumb. It's unfortunate that you don't have brilliant teachers, but that's the case for a majority of schools. Take consolation that you can learn from other sources like the web, library and playing with things on your own.

If you didn't do good in high school, was that the teacher's fault?

[ Little Devils Inc. ]

[edited by - rypyr on February 20, 2004 6:14:25 PM]
The expression evaluates to true or false, so a bool in C++ and an int in C (since it doesn''t have ''bool'').

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
quote:Original post by Deyja
3) int main() {}. Main must always return an int. It doesn't actually need a return statement though.


This last one is compiler - specific. Try building it on a compiler for an ARM device and youll drive it crazy (without a return stmt).... hehe

[edited by - psamty10 on February 20, 2004 6:16:07 PM]
so if i did

switch (a > b && c < d)

case true: blah blah
break;

case false: blah blah blah
break;


this would work? so using a case statement when evaluating an expression as opposed to evaluating a single variable, there could only be 2 different results???





[edited by - graveyard filla on February 20, 2004 6:56:22 PM]
FTA, my 2D futuristic action MMORPG
It''s because of the type of the expression - and thus the possible values the expression could have.

switch (some_integer * 3) {
case 3:
case 6:
case 9:
do_something();
case 10: /* can''t happen, silly! */
find_WMDs(Iraq);
}

This topic is closed to new replies.

Advertisement