ALGORITHM & PROGRAMMING
Program Control: Repetition (Summary)
Nama :Bismo Asyura Widianto
NIM :2201785595
bismo.widianto@binus.ac.id
NIM :2201785595
bismo.widianto@binus.ac.id
What is Repetition in C?
- One or more instructions used to repeat for a certain amount of time
- This instruction automatically repeats itself until .a condition is satisfied.
- There are 3 of repetition/looping in C programming:
- For
- While
- Do-While
- For
- While
- Do-While
Repetition: For
-Syntax:
for([initial] ; [condition] ; [increment or decrement]);
initial and condition can consist of a few expressions separated by a comma.
-Flowchart:
-Example:
(program to print out numbers from 0-100
*************************
#include <stdio.h>
int main()
{
int x;
for(x=0;x<=100;x++)
printf("%d",&x);
return=0
}
*************************
Repetition: While
-Syntax:
while (exp)--> Boolean expression (true or false,|1 or 0)
{
statement1;--> Will be executed while the expression is not equal to zero
statement2;
..
..
}
-Flowchart:
-Example:
*************************
#include <stdio.h>
void main()
{
int x=1;
while (x<=10
{printf ("%d\n",x); x++; }
}
*************************
initial and condition can consist of a few expressions separated by a comma.
-Flowchart:
-Example:
(program to print out numbers from 0-100
*************************
#include <stdio.h>
int main()
{
int x;
for(x=0;x<=100;x++)
printf("%d",&x);
return=0
}
*************************
Repetition: While
-Syntax:
while (exp)--> Boolean expression (true or false,|1 or 0)
{
statement1;--> Will be executed while the expression is not equal to zero
statement2;
..
..
}
-Flowchart:
-Example:
*************************
#include <stdio.h>
void main()
{
int x=1;
while (x<=10
{printf ("%d\n",x); x++; }
}
*************************
Repetition: Do-While
-Syntax:
do
{[statements];
}while (exp);--> expression will continue to execute while it is true and will only be done after executing the statement mentioned previously
-Flowchart
-Example:
*************************
int counter = 0
do {
printf("%d", counter);
++counter;
}
while (counter<=0);
do {
printf("%d", counter);
++counter;
}
while (counter<=0);
*************************
Break vs Continue
Break:
- Skip the previous statement inside a repetition and continue normally to the next loop