Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 9th, 2003, 01:38 PM
dmonk dmonk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 22 dmonk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question About Curly brackets { }

I'm new to nested if statements and I'm loosing track of those curlies. Is this the right way of writing these statements?

PHP Code:
if ($var1 $var2) {
    echo 
"statement_one";

    }else{
        if (
$var2 $var1) {  // is this one too many

            
echo "statement_two";

            }  
//close nested if

          
}  //close else 

Reply With Quote
  #2  
Old July 9th, 2003, 08:40 PM
a.koepke's Avatar
a.koepke a.koepke is offline
Second highest poster :p
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jul 2001
Posts: 7,323 a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 11 m 27 sec
Reputation Power: 27
Just lay out your code properly and you wont have problems

PHP Code:
if ($var1 $var2)
 {
  echo 
"statement_one";
 }
else
 {
  if (
$var2 $var1)
   {
    echo 
"statement_two";
   }
 } 


Now we can see which bracket lines up with the other. And you have the right amount.

Reply With Quote
  #3  
Old July 9th, 2003, 08:41 PM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 22 h 49 m 14 sec
Reputation Power: 41
PHP Code:
// Just space/tab them out nicely

if ($me == $munkie)
{
    echo 
"I am a munkie!";
}
else
{
    if (
$me == $munkie)
    {
        echo 
"I am a munkie!";
    }
    else
    {
        if (
$me == $munkie)
        {
            echo 
"I am a munkie!";
        }
        else
        {
            if (
$me == $munkie)
            {
                echo 
"I am a munkie!";
            }
            else
            {
                echo 
"I think you get the idea!!!";
            }
        }
    }

__________________

Reply With Quote
  #4  
Old July 9th, 2003, 08:42 PM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 22 h 49 m 14 sec
Reputation Power: 41
Third time in a row when a.koepke beat me to it!!

Reply With Quote
  #5  
Old July 10th, 2003, 10:01 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
What can you expect, he is good.

Reply With Quote
  #6  
Old July 10th, 2003, 10:32 AM
computer's Avatar
computer computer is offline
echo $usertitle['computer'];
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Jan 2003
Location: UK
Posts: 6,675 computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level)computer User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 4 Weeks 9 h 12 m 10 sec
Reputation Power: 219
Send a message via ICQ to computer
lol. he types at an amazing 400 words a minute, seriously....

Reply With Quote
  #7  
Old July 10th, 2003, 10:53 AM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
lol. Refer to thread "this is war" from the archive. That should give you about 90 posts worth of info on the topic.

Reply With Quote
  #8  
Old July 11th, 2003, 12:00 PM
dmonk dmonk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 22 dmonk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking Thank you

I want to say thanks to everyone that responded.
great examples.

Monkey's response left no room for doubt! Thanks.

After lining up the curly brackets under the "if" and "else"
I was able to debug my app in five minutes!.

Reply With Quote
  #9  
Old July 11th, 2003, 12:08 PM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
yeah, it really pays in the long run to block your code and keep proper bracket alignment.

Reply With Quote
  #10  
Old July 11th, 2003, 02:03 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Just an interesting piece of trivia: If you wrote Sam777's code in Mumps, it'd be:

Code:
i me=monkie w "I am a monkie!"
e  d
. i me=monkie w "I am a monkie!"
. e  d
.. i me=monkie w "I am a monkie!"
.. e  d
... i me=monkie w "I am a monkie!"
... e  d
.... w "I think you get the idea!!!"

Mumps is a nice language sometimes. Just count the periods to see what loop you're in.
("e d"="Else Do")

Reply With Quote
  #11  
Old July 14th, 2003, 11:55 AM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
well thats just wierd...

Reply With Quote
  #12  
Old July 14th, 2003, 09:02 PM
a.koepke's Avatar
a.koepke a.koepke is offline
Second highest poster :p
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jul 2001
Posts: 7,323 a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 11 m 27 sec
Reputation Power: 27
Quote:
Originally posted by AGibel
well thats just wierd...


My thoughts exactly.... no wonder I havent heard of that language.
__________________
- Andreas Koepke

I have photos for sale at RedBubble


Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Question About Curly brackets { }


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |