SA-MP Forums [Tutorial] An in-depth look at looping structures
 New Account Members List Search Today's Posts Mark Forums Read

 23/12/2011, 08:24 PM #1 Gh05t_ Big Clucker     Join Date: Dec 2011 Posts: 192 Reputation: 140 An in-depth look at looping structures An in-depth look at looping structures. ContentsIntroduction What is a loop? Compound statements Types of looping instructionsfor loop While loop do-While loop *foreach Jump statementsbreak statement continue statement goto statement Conclusion Introduction The following tutorial will explain in brief detail about iteration type loops. What is a loop? You can imagine a loop to be a circle in which continues. In programming, a loop is an iteration structure in which repeats a statement. This include both singular type and compound statements. Compound Statements In programming, a statement is an instruction. A group of statements, is called a compound statement. It is a sequence of other statements enclosed within braces. This allows programmers to manipulate collective data easily to perform operations and tasks. In C++, the term may be refereed to as a "code block". pawn Code: `function1()    print("Test"); // Singular-lined statement    function2(){    print("Test");    print("Test"); // Compound statement}main(){    function1();    function2();}` A singular-lined statement requires no braces, as it's task is to perform only one operation. pawn Code: `function()    printf("Function1");    printf("Another Function1");` An example of how data may be interpreted incorrectly, is by taking a look at the example above. A semicolon would generally state the end of a statement, though, by having one following the other, the compiler reads the data as shown below: pawn Code: `function()printf("function1");printf("Another function1");` This would generate a compiler error due to the fact of having a statement expressed globally which is simply improper and incorrect in programming. For tips on code optimization, see here. Types of looping instructions A looping instruction may be determined or can be categorized by whether or not a statement may be repeated (as stated above). There are the three main types of loops we will be looking at today; for loop, while loop and the do-while loop type iteration. for loop The for loop type instruction repeats a statement if the condition is true. The for loop contains three types or methods of it's control structure. Initialization, Condition and an Increase or Decrease value. Code: `for(new i = 1; i>0; i++)` This is an example of a for loop. The colours (contained within the parenthesis) represents the type of methods used within it's coding structure. Initialization - The statement represented in dark red in the code above initializes the variable type. The for loop structure allows variables to be initialized within the parenthesis. Declarations must begin at the beginning of the looping structure as it is the first statement. Declarations within the condition is NOT required. pawn Code: `new i;for(i = 1; i>0; i++)` Condition - The statement represented in green is known as the condition. The general purpose of the condition is to evaluate the expression as true. The task proceeding the looping instruction if the condition is true will be executed. pawn Code: `for(new a = 0, b = 1, c = 2; a != b && c; a++)printf("%d",a);` Ending Statement - The statement represented in purple is known as the ending statement. It is not required but a variable must perform a task which is either incremental or decremental. pawn Code: `function(){    for(new countdown = 100, end = 0; end 0)    {        print("function");    }}main(){    function();}` You cannot declare variables within the while loops condition. pawn Code: `#define while2(%0=%1;%2) %0=%1; while(%2)` pawn Code: `main(){    while2(new i=100; i--)    {        printf("%d",i);    }}` do-While loop The do-While loop evaluates the condition not before a statement, but a after. This looping structure begins by executing data proceeding the do statement. pawn Code: `function(){    new a = 10;    do    {        print("function");    }    while (a > 1);}main(){    function();}` Other functionalities or purposes of do-While loop is macro writing. pawn Code: `#define Something(%0) do{new __str[42];format(__str,sizeof(__str),"%d",%0);print(__str);}while(FALSE)` foreach Though not a natively implemented looping instruction, nor is it's structure the same as the for loop, foreach replaces loops with a method more convenient and efficient. Foreach uses a method known as Linked Lists. Instead of each index validating a check of whether the player is connected or not, it stores the id of the next connected player. A separate variable stores the id of the first connected player, therefore, you may use the current index, to retrieve the next index and ONLY loop through connected players until it has been fulfilled (index of -1). pawn Code: `for (new i = 0; i != MAX_PLAYERS; ++i){    if (IsPlayerConnected(i))    {        printf("Player %d is connected", i);    }}` becomes... pawn Code: `foreach (Player, i){    printf("Player %d is connected", i);}` More information relating to foreach, including methods of usage which has NOT been mentioned in the tutorial may be accessed by visiting here. Author: ****** Website: www.y-less.com Jump statements A jump statement is an instruction in which causes a change in program control. Three types of jump statements we will be looking at are; break statement, continue statement and the goto statement. break statement The break statement is a statement in which causes immediate termination of an operation. pawn Code: `#define while2(%0=%1;%2) %0=%1; while(%2)main(){    while2(new countdown = 10; countdown>0)    {        --countdown;        printf("%d",countdown);        if(countdown == 5) break;    }}` Code: ```output: 9 8 7 6 5``` We initiate the countdown to begin at 10. The following condition after initialization is true, therefore the proceeding statements will repeat until countdown is 5 and then the break statement terminates the loop. continue statement The continue statement skips the iteration in a looping instruction and then continues. pawn Code: `main(){    for (new i=10; i>0; i--)    {        if(i == 5) continue;        printf("%d",i);    }}` Code: ```output: 10 9 8 7 6 4 3 2 1``` goto statement The goto statement allows an immediate jump to another part of a code. pawn Code: `main(){    for(new i = 1; i>0; i++)    {        printf("%d",i);        if(i == 10) goto BEGIN;    }    BEGIN:print("End!");}` Conclusion As stated in the introduction, the tutorial only gives a brief explanation in detail of how these looping instructions work. If you have any questions, comments, suggestions, feel free to post in the topic. Special Thanks ****** Last edited by Gh05t_; 24/01/2012 at 11:13 PM.
 23/12/2011, 09:28 PM #2 Juniior Little Clucker   Join Date: Nov 2011 Posts: 14 Reputation: 0 Re: An in-depth look at looping structures Nice Job! [+rep]
23/12/2011, 09:38 PM   #3
Gh05t_
Big Clucker

Join Date: Dec 2011
Posts: 192
Reputation: 140
Re: An in-depth look at looping structures

Quote:
 Originally Posted by Berlovan http://forum.sa-mp.com/showthread.php?t=292506 This ain't right.
I've requested deletion of my *previous account due to security issues. Contacting dugi now.

 29/01/2015, 11:09 AM #4 ZcvDev Banned   Join Date: Jan 2015 Location: Malang, Indonesia Posts: 40 Reputation: 0 Re: An in-depth look at looping structures nice

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Alberto_DR Server Support 1 09/11/2011 02:57 PM lameguy Filterscripts 8 08/09/2010 01:24 AM [Include] [INC] some useful data structures Zeex Includes 7 10/05/2010 07:17 PM Coicatak Help Archive 7 12/03/2009 04:09 PM

All times are GMT. The time now is 02:26 PM.

 -- (EN) English -- (RU) Русски SA-MP - Archive - Top