C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 28th, 2002, 10:56 PM
Chooco Chooco is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Canada
Posts: 8 Chooco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Chooco
about {} braces in C++

my book is going over the part of if statements so i skimmed through it and what not.... i noticed that the author doesn't use these {} in some of his examples. at the end of the chapters, one of the assignments is to compile a code and see how it was different from what you guessed would happen. here is the code:


Code:
#include <iostream.h>
#include <stdlib.h>

int main()
{
      int a = 2, b = 2, c;
           if(c = (a-b))
                cout << "The vale of c is: ";
                cout << c;
      system("PAUSE");
      return 0;
}

i saw that the error was in 'c' not being assigned by == but that is not the point. the parts

Code:
if(c = (a-b))
                cout << "The vale of c is: ";
                cout << c;


if you change that to

Code:
if(c = (a-b))
{
                cout << "The vale of c is: ";
                cout << c;
}


it will no longer compile, it says it gets confused and it quote "bails out", why?

Reply With Quote
  #2  
Old May 29th, 2002, 08:24 AM
Alkis Alkis is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Hellas
Posts: 46 Alkis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 35 sec
Reputation Power: 8
hmm...

First of all the statement c=(a-b) it is not wrong because it does not check if the c is equal to a-b, but it the assignment of the abstraction a-b to c is succesfull.

As of the missing braces, they are not necessary, if the rest of your code does not have any other condition, and of course you want it to be in the if statement.

Now about the error you get when you insert braces, it may has to do with the system("PAUSE") statement. The reason I can think of now, is that you have already paused the application (even just for a moment) when you close the if statement with braces, so there is nothing left to pause because there is no other statement between the if and the system command. At least maybe it is somthing relative to that.

Reply With Quote
  #3  
Old May 29th, 2002, 01:24 PM
justice41 justice41 is offline
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: The Emerald City
Posts: 289 justice41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
If you are running on linux (or any other *nix) change the PAUSE to sleep 1.
system ("sleep 1");

I think that one point of the exercise is to notice the buffered output. You ( or at least i did) expect it to print the value of 'c' (0) and then pause for a sec and then end the program. That is not infact what happens. Instead when you run it, it pauses for a second and then 'displays' the value of 'c'. It's not that the system() command is being executed first, its that the output if buffered and doesn't actually print until the buffer is full or the buffer is flushed (in this case when the program ends).

edit:
Also interesting is to change a from 2 to 3. The if-statement doesn't test whether the assignment was successful, its tests the value being assigned to, in this case 'c'. if c == 0 as in the first case then this is equivalent to
if ( 0 )
which is false so it doesn't print "The value of c is:".
if you change a to 3 then the evaluation becomes
if ( 1 )
which is true so the statement IS printed. This is a danger in using assignments (among other things) as test statements.

jaa

Reply With Quote
  #4  
Old May 29th, 2002, 01:30 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
Funny thing:
Code:
int a = 2, b = 2, c;
if(c = (a-b))
    cout << "The vale of c is: ";
cout << c;
system("PAUSE");


Should output:
Code:
0Press any key to continue...


Since c is assigned the value of a - b, and equal to zero, the string from the if clause is never printed. The value 0 should then be printed, followed by the pause prompt. But I get the zero after the pause prompt. Weird.

Adding the braces shouldn't cause a problem. And infact, it works on my system, albeit for the above mentioned oddity. Like already said, it might have something to do with the system() call and how it interacts with your OS.

Reply With Quote
  #5  
Old May 29th, 2002, 07:21 PM
Alkis Alkis is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Hellas
Posts: 46 Alkis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 35 sec
Reputation Power: 8
justice41:

about the c=(a-b) we are saying the same thing. You don't need to say it in another way. if a-b is 0 the assignment in the expression is 0 so its the same as false in an if.

Reply With Quote
  #6  
Old May 29th, 2002, 08:29 PM
Chooco Chooco is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Canada
Posts: 8 Chooco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Chooco
MJ is right in terms of the output, it just displays "push any key to continue" or whatever that is......

Reply With Quote
  #7  
Old May 30th, 2002, 01:26 PM
phoenix_rizzen phoenix_rizzen is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Kamloops, BC, Canada
Posts: 55 phoenix_rizzen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Re: hmm...

Quote:
Originally posted by Alkis
First of all the statement c=(a-b) it is not wrong because it does not check if the c is equal to a-b, but it the assignment of the abstraction a-b to c is succesfull.


Slight correction. It doesn't check if the assignment is successful or not, but checks the result of the assignment.

c=3-2 is a successful assignment, and the result (1) allows the statement within the if {} to be called.

c=3-3 is also a successful assignment, but the result (0) will not allow the statement within the if {} to be called.

You might think you are saying the same thing as justice42, but you aren't.
__________________
Linux is for those who hate Windows.
FreeBSD is for those who love UNIX.
-------
Have you read The Handbook yet?
How about The FAQ?
Have you searched the mailing lists?
Or read any of the man pages?
Have you searched the web for BSD resources?
In short, have you done your homework yet?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > about {} braces in C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway