if then statement

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

if then statement

 
0
  #1
Mar 31st, 2006
how do u write this if then statment but in c++ if g=3282 then g=k;
to bad there wasnt a then like there is an else in c++
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: if then statement

 
0
  #2
Mar 31st, 2006
c and c++ does not use the "then" statement as some other languages do. The next statement or block following the if statement is executed when the if statement is true. Notice the use of the double '=' symbol which is the logical equal. A single '=' means assignment. Lots of people get tripped up on those differences -- even some old timers like me :o
  1.  
  2. if( g == 3282)
  3. {
  4. // true condition
  5. g = k;
  6. }
  7. else
  8. {
  9. // false statement
  10. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: if then statement

 
0
  #3
Apr 1st, 2006
>how do u write this if then statment but in c++ if g=3282 then g=k;
Don't you have a book? Learning a language is extremely difficult without a proper reference available.

>to bad there wasnt a then like there is an else in c++
Why? "then" is typically used as a language defined parsing helper for the compiler, like the parentheses in C++. When the parser finds "if", it processes a conditional test until it finds the corresponding "then". Compare:
  1. if a = b then
  2. <statements>
  3. endif
  1. if ( a == b ) {
  2. <statements>
  3. }
It's pretty obvious that this framework is already in place in C++; it just doesn't use the same tokens.

Now, an interesting tidbit that's largely unrelated is that "then" isn't always used like that. In a stack based language like FORTH, the parsing trick isn't needed, and "then" is used as a conditional terminator (what other languages use "endif" for):
  1. a b = if
  2. <statements>
  3. then
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

Re: if then statement

 
0
  #4
Apr 1st, 2006
whoa narue thats cool that there is an endif thanks um also thanks for all the advice u have given me it has taught me to have disipline during social speach
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: if then statement

 
0
  #5
Apr 1st, 2006
>whoa narue thats cool that there is an endif
Well, not in C++. But you can fake it with macros if you're a sadist:
  1. #define then {
  2. #define endif }
  3.  
  4. if ( a == b ) then
  5. <statements>
  6. endif
But, don't do that. You won't be able to show your code to very many people before they lynch you.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: if then statement

 
0
  #6
Apr 1st, 2006
Originally Posted by Narue
But, don't do that. You won't be able to show your code to very many people before they lynch you.
You are absolutely right about that! I once worked on a large-team project where one of the programmers came from PASCAL -- he created almost everything in macros so that his c++ program looked just like a PASCAL program. After he left we all cursed him for that and rewrite his programs to remove those macros.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

Re: if then statement

 
0
  #7
Apr 1st, 2006
thats amasing so macros are suplimentations from other codes, but instead i allso agree that thats stupid. if u want to creat a program in another launge then u should just supliment that santax into an other programs santax instead of using macros, and ok i thought i got it but now im not shure um does this soud corect anyways though?
the { }brackets are sublimented for the then, so
  1. (if a==b) /*then*/{a=2}
?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

Re: if then statement

 
0
  #8
Apr 1st, 2006
also i tried that here but it didnt work
  1. if( g == 3282)
  2. {g = k;}
  3. else
  4. {g=0}
here r the errors
C:\Dev-Cpp\Untitled2.cpp In function `int main(int, char**)':
37 C:\Dev-Cpp\Untitled2.cpp expected `;' before '}' token
here r the errors
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: if then statement

 
0
  #9
Apr 1st, 2006
see my previous post for example how to use the brackets. And put them on a separate line to make them easier to see -- helps understand the code better.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

Re: if then statement

 
0
  #10
Apr 1st, 2006
nvm ignore that one pleas answer the edited question above what u just wrote and pleas answere what i need to fix
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC