For

From SDU
Jump to: navigation, search
To make corrections or additions to this article, select the edit tab above.
To discuss or ask questions about this article, select the discussion tab above.

Overview

A "for" loop performs a set of commands for each iteration of the loop.

Usage

for (starting number;while;increment)

You do not have to specify all (or any) of the three values within the, although not doing so may result in an infinite loop. You must have code within the loop itself to exit if you choose to exclude these values. Also, if you do not specify a value you still must use the semi-colon(;) as a separator.

Examples

  • For loop that increments a variable by 1 on each pass
int i,count;
for (i=0,count=0;i<=4;i++) {
     count++;
}

This will increment the count var to 5.

  • For loop which logs all the indexes of the msg array which the send_wait function returns
int i;
send_wait(0, top_object(), "call_attr", "cnt", "get_groups_by_persid", "cnt:776B094702EABB4B87F04A139E72D44B");
for (i=0;i<msg_length();i++) {
     logf (SIGNIFICANT, "Msg index #%s: %s", i,msg[i]);
}
  • For loop which does not specify all three values
int i;
i=5;
for (i;;) {
     if (i>-1) {--i; } else {return;}
}

This will exit i reaches a value of -1