Teen Programmers Unite  
 

 

Return to forum top

C++ Newbie question2

Posted by diegoeskryptic [send private reply] at March 28, 2002, 08:42:23 PM

OK my fellow Teenaged programmers.... can someone xplain in DEPTH what a switch statement is and what assertions are.... I dont understand the definition that this damn book gives... thanx

Posted by Psion [send private reply] at March 29, 2002, 07:07:26 AM

There is no depth to the idea of switch statements. They pick from a list of choices for the value of an expression and run something.

An assertion is something that you say is true at some point in a program, and it had better be true or something has gone wrong in the program.

Posted by metamorphic [send private reply] at March 29, 2002, 08:12:52 AM

A switch statment is an alternative to many if else statments. Basic use could be:

int num = 1;

switch(num)
{
case 0:
{
cout << "The number is zero";
}
case 1:
{
cout << "The number is one";
}
case 2:
{
cout << "The number is two";
}
default:
{
cout << "error"
}
}

the output of that switch statment would be 'The number is one'

a switch statment tests a variable and exectes whichever code block corresponds to the value of the variable. it the example above the integer variable num had a value of 1. the statment then went thru the list of expressions. num wasnt 0 so the first block was ignored. the second block (testing against value 1) was correct, therefore the block was executed. the rest of the stament was then ignored as the correct block was already found. if the variable num was initialized with a value of say, 4 the default: code block is executed. Its job is so that if the whole switch statment doesnt test for a variables current value, there is always smething executed(usfull for error checking).
Hope this clafies it slighty

Marc

Posted by manoj [send private reply] at March 29, 2002, 10:38:27 AM

Hey Diego,
Explanation by metamorphic is more then enough. Just know that switch only takes int or chars as the case conditions.
If u r planning 2 take some exams in c/c++. Be sure 2 check what happens when breaks(not all) r added/removed and the use of effects etc in switch. Try to find books by Yashwant Kanetkar(Test ur c skills, exploring c etc) 2 clear all the doubts

Posted by diegoeskryptic [send private reply] at March 29, 2002, 10:56:37 AM

Thanks guys.... yall always help me out when the book cant!

Posted by RedX [send private reply] at March 29, 2002, 12:11:16 PM

Marc: you didn't tried this program, did you?
I did, you didn't put in a break at the end of each case
case 1:
{
cout << "The number is one";
break; <<<<<==========================
}

If you don't, it will execute the right case and every case after it.

RedX

Posted by Mycroft [send private reply] at March 29, 2002, 02:24:32 PM

A switch is just like a bunch of if-else statements, actually if there was no switch statement a programmer would just use if-else.

Posted by Psion [send private reply] at March 29, 2002, 02:39:14 PM

switch statements are much more efficient with many possible cases and non-stupid compilers. For instance, they can be implemented with automatic binary searches or jump tables.

Posted by manoj [send private reply] at March 29, 2002, 02:48:37 PM

It may seem stupid Psion, but I really don't know what a jump table is. Plz elaborate. I hope Diego too will like know!

Posted by Psion [send private reply] at March 29, 2002, 02:52:30 PM

All right. Here's some C code and pseudo-assembly.

switch (bob)
{
case 0:
bob = 1;
break;
case 1:
bob = 2;
break;
case 2:
bob = 3;
break;
}

... can translate as:

jumpto Table + (bob)
case0:
load 1 -> (bob)
jumpto done
case1:
load 2 -> (bob)
jumpto done
case2:
load 3 -> (bob)
done:
...

Table: .data case0, case1, case2

Posted by diegoeskryptic [send private reply] at March 29, 2002, 03:17:19 PM

yeah the the freak is a jump table... is that another reserved word in C++... ahh brother!!

Posted by manoj [send private reply] at March 29, 2002, 03:30:10 PM

OH. Thanks Psion.

Posted by metamorphic [send private reply] at March 30, 2002, 06:36:33 AM

hah thanks red, no i didnt try it. but thanks anyway. nice to know someone is looking out for me :)

Posted by vikram_1982 [send private reply] at April 02, 2002, 01:21:52 AM

Hey Diego, Seems u have got a lot of answers 4 ur question.
But what is it with the book that u have. If it is not able to clearly explain something as simple as a switch statement , i would suggest u chuck it out of the window. Try a book called "Programming in C++" by Robert Lafore. It is the best C++ book, that I have laid my hands on!!!!!

-Vikram

Posted by Faisal [send private reply] at April 02, 2002, 09:52:53 PM

try "C++ How to Program, 3e" its a good book

it costs 80 dollars with tax, so be rich

Posted by diegoeskryptic [send private reply] at April 03, 2002, 01:14:30 PM

Lol!!! Oh thats ok Faisal..... Ill download a tutorial off the internet for free!!

Posted by vikram_1982 [send private reply] at April 03, 2002, 11:46:12 PM

Hey Diego Dude,
I presume u r in college.... Why dont u try to get the book Faisal talked about from ur college library.... or try to persuade ur college to buy that book....

Vikram.

Posted by Psion [send private reply] at April 04, 2002, 08:11:36 AM

Most people here aren't in college.

Posted by diegoeskryptic [send private reply] at April 04, 2002, 02:25:20 PM

thanx Psion... that really makes me feel dumb!... lol

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.