This is a Clilstore unit. You can link all words to dictionaries.

Transition statement Goto. Random number generation

When developing programs sometimes it is necessary to transfer control not to the next, but to any other statement. For this purpose, the statement to which the control is transferred shall be labeled with a mark. The mark is an identifier followed by a colon. To transfer the control transition operator gotois used.

Statement format has the following view: goto label;

For example:

goto a1;

a1: statement;

b1, c1: statement;

goto b1;

The mark consists of Latin letters or letters and digits. The mark is written at the beginning of the string and also can be on a separate string. If after the mark there is a semicolon, then such statement is called blank.

For example:

a1:;

goto a1;

As a result, control is transferred to the blank operator.

Consider the examples of solving tasks with the transition statement

Example 12. The program displays on the screen a sequence of numbers 1000, 999, 998, 997 etc.

#include <iostream.h>

#include <conio.h>

main()

{int x;

x=1000;

a1: cout<<"\n x="<<x; system("PAUSE");

x--;

goto a1;

getch();

}

As a result of work of the program, integers will be displayed on the screen in the descending order. Team system ("PAUSE") displays numbers with delay. After display of numbers, the program expects pressing of any key.

Example 13. Find the solution of the quadratic equation ax2 +bx+c=0.

#include <iostream.h>

#include <conio.h>

#include <math.h>

main()

{ int a, b, c, D; float x1, x2, x;

cin>>a>>b>>c;

D=b*b-4*a*c;

if (D>0) {x1= (-b+sqrt (D))/(2*a); x2=(-b- sqrt (D))/(2*a);

cout<<”x1=”<< x1<<”x2=”<<х2; goto a1; }

if (D<0) cout<<”no solutions” ; else {x=-b/(2*a); cout<<”x1=x2=”<< x;}

a1: ;

getch();

}

If in this program, when executing condition D> 0, do not write statement goto a1, then after display of values x1, x2 the second condition will be checked. That is, statement else will be executed and another value x will be displayed. It would be the error.

Short url:   http://multidict.net/cs/6247