Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 5th, 2009, 3:38 PM   #1
kong
Hacker in Training
 
kong's Avatar
 
Join Date: Jan 2009
Posts: 104
Rep Power: 6 kong is on a distinguished road
Question Nested IF Statements

Hello! can someone help me explain the Nested If statement. I'm a little bet confuse. thank in advance!
kong is offline   Reply With Quote
Old Feb 5th, 2009, 4:29 PM   #2
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 10 Arla is on a distinguished road
Re: Nested IF Statements

Not sure if VB syntax but something like this

If you had two conditions, A and B, and then you wanted to do 4 things, depending on whether A was true, B was true, A and B are true, or neither are true, you could do it like this (note this is Pseudo Code)

IF A = TRUE
{
  IF B = TRUE
  {
      PRINT "A AND B ARE TRUE!"
  }
  ELSE
  {
     PRINT "A IS TRUE, BUT B IS FALSE"
  }
ELSE
  IF B = TRUE
  {
     PRINT "A IS FALSE, BUT B IS TRUE"
  }
  ELSE
  {
    PRINT "A AND B ARE FALSE"
  }
}

Not necessarily the BEST way to do this, but it would do it (and hopefully explains nested if's)
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline   Reply With Quote
Old Feb 5th, 2009, 5:04 PM   #3
sh250080
Super Trooper
 
sh250080's Avatar
 
Join Date: Jan 2009
Location: NW Arkansas
Posts: 35
Rep Power: 0 sh250080 is on a distinguished road
Re: Nested IF Statements

If A = True And B= True Then
'
End If

If A = True Then
'
ElseIf B= True Then
'
End If

If A = True Then
'
Else
'
End If

Something along those lines.

You could also do:
If a = true then
'
if b = true then
'
if c true then
'
Elseif a = false then
'
Else
End If
End if
End if
sh250080 is offline   Reply With Quote
Old Feb 5th, 2009, 10:55 PM   #4
kong
Hacker in Training
 
kong's Avatar
 
Join Date: Jan 2009
Posts: 104
Rep Power: 6 kong is on a distinguished road
Re: Nested IF Statements

Quote:
Originally Posted by Arla View Post
Not sure if VB syntax but something like this

If you had two conditions, A and B, and then you wanted to do 4 things, depending on whether A was true, B was true, A and B are true, or neither are true, you could do it like this (note this is Pseudo Code)

IF A = TRUE
{
  IF B = TRUE
  {
      PRINT "A AND B ARE TRUE!"
  }
  ELSE
  {
     PRINT "A IS TRUE, BUT B IS FALSE"
  }
ELSE
  IF B = TRUE
  {
     PRINT "A IS FALSE, BUT B IS TRUE"
  }
  ELSE
  {
    PRINT "A AND B ARE FALSE"
  }
}

Not necessarily the BEST way to do this, but it would do it (and hopefully explains nested if's)
ok I think I get what you are trying to said. Correct me if I'm wrong. Basically with the Nested if Statements, you can have two condition and a statement to execute the then statements

for example,

if first condition then
if second condition then
statement to execute
else
statement
end if

is this right. Can you show me an example. again thank for the help.
kong is offline   Reply With Quote
Old Feb 8th, 2009, 11:23 PM   #5
kong
Hacker in Training
 
kong's Avatar
 
Join Date: Jan 2009
Posts: 104
Rep Power: 6 kong is on a distinguished road
Re: Nested IF Statements

Yes I figure it out! if A = true AND B = true AND C = true then statement to execute!
elseif C = true AND D = true then
statement

yes I got it. thank all for replay!
kong is offline   Reply With Quote
Old Feb 9th, 2009, 12:11 AM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 887
Rep Power: 9 Jimbo is on a distinguished road
Re: Nested IF Statements

Nesting just refers to having one thing inside the body of another. It applies to if statements, loops, and various other constructs. Specific to the if clause would be something like:
vb Syntax (Toggle Plain Text)
  1. If A = True Then
  2. If B = True Then
  3. A_and_B()
  4. End If
  5. A() ' doesn't matter what the value of B is
  6. End If
__________________
<insert disclaimer here>
Jimbo is offline   Reply With Quote
Old Feb 9th, 2009, 10:28 AM   #7
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 777
Rep Power: 10 Arla is on a distinguished road
Re: Nested IF Statements

Quote:
Originally Posted by kong View Post
ok I think I get what you are trying to said. Correct me if I'm wrong. Basically with the Nested if Statements, you can have two condition and a statement to execute the then statements

for example,

if first condition then
if second condition then
statement to execute
else
statement
end if

is this right. Can you show me an example. again thank for the help.
I think your example is a bit confusing (although possibly because PFO doesn't seem to show the tab characters in your post) because it's hard to tell what if statement the else applies to.

Overall nested if's are just conditions within conditions, sometimes it's good to have just big IF statements
IF A and B and C
     DO SOMETHING
ELSE
    DO SOMETHING ELSE
END-IF

othertimes it's good to have nested IF statements
IF A
  IF B
       DO SOMETHING (when A and  B)
    ELSE
       DO A DIFFERENT SOMETHING (when A but not B)
  END-IF
ELSE
  DO SOMETHING (when not A)
END-IF

you have to think about what you are trying to do and what is the best way to code it (and then sometimes what the best way for maintenance is, because while IF A and B and C and not D and (E or F) and G, might make sense to you, maintenance is probably going to be a nightmare.
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline   Reply With Quote
Old Feb 27th, 2009, 1:39 PM   #8
exception
Software & Web Developer
 
exception's Avatar
 
Join Date: Oct 2008
Location: Philippines
Posts: 173
Rep Power: 7 exception is on a distinguished road
Send a message via Yahoo to exception
Re: Nested IF Statements

If You are not a boy then
if you are not a girl then
Print "Your a gay or a Lesbian"
end if
end if

but more efficient to use is this..

if you are not a boy And you are not a girl then
Print "Your a gay or a Lesbian
end if
__________________
|Knowledge alone is not POWER, Applied knowledge is POWER.
exception is offline   Reply With Quote
Old Feb 27th, 2009, 10:24 PM   #9
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,676
Rep Power: 11 lectricpharaoh will become famous soon enough
Re: Nested IF Statements

I hate to burst your bubble, but gays and lesbians (actually, the term 'gay' includes both males and females) tend to be either boys or girls (men or women). I think the term you are looking for is 'hermaphrodite', or (more politically-correct) 'intersex'.
__________________
My microwave has settings for snake, gremlin, and puppy.
lectricpharaoh is offline   Reply With Quote
Old Feb 27th, 2009, 10:58 PM   #10
kong
Hacker in Training
 
kong's Avatar
 
Join Date: Jan 2009
Posts: 104
Rep Power: 6 kong is on a distinguished road
Re: Nested IF Statements

Quote:
Originally Posted by exception View Post
If You are not a boy then
if you are not a girl then
Print "Your a gay or a Lesbian"
end if
end if

but more efficient to use is this..

if you are not a boy And you are not a girl then
Print "Your a gay or a Lesbian
end if
lol this thread is almost a month old! It looks like you are asking a question! moderators please close this thread!
kong is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick nested loop question stevief C 2 Nov 11th, 2008 7:21 AM
if and else statements Cisnotforme C 2 Mar 3rd, 2008 5:41 PM
Simple Pointers (find what's wrong in statements) aznballerlee C++ 13 Nov 23rd, 2006 5:33 PM
Nested Lists Mjordan2nd Python 2 Oct 22nd, 2005 4:41 PM
For Each ... Next, Nested Array issues. ChefBoiAreDee Visual Basic 2 Mar 2nd, 2005 5:26 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC