May 22nd 2025: A large number of spam user accounts have been removed from the wiki. If your account was accidentally deleted during this process, please get in contact to have it restored!

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guide:loop [2021/11/01 21:38] – created ulde3guide:loop [2023/11/27 05:00] (current) – Moved during wiki guide overhaul (Originally written by Levidre) ulde3
Line 1: Line 1:
-======Loops====== +====== Loop ====== 
-=====Introduction=====+<WRAP right> 
 +^  Loop  ^^ 
 +^ Type | Guide | 
 +^ Category | AYA/YAYA | 
 +</WRAP> 
 + 
 +===== Introduction =====
 Loops are a bit of code that is running and restarts from the beginning while a condition is true. They are useful with arrays and allow to check them entirely without knowing what size they have. Loops are a bit of code that is running and restarts from the beginning while a condition is true. They are useful with arrays and allow to check them entirely without knowing what size they have.
  
 Even if the number of time the bit of code is needed is known, loops keep from rewritting the same bit of code if it must be running many times. Even if the number of time the bit of code is needed is known, loops keep from rewritting the same bit of code if it must be running many times.
-=====While loops=====+===== While loops =====
 They are the more simple-looking loops but the most dangerous too. They are the more simple-looking loops but the most dangerous too.
  
Line 20: Line 26:
  
 The problem of this loops comes from the condition. If the condition never become false, the loops wouldn't end. For example 'while 1'. The condition never become false so the loops wouldn't end and run infinitely causing what we usually call a crash. The problem of this loops comes from the condition. If the condition never become false, the loops wouldn't end. For example 'while 1'. The condition never become false so the loops wouldn't end and run infinitely causing what we usually call a crash.
-=====For loops=====+ 
 +===== For loops =====
 They are less dangerous than while loops. They are less dangerous than while loops.
  
Line 26: Line 33:
  
 So for the previous example, we just add a little line otherwise it would be empty : So for the previous example, we just add a little line otherwise it would be empty :
-<code>for i = 0 ; i &lt; 10 ; i = i + 1+<code> 
 +for i = 0 ; i 10 ; i = i + 1
 { {
      "A value of i during a loop : %(i)\n"      "A value of i during a loop : %(i)\n"
-}</code>+} 
 +</code> 
 So here the initial situation would be 'i = 0', that's the first thing that would run before the loop in itself. At each loop, "A value of i during a loop : %(i)" would be ran and 'i = i + 1' would be done and the loops would run until i is equal or superior to 10. When we put a line of dialogue in loops, only one randomly would be displayed as all the lines of dialogues coming from the loops were next the one to an other despite all the -- that could be inside. So here the initial situation would be 'i = 0', that's the first thing that would run before the loop in itself. At each loop, "A value of i during a loop : %(i)" would be ran and 'i = i + 1' would be done and the loops would run until i is equal or superior to 10. When we put a line of dialogue in loops, only one randomly would be displayed as all the lines of dialogues coming from the loops were next the one to an other despite all the -- that could be inside.
  
 There are few chance of making infinite loops with for loops since the ending condition and what change from one loop to an other are at the same place. There are few chance of making infinite loops with for loops since the ending condition and what change from one loop to an other are at the same place.
-=====Foreach loops=====+ 
 +===== Foreach loops =====
 They are the more useful for manipulating arrays. In fact, they work only for lists or arrays. Their structure is 'foreach <array> ; <line>' They are the more useful for manipulating arrays. In fact, they work only for lists or arrays. Their structure is 'foreach <array> ; <line>'
 Here is an example : Here is an example :
-<code>array = IARRAY+ 
 +<code> 
 +array = IARRAY
 array[0] = 1 array[0] = 1
 array[1] = 2 array[1] = 2
Line 45: Line 58:
      i = i + element      i = i + element
 } }
-"i value : %(i)"</code>+"i value : %(i)" 
 +</code>
  
 Here the code would be ran for each element in the array. So when the code starts, an array called 'array' would be created with three elements : 1, 2 and 3. The variable 'element' would take the value of the first element of the array, so 1. Then it would be added to i and 'element' would become the next element of the array, 2. The loops would end when the array ends and the value of i would be 6. Here the code would be ran for each element in the array. So when the code starts, an array called 'array' would be created with three elements : 1, 2 and 3. The variable 'element' would take the value of the first element of the array, so 1. Then it would be added to i and 'element' would become the next element of the array, 2. The loops would end when the array ends and the value of i would be 6.
 +
 +{{tag>AYA Guide YAYA}}