944,214 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 17174
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 31st, 2006
0

if then statement

Expand Post »
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++
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
grunge man is offline Offline
169 posts
since Mar 2006
Mar 31st, 2006
0

Re: if then statement

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
C++ Syntax (Toggle Plain Text)
  1.  
  2. if( g == 3282)
  3. {
  4. // true condition
  5. g = k;
  6. }
  7. else
  8. {
  9. // false statement
  10. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Apr 1st, 2006
0

Re: if then statement

>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:
C++ Syntax (Toggle Plain Text)
  1. if a = b then
  2. <statements>
  3. endif
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  1. a b = if
  2. <statements>
  3. then
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 1st, 2006
0

Re: if then statement

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
Reputation Points: 10
Solved Threads: 2
Junior Poster
grunge man is offline Offline
169 posts
since Mar 2006
Apr 1st, 2006
0

Re: if then statement

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 1st, 2006
0

Re: if then statement

Quote 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Apr 1st, 2006
0

Re: if then statement

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
C++ Syntax (Toggle Plain Text)
  1. (if a==b) /*then*/{a=2}
?
Reputation Points: 10
Solved Threads: 2
Junior Poster
grunge man is offline Offline
169 posts
since Mar 2006
Apr 1st, 2006
0

Re: if then statement

also i tried that here but it didnt work
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Junior Poster
grunge man is offline Offline
169 posts
since Mar 2006
Apr 1st, 2006
0

Re: if then statement

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Apr 1st, 2006
0

Re: if then statement

nvm ignore that one pleas answer the edited question above what u just wrote and pleas answere what i need to fix
Reputation Points: 10
Solved Threads: 2
Junior Poster
grunge man is offline Offline
169 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: radix sort using queue and linked list
Next Thread in C++ Forum Timeline: C++ Compile Error's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC