954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

if then statement

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++

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

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
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

>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
Administrator
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
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

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

(if a==b) /*then*/{a=2}


?

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

also i tried that here but it didnt work

if( g == 3282)
{g = k;}
else
{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

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

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
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

nvm ignore that one pleas answer the edited question above what u just wrote and pleas answere what i need to fix

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

>{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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

o ok i see what you mean

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

but for me i am an html person so it is easy the other way for me but i see your point.
u know how u "niped" my other thread well is it posible for me to nip my own threads?

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

tecknickly it is a programing launguage because any computer launguage takes an input of text asined to a sirtian file type compiles it and sendes the out put to the monitor or out put device and it does all this through microprosessors converted from text to binary code to an output converter except in html it compiles over the internet server and out to the screen um and i know it is because xml 4.0 is an updated html programing launguage used in cell phones but feel free to corect me if i may be wrong

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 
tecknickly it is a programing launguage

Narue is right -- No HTML specification has ever called HTML a programming language, or anything like that. It is most commonily called a markup-language

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

ya i know hyper text markup launguage but come on it does the same function any other lauguange does

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

with some exseptions

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 
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
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

actually with javascript/css/xml ect other stuff you can creat games and other stuff um question whats i/o mean um is it mean 1/0 on off functions?

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You