13.07.2016, 22:34
- If you are looking for a only one symbol, it is better to use a loop.
- If necessary to use a for loop for the whole string, it is not necessary to know the size of the string.
PHP Code:new string[128] = "qw;lasdl;k1l;k2l;4k;"; // :D
for (new i; /* none */ ;i++)
{
if (string[i] == 'l')
{
// do something
// ...
// loop is complete
break;
}
// cycle is complete
// if symbol is not found
if (string[i] == '\0')
{
break;
}
}
PHP Code:new string[128] = "qw;lasdl;k1l;k2l;4k;",
i;
while (string[i++] != '\0')
{
if (string[i] == 'l')
{
// do something
// ...
// loop is complete
break;
}
}
- For more optimization you can use variables, so often not to refer to the array index.
PHP Code:new string[128] = "qw;lasdl;k1l;k2l;4k;",
c, // is values of the array index
i // index
;
while ((c = string[i++]) != '\0')
{
if (c == 'l')
{
// do something
// ...
// loop is complete
break;
}
}
- If the condition need to compare three numbers, it is possible to make it so
PHP Code:new value = 7;
if (1 <= value <= 9)
{
// do something
}