Algorithm & Programming – 4th Meeting

Program Control – Repetition:

–Repetition Definition

  • One or more instruction repeated for certain amount of time
  • Number of repetition can be predefined (hard-coded in program) or defined later at run time
  • Repetition/looping operation:
  • –for
  • –while
  • –do-while

–For

  • Syntax:

for(exp1; exp2; exp3) statement;

or:

for(exp1; exp2; exp3){

  statement1;

  statement2;

  …….

}

exp1 : initialization

exp2 : conditional

exp3 : increment or decrement

exp1, exp2 and exp3 are optional

  • exp1 and exp3 can consist of several expression separated with comma
  • Example:

void reverse(char ss[])

{

   int c,i,j;

   for(i=0, j=strlen(ss)-1; i<j; i++, j–){

       c=ss[i];

       ss[i]=ss[j];

       ss[j]=c;

   }

}

  • Infinite Loop

Loop with no stop condition can use “for-loop” by removing all parameters (exp1, exp2, exp3). To end the loop use break.

  • Nested Loop

Loop in a loop. The repetition operation will start from the inner side loop.

–While

while (exp) statements;

  • exp is Boolean expression. It will result in true (not zero) or false (equal to zero).
  • Statement will be executed while the exp is not equal to zero.
  • exp evaluation is done before the statements executed.

–Do-While

–Repetition Operation

–Break vs Continue

About adrianjsj

18 | Binusian 2019 | Game Application Technology | Devoted to Jesus | No Other Name | Dota 2 Semi-Pro player | From 94 to 62
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *