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
if( g == 3282)
{
// true condition
g = k;
}
else
{
// false statement
}
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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:
if a = b then
<statements>
endif
if ( a == b ) {
<statements>
}
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):
a b = if
<statements>
then
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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:
#define then {
#define endif }
if ( a == b ) then
<statements>
endif
But, don't do that. You won't be able to show your code to very many people before they lynch you. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>{g=0}
You need a terminating semicolon:
{g=0;}
These things are easier to see if you separate the statements from the brackets and use whitespace between tokens consistently:
if (g == 3282)
{
g = k;
}
else
{
g = 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>but for me i am an html person so it is easy the other way for me but i see your point.
Well, HTML technically isn't a programming language, so now you're learning the real thing. :)
>is it posible for me to nip my own threads?
No, only forum moderators can lock threads. You can, however, mark your own thread as solved when you feel your question has been answered to your satisfaction. The option is just above the first post, to the right of the Advanced Reply button.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
with some exseptions
and there are a lot of exceptions :cheesy: you can't even do file i/o with it. the only useful thing HTML can be used for is displaying stuff in browser windows. (I'm not an HTML programmer).
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343