Algorithm And Programming-Repetition

ALGORITHM AND PROGRAMMING
PROGRAM CONTROL-REPETITION
(C Programming Language)

What is Repetition?
Basically,a repetition is one or more programs that are repeated for a number of times which number can be defined first or defined later according to the condition. 
So,there are 3 ways to do looping in C programming,which is for,while,dowhile,and for is the one that is predefined,meanwhile while's and dowhile's number is defined later

Forever in C can be used like this:
while (true)
{
    printf("hello, world\n");
}

Or...
we can also use do while
What is the difference?While vs do while?
well for do while,we must "do" it first before checking the condition,so the loop will be executed at least once,meanwhile for while,we must check the condition before executing it even once.
The while keyword means that the loop will run as long as the Boolean expression inside the parentheses is true. And since true will always be true, the loop will run forever.
To repeat something a certain number of times, we can use this

for (int i = 0; i < 50; i++)
{
    printf("hello, world\n");
}

This is a little harder to figure out, but we can go through step by step,for is another keyword in C that indicates a loop.
  • int i = 0 is an initialization of a variable, which means that we created a variable with the name i, of the type int, or integer, and set its initial value to 0. In C, each variable has a type of value.
  • Then i < 50 is the Boolean expression that the for loop checks, to determine if it will continue or not. Since this condition is true, the for loop will run the printf line. And since we started i at 0, stopping before i reaches 50 will mean this runs exactly 50 times, as we intended.
  • Finally, i++ is an expression in C that adds 1 to the value of i. Then, the for loop will check i < 50, and repeat this process until the Boolean expression is no longer true.
Luis Indracahya
2201758934
luis.indracahya@binus.ac.id
skyconnectiva.com

Komentar

Postingan populer dari blog ini

FUNCTIONS(DATA AND VOID),RECURSION

Algorithm and programming-review