Senin, 22 Oktober 2018

Repetition

Hello Everyone.... Welcome to My Blog!!!
In My Blog I am going to write about Coding especially for C !!!
And Today I am going to discuss about Repetition in Coding.

So what's Repetition??

Repetition in Coding is know as Looping, while Looping is a sequence of statements which is specified once but which may be carried out several times in succession. The number of looping can be predefined (hard - coded in program) or defined later at run time.

And there are three in C that are designed for Looping :
1. The for statement
2. The while statement
3. The do-while statement

Let's Discuss one by one!!

The for Statement

The general form / syntax of the for statement is like this:

for (expression1; expression2; expression3) {
   statement1;
   statement2;


}


exp1 :  initialization
exp2 :  conditional
exp3 :  increment or decrement
exp1, exp2 and exp3 are optional


The for statement first evaluates expression1, which usually initializes one or more variables. In other words, expression1 is only evaluated once when the for statement is first encountered.

The second expression, expression2, is the conditional part that is evaluated right after the evaluation of expression1 and then is evaluated after each successful looping by the for statement. If expression2 returns a nonzero value, the statements within the braces are executed. Usually, the nonzero value is 1. If expression2 returns 0, the looping is stopped and the execution of the for statement is finished.

The third expression in the for statement, expression3, is not evaluated when the for statement is first encountered. However, expression3 is evaluated after each looping and before the statement goes back to test expression2 again.

And here is the example in C :

#include <stdio.h>
int main (){

    int j;
    for (j = 1; j < 10; j++){
       printf ("Hello");

       printf("\n")
    }

    return(0);
}

So in that Coding we can read that " j " is a Integer, and inside the for in expression one it means Integer " j " is starting from number 1. Then the second expression means that the Maximum Integer for " j " is no more than 10. After that the third expression means that " j " is going to be add by 1 number until it got the maximum of expression 2. And after the number is maximum, the program is going to execute 9 word "Hello" (without quotation) because it is commanded to write Hello.

So if I am going to make a sentence of that coding is "For j is same with one and j is lower than 10, then I am going to add more number until it is 9, after that I finally could write word "Hello" 9 times.

The Flow Chart of For is like this:


And here is some program to print out number 10 to 1


While the program to print out 1 to 10 is like this :
There is 2 type of Loop:
1. Infinite Loop
2. Nested Loop

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

Nested Loop is Loop in a loop, the repetition operation will start from the inner side loop.

Example:
The While Statement

What is the while repetition statement??
The while statement is also used for looping. Unlike the situation with the for statement, there is only one expression field in the while statement.

The general form / syntax is like this :

while (expression) {
   statement1;
   statement2;

}


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.

Here expression is the field of the expression in the while statement. The expression is evaluated first. If the expression returns a nonzero value (normally 1), the looping continues; that is, the statements inside the statement block are executed. After the execution, the expression is evaluated again. The statements are then executed one more time if the expression still returns nonzero value. The process is repeated over and over until the expression returns 0.

And here is the Example in C :

#include <stdio.h>
int counter = 1;
while ( counter <= 10 ) {
     printf( "%d\n", counter );
     ++counter;

}
So in that coding we can read that "Integer counter is starting from 1, While integer counter is lower than 10, write "number till 10" / add more number until 10. Example : ((+1)*9).

So yeah... it's a little bit different with For and the difference is on the ++ placement, while "for statement" ++ placement is after "Lower than" Command. But for "while statement" the ++ command is after the printf.

The Flow Chart While is like this : 

The Do-While Statement

What is Do-While Statement??

Do - While, which puts the expressions at the bottom of the loop. In this way, the statements controlled by the do-while statement are executed at least once before the expression is tested. Note that statements in a for or while loop are not executed at all if the condition expression does not hold in the for or while statement.

The general form / syntax is like this :

do {
   statement1;
   statement2;

} while (expression);

Keep executing while exp is true
exp evaluation done after executing the statement(s)
Here expression is the field for the expression that is evaluated in
order to determine whether the statements inside the statement block
are to be executed one more time. If the expression returns a
nonzero value, the do-while loop continues; otherwise,the looping
stops.
And here is the Example in C :
#include <stdio.h>
int counter=0;
      do {
            printf( "%d  ", counter );

  ++counter;
} while (counter <= 10);


So in that Coding we can read that "Integer counter is start from 0, do write number while adding more number one by one and while counter is less than 10").
Yeah it seems like it's not really different from For and While, But in the Do-While ends with a semicolon, which is an important distinction from the if and while statements. And in Do-While we are writing the word first before counting the maximum word can be placed.

The Flow Chart Do - While is like this :

The Repetition Operation
In while operation, statement block of statements may never be executed at all if exp value is false
In do-while on the other hand statement block of statements will be executed min once
To end the repetition, can be done through several ways:
Sentinel
Question, should the repetition continue?


As sentinel, used 0 for width or height

Break and Continue Operation
Break is to force finish the loop (for, while, do-while), and to end the switch operation.

Continue skip all the rest of statements (subsequent to the skip statement) inside a repetition, and continue normally to the next loop.
And so the Summary for Repetition is a condition which is one or more instruction repeated for certain amount of time.

So yeah... That's all about Repetition / Looping in Coding especially C++, how was it?? you understand right??

Thank for reading this Blog and You are Free to Comment on the comment section below.
Good Bye!!!




References:
Paul Deitel& Harvey Deitel. (2016). C how to program : with an introduction to C++. 08. Pearson  Education. Hoboken. ISBN: 9780133976892. Chapter 3 & 4
Doing the Same Thing Over and Over: http://aelinik.free.fr/c/ch07.htm 

Array and Pointer


Hello everyone welcome to my Blog !!!
This is the new blog that consist every Coding tutorial !!!
It's the same with my JPrepetition blog, but that one consist only repetition tutorial.

And now in this blog I will post every coding tutorial that I could....
So keep reading okay !!!

So now our theme is Array and Pointer....

What is Array ??
Array is data that is saved in a certain structures to be accesed as a group or individually. Some variables saved using the same name distinguish by their index. And array is a collection of data that holds fixed number values of same types

Array Charateristic ??
1. Array is homogeneous, it means all elements have similar data type.
2. Array s a random access type, it means that each element can be reached individually, does not have to be sequential. 

The Syntax of Array 
- type array_value [value_dim]; or dataType arrayName[arraySize];

The Example of Array Coding 
int arry[15];
   for ( int a = 0; a <15; a++) arry[a] = 0;

And in here the meaning is integer arry with size 15 is, meaning it can hold 100 integer values.
And integer a will loop or fill the arry until it's maximum (15).  

The elements of Array :
Suppose you declared an array arry as above. The first element arry[0], second element arry[1], third element arry[2], and so on until 15.

It's possible to initialize an array during declaration, For example :
int arry[15] = {1, 3, 5, -7} or int arry[] = {2, 3, 7, -1}
it means that :
- arry[0] is equal to 1,
- arry[1] is equal to 3,
- arry[2] is equal to 5,
- and so on until 15.

But, you couldn't initialize these kind or array
  int arry[15] = {0, 0, 0, 0}
Why?
Because the integer arry only choose one data that is same.

In array you could even make an Assigning Value for Example :
- A[1] = 25; B[3] = 20
- C[6] = A[1] - B[3]
That means C[6] is 25 - 20 = 5.

So... That's all about Array.... Then how about Pointer ??



What is Pointer ??
Pointer is a variable that store the address of another variable or pointer is used to access the memory and manipulate the address.

The Syntax of Pointer 
- <type> *ptr_name;
- pointer uses mostly 2 type of operators which is : *(content of), &(address of)

The Example to use Pointer
- int *p; or int* p;
It means the variable p is the holder of the address and the asterisk is the content of
So that means " pointer p is a content of int " (the address name).

Initialize Pointer in C++

int point = 10;
int *pointr;
pointr = &point;
printf("%d", *pointr);

The output will be like this 

10

How ??
To make it sentence = " There is integer point and pointr, we give Pointer to integer pointr, And then we will read that pointr is address of point. "

Then we will print the result of *pointr going to point as a address.

The Simple example : pointr > point > bring up point value.


So... That's all about Pointer... 
Yeah I know that's more simple than Array but I hope that you (the one who reads) understand about 
Array & Pointer !!!

Thanks for Reading this Blog and Learn from this, I hope you like it  <3<3




Reference : 
- https://www.programiz.com/cpp-programming/pointers
- https://www.programiz.com/cpp-programming/arrays
- Paul Deitel & Harvey Deitel. (2016). C how to program : with an introduction to C++. 08. Pearson  Education. Hoboken. ISBN: 9780133976892. Chapter 6 & 7