Lots of Questions!

Oct 26, 2011 at 9:09pm
Hey guys, so far in my C++ class I have stumbled upon a few things or just in need of some clarification. If you guys can answer me, this would be a great help!

1.) What is the main difference between break; and continue;?

I understand their main function lies within if(). For example, you make an if for when an equation reaches a denominator of 0. You would use break; which would end the loop at that point and then continue on with the program...

What exactly does continue do then, assuming that I have understood break correctly..?

2.) What is the main difference between while() and for()?

I understand that for() is technically a lot easier compared to while(). I did notice that for tends to have multiple statements such as for(i=3; i<10; i++;) compared to while which is usually while(i<4) or something...

Are there really any other differences between the two?

3.) This is what stumps me the most. So far my professor has said that every exam will have problems dealing with integers and doubles. I just can't wrap my mind around these problems. If you are uncertain of what I mean, this is an example...


3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4

How would you go about solving this? What is the integer and double answer?

--Does anyone know of a website with practice problems such as the above?

Thanks!
Oct 26, 2011 at 9:30pm
lirik wrote:
1.) What is the main difference between break; and continue;?


break; will exit your loop and continue with your program.
continue; will end execution of the current iteration of your loop but it will continue going through the loop.

lirik wrote:
2.) What is the main difference between while() and for()?


for() has everything it needs to do a loop contained in it's structure.
while() is usually testing a value that changes elsewhere, such as a boolean when something has completed. While can also be used to do a loop that will always execute one last time with the "do...while()" form. Otherwise they do the same thing, which is cause a loop in your program.

lirik wrote:
3.) This is what stumps me the most. So far my professor has said that every exam will have problems dealing with integers and doubles. I just can't wrap my mind around these problems. If you are uncertain of what I mean, this is an example...
3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4


exp(3/5) is an exponential number with decimal places, and value 1.25 would be a double. (Although I would use a float for this personally). Also, 3/5 and 4/3 result in values with decimals. Integers can't handle these values so a double(or a float) needs to be used otherwise you loose those decimal places.
Oct 26, 2011 at 9:43pm
Thanks for 1.) and 2.) makes a lot more sense!

For 3.)

So does that mean all of those fractions zero-out or something?
Last edited on Oct 26, 2011 at 9:50pm
Oct 26, 2011 at 9:50pm
3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4

4/3 is 1.3333, but if you put it into an integer it would simply be 1.
3/5 is 0.6, and would end up being 0 if stored as an integer.
exp(3/5) is 1.8221188, and again is only 1 when stored as an integer.

Basically, integers drop the decimal values, and doubles do not.
Oct 26, 2011 at 9:50pm
It means you lose the decimal portion. 3/5=0.6=0 and 4/3=1.3=1
It's because 3, 4, and 5 are integers, so / does integer division (only whole numbers).
Oct 26, 2011 at 9:57pm
What if the question asks for the answer if it is an integer and when it is a double?


Would you not zero-out the fractions if it were a double? so double x = "that equation"
Last edited on Oct 26, 2011 at 9:57pm
Oct 26, 2011 at 10:16pm
double x = 3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4
x = 0.6 * (1.25-2) + 1.33 * (2-(4*2)) + exp(0.6) + 0.75
x = 0.6 * (-0.75) + 1.33 * (-6) + 1.8221188 + 0.75
x = -5.8578812

int x = 3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4
x = 0 * (1-2) + 1 * (2-(4*2)) + exp(0) + 0
x = 0 * (-1) + 1 * (-6) + 1 + 0
x = -5
Oct 26, 2011 at 10:24pm
Thank You bcthund! That makes a lot of sense now :)
Topic archived. No new replies allowed.