Detected Country () : English
Looping - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

Looping

Level Newbie [2017-11-04 23:08:12] - by hillzx
Featured Image

Sometimes there is a moment when we have to repeat the instruction several times where the instruction will stop at certain conditions. There are 2 ways that can be used to perform the process of repetition :

  1. FOR
  2. DO WHILE

 

FOR :

 

for(expression1; expression2; expression3)
{
   operator;
}

 

DO WHILE :

 

do
   operator;
while(expression);

 

Let's take a case example, for example we want to write numbers from 1 to 10 using FOR then the way is:

 

for(x=1;x<=10; x++)
{
Print("Angka " + x);
}

-----------------------------------------------
// Hasilnya :
Angka 1
Angka 2
Angka 3
Angka 4
Angka 5
Angka 6
Angka 7
Angka 8
Angka 9
Angka 10

The explanation: x is the variable to hold the number, when the loop starts, x is set with the number 1, do the loop for x less than or equal to 10, each process is repeated while incrementing value 1 into the value of x.

If we use the DO WHILE formula, it will look like this:

 

int x = 1;

do
{
Print("Angka " + x);
x++; // Jika baris ini tidak diisi maka perulangan akan tiada akhir
}
while(x<=10);

-----------------------------------------------
// Hasilnya :
Angka 1
Angka 2
Angka 3
Angka 4
Angka 5
Angka 6
Angka 7
Angka 8
Angka 9
Angka 10

 


Tips, if you want to get out of a sudden iteration, use break; syntax, in the middle of the loop.

 

for (x=1;x<10;x++){

   if (x==5){
      break;
   } else {
      Print("Angka " + x);
   }

}

-----------------------------------------------
// Hasilnya :
Angka 1
Angka 2
Angka 3
Angka 4

Good luck, do not forget to share and follow our official facebook and youtube, happy trading!



Next Article

Featured Image

Logic




- Provided by HiLLzSoft Studio -