Recurrence WITH FOR

             Unlike the WHILE, with FOR we can determine with certainty the number of iteration. In Sketch 2.3, respectively WHILE loop will perform 10 times by reducing the number 1000 with the 100 gradually. If using the FOR, we can do it more simple looping. The basic format of FOR is:
 for(statemen; condition; statemen){ 

         
                 The first statement contains the initial conditions, usually initialize a variable or data (eg, a = 0). While the latter statement is the change that will occur in the variable on the initial statement (eg a = a + 1). While the condition is a condition where the loop will happen, when the conditions are not appropriate, then the iteration will stop. for more details about the FOR, the following simple example:

// www.ioisalman.com  
// Pin 8 for LED 
const int pinLED = 8;  
void setup() {     
// pin LED as output     
pinMode(pinLED, OUTPUT); }  
// start time delay 1000 | 1 second
int timeDelay = 3000;  
void loop() {   
// From the loop as much as 10 times 1 Up to 10   
for(int i=1; i<=10; i++){     
// LED life and death with a duration of 500 milliseconds     
digitalWrite(pinLED, HIGH);     
delay(500);     
digitalWrite(pinLED, LOW);    
delay(500);   }   
// still for 3 seconds
  delay(timeDelay); }  

             When the program is uploaded, then the LED will flash with a duration of ½ seconds 10 times, then the LED will live (die) for 3 seconds (note, the value timeDelay replaced 3000), and continue to blink again. Examine the line 14:
 for(int i=1; i<=10; i++){ 

              At line 18 shows FOR the conditions of the initial value i = 1, i will be continuously added with 1 (i ++) for looping over i <= 10. Put simply, the value of i will change from 1, 2, 3, 4, ... up to 10 , An important part to understand is i ++, i ++ together with

i = i + 1;

           Besides i ++, there is also ++ i, i--, and --i. I-- writing together with i ++, i-- just means i = i-1, or i will be reduced by 1 continuously for recurrence.
++ Placement in front or behind i berarri that: if it is on striped ++, then the addition is performed after the code block for the run. But if it is in front ++, then the process will be carried out before the addition of the process was started. Here's an example:

 int i = 1; int j = 3 * (i++);  // j = 3 because 3 * 1  
// i = 2  
int j = 3 * (++i);  // j = 9 because 3 * 3 
// i been coupled with one another (++ i) before the line 6 is executed

           Note, in row 6, j worth 3 for 3 multiplied by the value of i = 1. After the 4th line, then the value of i will turn into two because there are statements ++ i (i = i + 1).

          While on row 10, j worth 9 (not 6) because i was not worth 2 again, but i worth 3 because before the line is executed i first added by 1 (++ 1). So i do not do after the addition of the execution line 10 the line, but before executing the line 10th.

Subscribe to receive free email updates:

0 Response to "Recurrence WITH FOR"

Post a Comment