[Tutorial] An in-depth look at looping structures
#1

An in-depth look at looping structures.

Contents
  • Introduction
  • What is a loop?
  • Compound statements
  • Types of looping instructions
    • for loop
    • While loop
    • do-While loop
    • *foreach
  • Jump statements
    • break 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 Код:
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 Код:
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 Код:
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.

Код:
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 Код:
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 Код:
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 Код:
function()
{
    for(new countdown = 100, end = 0; end<countdown; --countdown)
    printf("%d",countdown);
}

main()
{
    function();
}
An example of a countdown beginning at 100.

Prefix and Postfix incremental and decremental assignments have the same meaning. The only difference, is one evaluates the operator before the variable and the other after.

pawn Код:
i++ //Postfix Incrementation
++i //Prefix Incrementation
i-- //Postfix Decremental Assignment
--i //Prefix Decremental Assignment
While loop
The while loop (as of the for loop example above) will repeat a statement once the condition is true.

pawn Код:
function()
{
    new a = 1;
    while(a > 0)
    {
        print("function");
    }
}

main()
{
    function();
}
You cannot declare variables within the while loops condition.

pawn Код:
#define while2(%0=%1;%2) %0=%1; while(%2)
pawn Код:
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 Код:
function()
{
    new a = 10;
    do
    {
        print("function");
    }
    while (a > 1);
}

main()
{
    function();
}
Other functionalities or purposes of do-While loop is macro writing.

pawn Код:
#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 Код:
for (new i = 0; i != MAX_PLAYERS; ++i)
{
    if (IsPlayerConnected(i))
    {
        printf("Player %d is connected", i);
    }
}
becomes...

pawn Код:
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 Код:
#define while2(%0=%1;%2) %0=%1; while(%2)

main()
{
    while2(new countdown = 10; countdown>0)
    {
        --countdown;
        printf("%d",countdown);
        if(countdown == 5) break;
    }
}
Код:
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 Код:
main()
{
    for (new i=10; i>0; i--)
    {
        if(i == 5) continue;
        printf("%d",i);
    }
}
Код:
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 Код:
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
******
Reply
#2

Nice Job! [+rep]
Reply
#3

https://sampforum.blast.hk/showthread.php?tid=292506

This ain't right.
Reply
#4

Quote:
Originally Posted by Berlovan
Посмотреть сообщение
I've requested deletion of my *previous account due to security issues. Contacting dugi now.
Reply
#5

nice
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)