Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Syntax of nested if statement is

C#
if(Condition-1)
{
    if(condition-2)
    {
        statement-2;
    }
    Statement3;
}



i want to know that if condition 1 is false Compiler goes to ckeck condition-2 or not? if condition-1 is false what it do?
Posted
Updated 27-Jul-11 4:18am
v2

Condition 2 will only be evaluated if Condition 1 asserted.

If Condition 1 is false, then nothing will happen.
Condition 2 won't be evaluated and Statement 3 won't be executed neither.

If Condition 1 is True then Condition 2 will be evaluated, if false then only Statement 3 will be executed, otherwise both Statements (2 and 3) will be executed.

Hope it helps
 
Share this answer
 
v2
You should learn to use a debugger and buy a good C++ book. You can put an else statement after an if, as in

if (conditiond2)
{
statement2
}
else
{
statement3
}

if you don't do that, then statement 3 will always run, because you're asking it to.

In addition, you nested the second condition, thus telling the program to only check it if the first succeeds. Put the second check out side the first check, if you want both to always be checked.
 
Share this answer
 
v2
Comments
Emilio Garavaglia 27-Jul-11 12:59pm    
Christian, since you got that you got it wrong (gulp!), ... delete this to avoid confusion!
No, because the second if statement only gets looked at if condition-1 is TRUE
 
Share this answer
 
Comments
Christian Graus 27-Jul-11 10:17am    
Oh, I read that backwards, his question was more simple than I thought, and his example convoluted.
Richard MacCutchan 27-Jul-11 10:28am    
I also missed the second question, and I'm not 100% sure I answered what he was really asking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900