help with if statements

I know that

if(5){
//rest of code here
}

is legal, but why would the c++ developers make it this way and how does this work? what are the benefits and such? Thank you.
if (5) would be a rather bizarre thing to do, but here is an explanation (Note that while I think I've got everything right, I don't guaranty it):

Firstly, an if statement just checks what's if what's inside the brackets, is not zero (I think that's what it does). If the expression inside the brackets is non-zero, it will execute the following block of code or following statement.

This is why when people want to create an infinite loop, they use while (1) or while (true), though you can use anything that's not zero (including negative numbers).

When you have something like if (num1 > num2), that is just really a changing true or false statement. If num1 is bigger than num2 then the statement is true, but if it's not, it's false. For boolean or other types, this works perfectly well also. For example, if (boolThing) would check if the expression 'boolThing' is non-zero, and if it is, execute the following code. The expression in this case, IS the variable.

Hope the helped and sorry if I got some of the terminology wrong :p
The only thing I can think of is just needing a block of code where they can have some data that is local to only that block. Since the if statement would always execute you could declare some variables and execute some code that goes away once the if statement terminates.

Oh, are you talking about the braces? Well, the braces just define the stuff within them as a block. Any non-static variable created within a block, is deleted once it terminates. What is the benefits. Well, it lets you group code, so instead of just having one statement in the if statement (or any statement, you can have many.
thanks for all the help :), it's tough because my computer science teacher takes three days, well actually two, to explain what the "=" sign means so my friend and I are teaching ourselves.

EDIT: I do not remember posting this... Especially to an answer to something I already knew. Thanks for the responses to whoever used my account though.
Last edited on
Topic archived. No new replies allowed.