[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Difference between for and while loop



On Fre, 18 Apr 2003 at 16:40 (+0300), Aryan Ameri wrote:
> On Friday 18 April 2003 13:46, Frank Gevaerts wrote:
> > On Fri, Apr 18, 2003 at 12:58:43PM +0300, Aryan Ameri wrote:
[...]
> > > Well, the way it has been explained to me, you can also use the FOR
> > > loop in non defined loops, and you can also set a FOR loop, in a
> > > way that the minimum execution time might be less than 1, say 0.
> >
> > What language are we talking about ?
> 
> I was talking about C. I guess that was the OP's question.

But thers's no way to prevent the first execution of the loop body.
The stop condition (even if it's missing) is checked at the _end_ of
first execution. You can leave the body empty or use a *break* to
get out of the loop, but that has nothing to do with the definition
of the for loop.

[...]
> Newbish question, what's recursion ?

You can understand recursion only if you understood recursion ;-)

A function calls itself. For Example (Perl, only the skeleton):

<recursion.pl>
#! /usr/bin/perl

use strict;

# define function
sub read_tree {
  my $dir = shift;

  print "processing directory ", $dir, "\n";

  # read the content of the directory into an array
  opendir (DIR, $dir) or die "error $!\n";
  my @content = readdir DIR;
  closedir DIR;

  # walk through the directory entries
  foreach (@content) {

    # ignore the . and .. entries
    next if /^\.{1,2}$/;

    # print the entry
    print $_, "\n";

    # subdirectory found: recursion
    my $next = $dir . "/" . $_;
    read_tree ($next) if -d $next;

  }

}

# main: start recursion
read_tree ".";

exit 0;
</recursion.pl>

The script starts at the current directory and walks recursivly
through the tree by calling the function read_tree for every found
subdirectory again. The recursion stopps if no more subdirectories
are found.

Another example in C: converting a binary notation string into a
decimal number (without input and overflow checking):

<bin2dec.c>
#include <stdio.h>
#include <string.h>

/* recursive function */
int calc (char *str, int base, int num) {
  /* the last character in string */
  char c = *(str + strlen (str) - 1);

  /* add the value base * 0 or base * 1 to result */
  num += base * (int)(c - '0');

  /* double base for next round */
  base *= 2;

  /* set the last character to 0 = end of string, means: cut string
   * */
  *(str + strlen (str) - 1) = 0;

  /* string not empty: recursion */
  if (*str) num = calc (str, base, num);

  /* return result */
  return (num);
}

/* main */
int main (int argc, char **argv) {
  char *str = NULL;
  int num = 0;

  /* exit if no argument given */
  if (argc < 2) return (1);

  /* duplicate argument */
  str = strdup (argv[1]);

  /* start recursion */
  num = calc (str, 1, 0);

  /* result */
  printf ("Result for %s: %d\n", argv[1], num);

  /* freeing memory */
  free (str);
  return (0);
}
</bin2dec.c>

The function calc is called recursivly for every character in the
argument (beginning at the end). The base (2^n) is doubled for every
new call. The recursion stopps if all characters are processed.

Jan



Reply to: