Pawn from Scratch? Is it really possible to write your own server plugin Week 3
About this content

Week 3: Control Structures and Files
- Control Structures
- What control structures are and why they are needed
- How to use the conditional operators if, else, and switch
- How to use the for, while, and do-while loops
Example of using conditional operators and loops:
main()
{
new x = 10;
if(x > 5)
{
print("x больше 5");
}
else
{
print("x меньше или равен 5");
}
for(new i = 0; i < 5; i++)
{
print(i);
}
new y = 0;
while(y < 5)
{
print(y);
y++;
}
new z = 0;
do
{
print(z);
z++;
} while(z < 5);
}
Explanation:
if(x > 5)- the if conditional operator for checking the value of variable xelse- the keyword for executing an alternative block of codefor(new i = 0; i < 5; i++)- the for loop for executing code 5 timeswhile(y < 5)- the while loop for executing code as long as the condition is truedo-while- the do-while loop for executing code at least once, even if the condition is false
- Working with Files
- How to open and close files
- How to write and read data from files
Example of working with files:
main()
{
new file = fopen("test.txt", "w"); // Открываем файл для записи
if(file != INVALID_HANDLE) // Проверяем, удалось ли открыть файл
{
fwrite(file, "Hello, world!", sizeof("Hello, world!")); // Записываем строку в файл
fclose(file); // Закрываем файл
}
else
{
print("Ошибка открытия файла");
}
new readfile[50];
file = fopen("test.txt", "r"); // Open the file for reading
if(file != INVALID_HANDLE) // Check whether the file was opened successfully
{
fgets(readfile, sizeof(readfile), file); // Считываем строку из файла
print(readfile); // Выводим строку на экран
fclose(file); // Закрываем файл
}
else
{
print("Ошибка открытия файла");
}
}
Explanation:
fopen("test.txt", "w")- opening the test.txt file for writingfwrite(file, "Hello, world!", sizeof("Hello, world!"))- writing a string to a file- `fclose(file






Comments (0)