Tuesday, July 29, 2014

For and While loops

-Array elements access:
in our article about arrays here Arrays we talked that its a special type of object and its a variable that can store more than one value so the best way is to write something that can call a code more than one time and for different values.
-For loops:

  • If you want to execute the same code over and over again and with different values you can do it like this: let see this array elements


what if our array has 1000 elements sure we wont do it like this so lets use our for loops
-For loops syntax:

  • for(initialize a counter;stop condition;increment counter)
-initializing a counter:

  • its the counter of our loop where it should start since the counter is a variable so it can be zero or 10 it can be any NUMBER
  • stop condition: we don't want our loop to get into an infinite loop because it wont stop executing and it might cause a system freezing if out program was big "for java,c++,c# programs"
  • increment counter: since we want to access different values in arrays for example this is the way to go through the elements indexes we can go one by one or maybe ten by ten 
  • lets see this example which will make everything clear:

-lets explain this code:
  • "int i=0": our counter is the variable is "i" which start with the value of zero
  • "ilt;4": the stop condition is when the "i" is still smaller then 4 and you can check this article about comparisons Comparison and logic operations so while the "i" is still smaller the code "x[i]+1" will keep executing
  • "i=i+1" it'll increase the "i" by 1 so each loop "i" will be added to 1 so let see this :
          loop 1:
          i=0;
          x[0]+1;

          loop 2:
          i=1;
          x[1]+2;
          loop 3:
          i=2;
          x[2]+1;
          loop 4:
          i=3;
          x[3]+1
          loop 4:
          i=4;
          it'll break the for loop since it doesn't satisfy the loop condition that the i should be smaller than 4 so              every time the loop finish it'll check if the another loop can run or not by checking the loop condition  
  • here we can access all the array elements since we can get to the i=3 and its the last index of our array
  • we can change the increment so it can be i=i+2 so it'll go through every two elements 
-While loops:
  • it has the same job of the For loops but in different syntax and in different way 
-While loops syntax:

  • while(condition){........code.......}
  • as you can see its just in a different way but its the same main idea
  • initialize counter since we only have a place for the stop condition we should declare the counter out of the while loop
  • counter increment it should be inside the while body so every time the code execute it increase the counter by 1 for example and the go again the the while condition and check it and decide if the code will run again or not according the the counter value if its valid or not.
  • let see this example:


-BE AWARE:
  • you should be aware about the stop condition because for example if you have an array of 100 element and your stop condition was "ilt;120" the for loop will go though 20 elements which aren't defined and will get a very strange a random variables and maybe will cause an error and your code will stop executing .
  • Note: it has a lot of explanation but this is the easiest for beginners :).



No comments:

Post a Comment