Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

an easy example for FOR Loops?

7 like 0 dislike
an easy example for FOR Loops?
asked 1 year ago by webmaster (25,380 points)

2 Answers

3 like 0 dislike
 
Best answer
A FOR loop is very similar to a WHILE loop in that it continues to process a block of code until a statement becomes false, however everything is defined in a single line. The basic structure for a FOR loop is:

for ( start; conditional; increment) { code to execute; }

Let's go back to our first example using the WHILE loop, where we printed out the numbers 1 through 10 and do the same thing using a FOR loop.

 <?php
 for ($num=1; $num <= 10; $num++ )
     {
     print $num . " ";
     }
 ?>

The FOR loop can also be used in conjunction with a conditional, just like we did with the WHILE loop:

 <?php
 for ($num=1; $num <= 10; $num++ )
     {
     if ($num < 5)
         {
         print $num . " is less than 5 <br>";
         }
     else
         {
         print $num . " is not less than 5 <br>";
         }   
     }
 ?>
answered 1 year ago by monirulislam (51,620 points)
1 like 0 dislike
How do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression?

A 'for loop' is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.

For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.
answered 1 year ago by marck_don (191,010 points)

Related questions

7 like 0 dislike
2 answers
8 like 0 dislike
1 answer
11 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked 7 months ago by Rosena Doggers (5,480 points)
4 like 0 dislike
1 answer
asked 1 year ago by sr_webmaster (21,000 points)