What are Loops? (C# Basics, for, while, do)

Поделиться
HTML-код
  • Опубликовано: 1 дек 2024

Комментарии • 70

  • @CodeMonkeyUnity
    @CodeMonkeyUnity  4 года назад +8

    🌍 FREE C# Beginner Complete Course! ruclips.net/video/pReR6Z9rK-o/видео.html
    🔴 Watch my Complete FREE Game Dev Course! 🌍 ruclips.net/video/AmGSEH7QcDg/видео.html
    📝 C# Basics to Advanced Playlist ruclips.net/p/PLzDRvYVwl53t2GGC4rV_AmH7vSvSqjVmz
    🌐 Have you found the videos Helpful and Valuable?
    ❤️ Get my Courses @t or my Steam Games 🎮 @t
    Here's another C# Basics video, this time covering on the most important things to know as a programmer, Loops!

  • @alexw9309
    @alexw9309 4 года назад +64

    Dont use
    for (int i = 0; i < array.lenght; i++)
    Better use
    for (int i = 0, int max=array.lenght; i < max; i++)
    In the 1. the lenght will be calculated on every loop, on the 2. only once. If u have big loops this could make a speed difference.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +10

      Yup, if performance is a concern then caching the length is a nice an easy way to get some ms

    • @GymCritical
      @GymCritical 4 года назад +3

      This is definitely good for my inventory script which is taking up approx. 0.27ms rn. Scalability wise, this’ll help.

    • @nemesis8508
      @nemesis8508 4 года назад +4

      Commenting to remember this later

    • @anormalguy8407
      @anormalguy8407 3 года назад +1

      thanks, this is going into my collection of performant code :D

    • @nexgen.graphics
      @nexgen.graphics 3 года назад +1

      Makes sense. Thanks for this.!

  • @mephistonight2958
    @mephistonight2958 4 года назад +9

    You're very quickly become the best Unity channel on RUclips, you know? No fuss, no messing about... straight in to it covering the important information in a digestible way. Nice one.

  • @JTAGames
    @JTAGames 4 года назад +5

    This helps tremendously with the project I’m working on right now, thanks!

  • @dhee9040
    @dhee9040 Год назад

    Best vedio on c# loops I've seen everrr

  • @JG-bb4dc
    @JG-bb4dc 4 года назад +2

    Thank you Code Monkey. Really appreciate you making this video on the basic C# loops. Looking forward to your next tutorial like this on C#. Keep making these awesome videos on Coding C# with Unity!!!

  • @michaelesch
    @michaelesch 2 года назад

    Thank you so much! I've been struggling with the concept of loops and I think it is starting to click.

  • @Aye_Sid
    @Aye_Sid 2 года назад +1

    Finally completed all videos in this playlist 🎉. Thanks for this invaluable playlist sir. 🙌🏻
    But please can you make a video on switch statements. 🙏🏻😇

  • @diegoolivares2669
    @diegoolivares2669 2 месяца назад

    thanks for the info was so useful, very well explained.

  • @jackbamford9749
    @jackbamford9749 4 года назад

    We need more videos on this series🙌

  • @Andrew90046zero
    @Andrew90046zero 4 года назад

    I personally have never used the continue keyword, but I bet there are some places where I could use it.
    Also, for anyone else reading. I have heard for loops being called "count controlled loops" in case you hear that term.
    The idea is that while loops are more used for things you are not incrementing repeatedly. And have no solid predefined amount of iterations. So a while loop would be used to hold a simple console program open. Whereas for loops are more for iterating over data structures with a finite size, and therefore, have a known amount of total loops.

  • @ThePostalGril
    @ThePostalGril 4 года назад

    i like your teaching style. i'm learning c# :) i want to make little games. not for profit tho i know someone like me could never make good games but i wanna do it for fun :)

  • @makinamieclair3769
    @makinamieclair3769 4 года назад +1

    you can scan list in reverse order to remove element

  • @RawHunterX
    @RawHunterX 4 года назад +1

    Great vid. Can you do Events in the future?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      I have covered Events here ruclips.net/video/OuZrhykVytg/видео.html

  • @sagiziv927
    @sagiziv927 4 года назад

    Important to note that in the for loop you are not forced to write all 3 blocks, you can skip some of them if needed

  • @anormalguy8407
    @anormalguy8407 3 года назад

    I already know how to use these in java but watched anyways and found out that certain syntaxes are different so I guess I still learned some stuff :D

    • @anormalguy8407
      @anormalguy8407 3 года назад

      also very nice tip on how to remove an object in a list

  • @supertenchoo4271
    @supertenchoo4271 4 года назад

    my brain is boost with loop...make this basic tutorial it real help us who are new in programming

  • @svetavasilieva7841
    @svetavasilieva7841 4 года назад

    Thank you

  • @Andrelas11
    @Andrelas11 4 года назад

    What would be some helpful "game specific" instances you would use each of these loops. I find building on these simple instructions with a practical example helps solidify the information. Many things make sense as a "concept" but implementing the concept is sometimes the most difficult part. Thanks!

    • @andrewshandle
      @andrewshandle 4 года назад +1

      Looping through a player's inventory, looping through enemies and checking if they are alive (the example CM gives for "Contunue"), looping through visible targets, doing an initiative order in a Turn Based game, updating a status flag on a group of items that are on the ground that is on fire, every system in ECS/DOTS with an Entities.ForEach, etc.
      Basically, any time there can be more than one of something.
      FWIW, whenever CM does these types of pure C# mechanical videos, another one follows it which uses what he is talking about, so I imagine his next video will contain loops.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      Yeah I normally try to do that but this is such a basic concept I couldn't really come up with a simple example to explain it. Every example I thought about would take longer to explain the example than to show the loops in action.

  • @ArunKumar-xg4st
    @ArunKumar-xg4st 4 года назад

    Thank you Code Monkey🙂🙂

  • @iUmerFarooq
    @iUmerFarooq 4 года назад

    I'm waiting for next video please upload as quickly as possible?

  • @angelmerchev8933
    @angelmerchev8933 4 года назад

    im new at programming im using visual studio and unity looks so alike

  • @manicmillennial3287
    @manicmillennial3287 4 года назад

    hey CM, ty for this video! What is the "c" after "char" @ 8:27 and what do you mean? I have some trouble hearing you and understanding what you mean by defining "char"

    • @Andrelas11
      @Andrelas11 4 года назад +1

      The "c" is just a typical name assigned to character variables. You could have named it whatever you wanted. So "c" is the variable name, "char" is the variable type. The variable has no value on its own until you start passing in values from the Array with the foreach loop.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Yup it's just the variable name, since it's a "char" I just used "c" but it can be any name you want.
      char is a C# type that represents a character. A string is composed of characters.

  • @GaiaTree
    @GaiaTree 4 года назад +1

    Hi dude. Can u make trade system for mmorpg games? Thanks for helps.

  • @papadross
    @papadross 3 года назад

    I'm new to programming and I'm not sure how to ask but I have some code in a for loop that I don't fully grasp.
    for (int i = 0; i < numArray.Length; i++)
    {
    if(Convert.ToInt32(numArray[i])-i != firstArray)
    {
    cons = false;
    break;
    }
    }
    I get what most of this is doing except for the -i behind (numArray[i]). Any info would be most appreciated.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      I have no idea what numArray and firstArray are meant to represent so I can only tell you what the code is doing
      Convert.ToInt32(numArray[i]) is converting whatever is in numArray[i] into an integer
      Then it's subtracting by 'i' which means it's subtracting more and more and the for loop cycles more and more

    • @papadross
      @papadross 3 года назад

      @@CodeMonkeyUnity That's what I was wondering. Thanks.

  • @mozzastick-05
    @mozzastick-05 4 года назад

    I just get an ad of one of your videos... in your video xd

  • @joseruiz7122
    @joseruiz7122 4 года назад +2

    Your videos are informative but those fast forwards disorient me. I start to catch on but when you fast forward the steps I'm lost.

  • @alexw9309
    @alexw9309 4 года назад

    Does break 2; work in c# for breaking 2 levels or even more? Im from the php world and we have this syntax there. :-)

    • @sagiziv927
      @sagiziv927 4 года назад

      no, unfortunately you can only break the loop you are in, not the outer loops

    • @alexw9309
      @alexw9309 4 года назад

      @@sagiziv927 ok, thx for the answer :-)

  • @David-vz4yk
    @David-vz4yk 4 года назад

    Could you do tutorials for top down shooter game?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      I've done quite a lot of them and you can see some that I used in the Top Down Shooter game I made unitycodemonkey.com/game.php?g=topdownshooter

    • @David-vz4yk
      @David-vz4yk 4 года назад

      Code Monkey thanks!

  • @Deadwindshadow
    @Deadwindshadow 4 года назад

    Whats the difference between while and if statement?
    while (i < 3) {
    do this
    }
    if (i < 3) {
    do this
    }
    I don't see a difference here :/

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      The if code block will only run once and the while code block will run for as long as i is under 3

    • @Deadwindshadow
      @Deadwindshadow 4 года назад

      @@CodeMonkeyUnity Ah, sure! I was thinking of an if statement in the update function haha, but isee how useful while could be used in not update!
      Thank you!!

  • @brockormond4131
    @brockormond4131 4 года назад +8

    Wow, I didn't even know 'continue' was a thing. Embarrassing. I could explain a Recursive loop, but didn't know continue... that's a college education for ya.
    Really, you should do a class for Udemy or something... that'd probably sell well.

  • @СергейПо-х2и
    @СергейПо-х2и 3 года назад

    you mean It not runs second time* (not "second loop") at all ? 0_o 13:36

  • @mr-spoon
    @mr-spoon 4 года назад +2

    Make video about making amionation

    • @spe.z.artist
      @spe.z.artist 4 года назад +1

      dai craft make me a sandwich

    • @mr-spoon
      @mr-spoon 4 года назад

      @@spe.z.artist what about tea?
      Are you writing that while sleeping

    • @spe.z.artist
      @spe.z.artist 4 года назад +1

      dai craft haha tea would be nice. just thought your comment is a bit demanding. I figured I’d give you a taste of your own medicine. Imagine you work hard to make a video andyou post it and the first comment is “do something else” not even a mention of this video.

  • @BigJTT
    @BigJTT 2 года назад

    It's actually painful you don't use the dark theme xD

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад +1

      Sorry but dark theme is painful for me, I can't look at it for more than 30 seconds

    • @BigJTT
      @BigJTT 2 года назад

      @@CodeMonkeyUnity I feel you probably get that comment alot :D

  • @dasmaffin1633
    @dasmaffin1633 4 года назад

    Well if you do i-- in the for loop it is not *infinite* but well yes it will run for a while lol

  • @BarioIDL
    @BarioIDL Год назад

    if alex watched this 3 years ago, yandere simulator would be finished by now
    (jk you can't cure procrastination)

  • @developerakhter______0076
    @developerakhter______0076 4 года назад

    *I found 0.1 Differences in between*
    *C++ && C#*

  • @nullreferenceexception9327
    @nullreferenceexception9327 4 года назад

    I crashed my pc when i did this lol

  • @hj1ax
    @hj1ax 4 года назад

    lol, published 10 seconds ago.

  • @feliwx4087
    @feliwx4087 4 года назад +1

    Oofh I'm quick lol