Termux Work & Programming Language

Termux Work & Programming Language
Learn C++ #08: Understanding the Recurrence Block in C++



What will you do when you are told to print a sentence over and over again?


For example:


Please show the sentence "C++ Programming Tutorial!" 10x to the screen!


Maybe you'll use cout 10 times like this:


include


using namespace std;


int main(){


     cout << "C++ Programming Tutorial!" << endl;


     cout << "C++ Programming Tutorial!" << endl;


    cout << "C++ Programming Tutorial!" << endl;


    cout << "C++ Programming Tutorial!" << endl;


    cout << "C++ Programming Tutorial!" << endl;


    cout << "C++ Programming Tutorial!" << endl;


    cout << "C++ Programming Tutorial!" << endl;


   cout << "C++ Programming Tutorial!" << endl;


   cout << "C++ Programming Tutorial!" << endl;


   cout << "C++ Programming Tutorial!" << endl;


return 0;


Result:


 


Can it be like this?


Yeah, you can.


But…


What if you want to show it 1000 times.


Must be tired donk ticking it.


Therefore, we must use looping.


 


 


It helps us execute code over and over again, no matter what we want.


There are four types of looping in C.


In general, it is divided into two groups.


Counted loop and uncounted loop.


Difference:


Counted Loop is a clear repetition and of course a lot of repetition.


While Uncounted Loop, is a repetition that is not clear how many times he must repeat.


The loops included in the Counted Loop:


Repetition For


Loop included in Uncounted Loop:


While loop


Do/While Repeatability


Let's discuss them one by one…


1. The Loop Block For


A for loop is a loop that is included in the couted loop, because it is clear how many times it will repeat.


 


 


Flowchart looping for


 


The code is like this:


for(int i = 0; i < 10; i++){


printf("Repetition %i\n", i);


}What needs to be considered is the condition that is in brackets after the word for.


This condition will determine:


Count will start from 0 (i = 0);


How many up to? Up to i < 10;


Then in each looping i will increase +1 (i++).


Variable i on looping for serves to store the value of the count.


So every repetition done value i will always increase by one. Because we specify it in the i++ section.


Example of a loop program with for:


include


using namespace std;


int main(){


for(int i = 0; i < 10; i++){


printf("Repetition %i\n", i);


}


return 0;


}This is the output:


 


 


 


 


 


Does the variable name always have to be i?


No. gabe.


We can also use another name.


For example:


include


using namespace std;


int main(){


for(int counter = 0; counter < 10; counter += 2){


printf("Repetition %i\n", counter);


}


return 0;


}In that example, we do the looping starting from zero 0. Then in each looping the value of the router variable will be added 2 (counter+=2).


Result:


 


 


 


 


 


What if the counter repetition starts from the larger akanga to the smallest?


This is usually done when we want to count down…


Easy way.


We just fill the counter value with the greatest value.


For example, we will start counting from 10 to 0.


Then the counter value, we fill in initially with 10.


Then in the comparison conditions, we give counter > 0. This means that the repetition will be done as long as the counter value is greater than 0.


Then we subtract (-1) the counter value in each looping (counter-).


for(int counter = 10; counter > 0; counter--){


printf("Repetition %i\n", counter);


}The result:


 


 


 


 


 


Why not go to zero (0)?


Due to the conditions we provide counter > 0. If the counter is worth 0, then this condition will be false.


Unless we use the same larger operator with (>=), then if the counter is worth 0, the condition will be true.


2. While looping on C++


While looping can also be a loop that is counted loop by giving a counter inside.


The shape of the flow chart is the same as the flow chart for.


 


 


Flowchart looping while


 


To understand this repetition…


…Let's look at an example:


include


using namespace std;


int main(){


  char repeat = 'y';


  int counter = 0;


   // while looping


      while(repeat == 'y'){


      printf("Do you want to repeat?\n");


      printf("Answer (y/t): ");


      cin >> repeat;


     // increment counters


     counter++;


 }


    printf("\n\n--------\n");


    printf("Repetition Done!\n");


   printf("You repeat %i times.\n", counter);


return 0;


Result:


Look at the code block while:


// while looping


while(repeat == 'y'){


     printf("Do you want to repeat?\n");


    printf("Answer (y/t): ");


    cin >> repeat;


    // increment counters


    counter++;


}


There. The repetition will occur during the repeat variable brenilai y.


Then we use the scanf() function to retrieve the input.


As long as we answer y at the input, then the repetition will continue.


But if we answer the others, then the repetition will be stopped.


Because the condition of its looping is not fulfilled.


3. Do/While looping on C++


Do/while repetition is the same as while repetition.


Difference:


Do/while repetition will repeat 1 time first, then check the conditions in while brackets.


Do/while loop flow chart:


 


 


 


 


 


The code is like this:


do {'s


// blocks of code to be repeated


} while ();So the difference:


Do/while repetition will check the condition behind (after repeating), while while will check the condition in front or at the beginning (before repeating).


Let's look at an example:


include


using namespace std;


 


int main(){


   char repeat = 'y';


i nt counter = 0;


do {'s


     printf("Do you want to repeat?\n");


    printf("Answer (y/t): ");


    cin >> repeat;


   // increment counters


       counter++;


} while(repeat == 'y');


    printf("\n\n--------\n");


    printf("Repetition Done!\n");


    printf("You repeat %i times.\n", counter);


    return 0;


}


The example is the same as the example in while repetition.


During the first iteration, try to cancel the iteration by answering t.


The result: then:


 


 


 


 


 


4. Nesting Loop (Nested Loop)


Inside the looping block, we can also make looping.


This is called a nested loop or looping nest or looping in the adventure.


Let's look at an example:


include


using namespace std;


int main(){


for(int i = 0; i < 10; i++){


for(int j = 0; j < 10; j++){


printf("Repetition to (%d, %d)\n", i, j);


}


}


return 0;


}The result:


 


 


 


 


 


In this loop, we use two loops for.


The first repetition uses the variable i as a counter, while the second exchange uses the variable j as a counter.


What Next?


The point is that repetition is used to do repetition. This is the basic thing that every programmer should understand.


Because in the future, we will use it a lot in making programs with C++.


Keep in mind, there are two types of loops: Counted loops and uncounted loops.


Happy learning…