Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gamerocologyst · Apr 03, 2014 at 04:19 PM · c#loop

c# loop why

why c# loop count till 9 if the condition say 10 ? even if the variable is 1

int i=0;

void start ()

{

while(i < 10)

{

Debug.Log(i);

i++;

}

}

p.s and i need to know what have this to do with games ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image koray1396 · Apr 03, 2014 at 05:52 PM 0
Share

for p.s games are basicly conditions and outcomes. you shoot, if the ball passes a rectangle, then it's a goal. in hide and seek, you hide while you are not seen by the seeker. in 8 ball, you lose if you pot the 8 ball while you have other balls left.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FrancisDuranceau · Apr 03, 2014 at 04:25 PM

When i == 10, you check the condition if i is less than 10, obviously it's equal and not less so you are done looping.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gamerocologyst · Apr 03, 2014 at 04:38 PM 0
Share

so , with the condition being i<10 im telling that the number must be smaller than 10 ,like 9 ?

avatar image perchik · Apr 03, 2014 at 04:45 PM 1
Share

Right, the way the while loop works is this:

First, it checks to see if i is less than 10. When the program starts, you set i to 0, so yes, zero is less than ten.

Then, the code says write to the log (`Debug.Log(i);`) and then add one to i (`i++;`) (so now i is equal to 1)

Now, because it's a while loop, it goes back and checks the condition again. "Is i less than 10?" -yes, 1 is less than 10

and it does the inside code again.

...

eventually i=9, the code says "Is i less than 10?" -yes 9 is less than 10

write to the log, add 1 to i. i is now 10.

the code asks again "Is i less than 10? ***-NO. 10 is not less than 10."

so then it moves past the while loop

avatar image
-1

Answer by wijesijp · Apr 03, 2014 at 04:26 PM

  int i = 0;
 
     void start()
     {
 
         while (i <= 10)
         {
 
             Debug.Log(i);
 
             i++;
 
         }
 
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wijesijp · Apr 03, 2014 at 04:57 PM 0
Share

I provide a valid and correct answer. When I started answering it there were no replies. FrancisDuranceau beat me to it.

But why would anyone -1 this?

avatar image gamerocologyst · Apr 03, 2014 at 05:12 PM 0
Share

i didnt downvoted you :/

avatar image wijesijp · Apr 03, 2014 at 05:46 PM 0
Share

I took users code, fixed his error, post the answer I expected the user to compare his code with $$anonymous$$e and figure out what he has done wrong.

Worthwhile input is really subjective

avatar image gamerocologyst · Apr 03, 2014 at 06:03 PM 0
Share

i was asking why the number stop on 9 ,when my condition is 10

my int was 0 ,so i tought the loop count 10 times , since the

condition was (i < 10 times) XD

0 = 1

1 = 2

2 = 3

3 = 4

4 = 5

5 = 6

6 = 7

7 = 8

8 = 9

9 = 10

even if my variable is 1 it stops on 9 ,so in theory the loop

count 9 time ins$$anonymous$$d of 10 times

like this

1 = 1

2 = 2

3 = 3

4 = 4

5 = 5

6 = 6

7 = 7

8 = 8

9 = 9

because i was sure that the condition is a iteretion

i hope you understand what i mean :) because English is not my main language :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I dont know why im getting a null reference exception 1 Answer

Executing coroutines consecutively 0 Answers

Increase value through frames, or in a while? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges