Lecture
To implement a loop with a precondition in the C language, the while keyword is used. The operator part can consist of one operator or of several, united by curly brackets. In general, the operator can be written as:
while (condition)
{
operator 1;
operator 2;
}
The operator part of the cycle is repeated until the condition becomes false or equal to zero. The following relative operations can be used when creating conditions: < , <= , == , > = , > ,! = . Under conditions, it is also possible to use logical constructions, which in C have the following form:
&& - logical "AND"
|| - logical "OR"
! - logical negation
To implement a loop with a parameter, the for keyword is used. The operator record uses 3 expressions that control the operation of the cycle. In general, the cycle has the following form:
for (assignment of the initial value; expression to be tested; expression changing the value of the variable) operator;
The expression assigning the initial value is executed only once before the first action in the loop. Then the checked expression is evaluated. Then the expression modifying the loop variable is evaluated. The for loop in C is a loop with a precondition.
The post-conditional loop in C is implemented by the keywords do and while . In general, the operator can be written:
do
operator;
while (condition);
Task: write a program that displays the following sequence:
ABCDEF
BCDEF
CDEF
DEF
EF
F
#include
int main (void)
{
const int a = 6;
int i;
char k;
for (i = 0; i <6; i ++)
{
for (k = 'A' + i; k <'A' + a; k ++)
printf ("% c", k);
printf ("\ n");
}
}
Comments
To leave a comment
Algorithmization and programming. Structural programming. C language
Terms: Algorithmization and programming. Structural programming. C language