Algorithm Part 9 Program Control Repetiton

Algorithm
Part 9
Program Control Repetition

Repetition means One or more instructions that runs in one time.

Repetition can be divided 3 types:
1. For
2. While
3. Do-While

1.For
For statement can be used if the user initalized first the variable, and then the condition and last is condition after the looping done,seperated by semi-colon (;)
the format is like this:
for(exp1;exp2;exp3){
         [statements]
         }

Example:
for(int i=0;i<5;i++){
            [statements]
          }

means of int i=0 is the initialization that i have the value of 0
after that i<5 means 
while i<5 it will looping until condition is meet (until n>=5)
and the last is condition of the end looping
its mean i++
thats mean i+1 everytime it looping
so the program will looping 5 times.

2.While
While condition can be used instantly the condition,do-while is slightly different than while.
The format is:
While(exp){
         statements
        }
example:
int x = 0;
     while(x != 5){
       printf("Hello World!");
      x++;
      }
that means until x = 5 the program will be stop looping
because the last we use x++ so it will looping 5 times

3.Do-While
Do-while is same but slightly different against while
the different is that do-while is looping again 1 times and after that it will be checking
it means that do-while is looping first and after that it will be checking the condition
the format is:

do{
       [statements]
       [variable inc/dec]
      }
while(exp);

Example:
int x = 0;
do{
     printf("Hello World!");
     x++;
     }
while x<5;

so it will running 5+1 times = 6 times.

Comments

Popular posts from this blog

Human Computer Interaction Redesign UI(User Interface) Cinepolis Indonesia