Contacts

Questions and tasks. Conditional operator? Operator usage restrictions

The first operand - "expression1" - can be any expression whose result is a value of type bool . If the result is true , then the operator specified by the second operand, that is, "expression2", is executed.

If the first operand is equal to false , then the third operand is executed - “expression3”. The second and third operands, that is, "expression2" and "expression3", must return values ​​of the same type and must not be of type void . The result of executing a conditional statement is the result of "expression2" or the result of "expression3", depending on the result of "expression1".

Operator usage restrictions

The operator based on the value of "expression1" must return one of two values ​​- either "expression2" or "expression3". There are a number of restrictions on these expressions:

  1. You cannot mix a user-defined type with a simple type or enumeration. It is acceptable to use NULL for a pointer.
  2. If the value types are simple, then the type of the operator will be the maximum type (see Type Coercion).
  3. If one of the values ​​is an enumeration type and the second is a numeric type, then the enumeration is replaced by int and the second rule applies.
  4. If both values ​​are enum values, then their types must be the same, and the type of the operator will be an enumeration.

Limitations for custom types (classes or structures):

  1. the types must be the same or one must inherit from the other.
  2. if the types are not the same (inheritance), then the child is implicitly cast to the parent, that is, the type of the operator will be the type of the parent.
  3. You cannot mix object and pointer—either both expressions are objects or pointers. It is acceptable to use NULL for a pointer.

Note

Be careful when using a conditional operator as an argument to an overloaded function, since the type of the conditional operator's result is determined at the time the program is compiled. And this type is defined as the larger type of the types "expression2" and "expression3".

Example:

void func(double d) ( Print ("double argument: " ,d); )
void func(string s) ( Print ("string argument: " ,s); )

bool Expression1=true;
double Expression2=M_PI;
string Expression3= "3.1415926" ;

void OnStart()
{
func(Expression2);
func(Expression3);

func(Expression1?Expression2:Expression3);
func(!Expression1?Expression2:Expression3);// get a compiler warning about explicit casting to string type
}

// Result:
// double argument: 3.141592653589793

// string argument: 3.141592653589793
// string argument: 3.1415926

Data output
Outputting data from random access memory on the monitor screen:
write
(<выражение 1> ,< выражение 2> , ...,< выражение N>)
output list
Expressions - symbolic, numeric, logical,
including variables and constants
Example:
write("s=", s).
For s=15 the screen will show: s=15.
Information in quotation marks is displayed on the screen
without changes

Output organization options
Option
organization of withdrawal
No separators
Inference operator
write(1, 20, 300).
Result
120300
Add delimiters write (1, ’,’ , 20,
– commas
’, ’, 300)
1, 20, 300
Add delimiters write (1, ‘ ‘, 2, ‘ ‘, 3)
– spaces
1 20 300

Output Format
The output format allows you to set the number of positions
on the screen occupied by the displayed value.
write(s:x:y)
x - the total number of positions allocated for the number;
y - the number of positions in the fractional part of the number.
Inference operator
Execution result
operator
write(‘s=‘, s:2:0);
s=15
write(‘s=‘, s:3:1);
s=15.0
write(‘s=‘, s:5:1);
s=
writeln
15.0
- output from a new line!

First program
program n_1;
const pi=3.14;
var r, c, s: real;
begin
r:=5.4;
c:=2*pi*r;
Result of the program:
s:=pi*r*r;
writeln("c="", c:6:4);
writeln("s=", s:6:4)
Turbo Pascal
Version 7.0
end.
c =33.9120
s =91.5624

Keyboard input
Entering variable values ​​into RAM:
read
(<имя переменной1>, …, <имя переменной N>)
input list
Executing the read statement:
1) the computer goes into data standby mode:
2) the user enters data from the keyboard:
multiple variable values
numeric types can be entered
separated by space or comma;
when entering character variables
Spaces and commas cannot be used;
3) the user presses the Enter key.

Keyboard input
!
The input value types must match
variable types specified in the description section
variables.
var i, j: integer;x: real;a: char;
read(i, j, x, a);
options for organizing the input stream:
1 0 2.5 A 1,0 1
2.5, A 0
2.5
A
After the readln statement is executed, the cursor moves to
new line.

Improved program
program n_1;
const pi=3.14;
var r, c, s: real;
begin
writeln("Calculate the circumference and area of ​​a circle");
write("Enter r>>");
readln(r);
c:=2*pi*r;
Result of the program:
s:=pi*r*r;
writeln("c="", c:6:4);
Pascal Version 7.0
writeln("s=", s:6:4) Turbo
Calculating the circumference and area of ​​a circle
Enter r>> 8.5
end.
c =53.3800
s =226.8650

The most important
To enter variable values ​​into RAM
The read and readln input operators are used.
To display data from RAM on the screen
The monitor uses the write and writeln output operators.
Input of initial data and output of results must
be organized clearly and conveniently; this ensures
user-friendliness of the user interface.

Questions and tasks
1) Given a program fragment:
a:=10; b:=a+1: a:=b–a; write (a, b)
What numbers will be displayed on the computer screen?
2) Describe the variables needed for the calculation
the area of ​​a triangle along its three sides, and
write a statement providing input
necessary initial data.
3) What is the result of executing the statement?
a) write (a)
b) write("a")
c) write("a=", a)
4) Integer variables i, j, k need to be assigned
respectively, the values ​​are 10, 20 and 30.
Write down the input statement corresponding to the input
stream:
a) 20 10 30
b) 30 20 10
c) 10,30,20

In the previous paragraph, we got acquainted with the structure of a program in Pascal, learned how to describe data, and looked at the assignment operator. This is enough to write a data conversion program. But the result of these transformations will not be visible to us.

To output data from RAM to the monitor screen, use the write output operator:

Here, in parentheses, an output list is placed - a list of expressions whose values ​​are printed. These can be numeric, symbolic or logical expressions, including variables and constants.

An arbitrary set of characters enclosed in apostrophes is considered a string constant. A string constant can contain any characters typed on the keyboard.

Example. The write ("s=" , s) statement is executed like this:

  1. Symbols enclosed in apostrophes are displayed on the screen: s=
  2. The value of the variable stored in a RAM cell named s is displayed on the screen.

If the value of the variable s is 15 and it is of integer type, then the screen will display: s=15.

If the value of the variable s is 15, but it is of real type, then the following will appear on the screen: s=l.5E+01.

When the output statement is executed, all elements of the output list are printed immediately after each other. Thus, as a result of the write (1, 20, 300) operator, the sequence of numbers 120300 will be displayed on the screen, which will be perceived by us as the number 120300, and not as three separate numeric constants. You can make the output data more accessible to perception in different ways:

Output Format is an integer indicated after the colon that determines how many positions on the screen the displayed value should occupy. If there are fewer digits in a number than the positions reserved for it on the screen, then the free positions are supplemented with spaces to the left of the number. If the number specified in the output format after the colon is less than necessary, it will automatically be increased to the minimum required.

To output a real number in fixed-point format, two parameters are specified in the output list for each expression:

  1. the total number of positions allocated for the number;
  2. the number of positions in the fractional part of the number.

When a new write statement is executed, the output continues on the same line. To make the transition to new line, the writeln operator is used. There are no other differences between the write and writeln statements.

4.2.2. First program in Pascal language

Using the operators discussed above, we will create a program that calculates the circumference and area of ​​a circle with a radius of 5.4 cm.

The initial data in this problem is the radius: r - 5.4 cm. The result of the program should be the values ​​C - the circumference and S - the area of ​​the circle. C, S and r are quantities of real type.

The initial data and results are related by relations known from the mathematics course: C = 2πr, S = πr +. A program that implements calculations using these formulas will look like:

This program is correct and solves the problem. When you run it, you will get the following result:

Still, the program we compiled has a significant drawback: it finds the circumference and area of ​​a circle for a single radius value (5.4 cm).

In order to calculate the circumference and area of ​​a circle for a different radius value, you will need to make changes directly to the program text, namely, change the assignment operator. Making changes to an existing program is, to say the least, not always convenient (for example, when the program is large and there are many assignment operators). Below you will get acquainted with an operator that allows you to enter initial data while the program is running, without changing the program text.

4.2.3. Keyboard input

To enter variable values ​​into RAM, use the read input operator:

When the read statement is executed, the computer enters data-waiting mode: the user must enter data from the keyboard and press the Enter key. Multiple values ​​for numeric type variables can be entered separated by spaces or commas. When entering character variables, spaces and commas are treated as characters, so they cannot be entered.

The first variable value entered by the user is placed in the memory location whose name is located first in the input list, etc. Therefore, the types of input values ​​(input stream) must correspond to the types of variables specified in the variable description section.

Example. Let

var i, j: integer; x: real; a:char;

Let's assign the variables i, j, x, and the values ​​1, 0, 2.5 and "A". To do this, we will use the read (i, j, x, a) operator and organize the input stream in one of the following ways:

Here we not only used various delimiters (space, comma), but also represented the input stream as one, two and four lines.

You can also use the readln operator to enter data from the keyboard, which differs from the read operator only in that after it is executed, the cursor moves to a new line.

Let's improve program n_1 by organizing data entry in it using the read operator. And so that the user knows what the program is intended for and understands what action the computer expects from him, we will display the corresponding text messages using the writeln operator:

The result of the improved program:

Now our program can calculate the circumference and area of ​​a circle for any value of r. In other words, it solves not a single problem, but a whole class of problems. In addition, the program clearly and conveniently organizes the input of initial data and the output of the results obtained. This ensures a friendly user interface.

The most important

To enter variable values ​​into RAM, the read and readln input operators are used.

To output data from RAM to the monitor screen, the write and writeln output operators are used.

Input of initial data and output of results should be organized clearly and conveniently; this ensures a friendly user interface.

Questions and tasks

  1. Write a statement that allows you to enter the value of the summa variable while the program is running.
  2. The integer variables i, y, k need to be assigned the values ​​10, 20 and 30, respectively. Write down the input statement corresponding to the input stream:
      a) 20 10 30
      b) 30 20 10
      c) 10 30 20
  3. Describe the variables needed to calculate the area of ​​a triangle based on its three sides, and write a statement that provides the required input data.
  4. What is the result of executing the statement?
      a) write (a)
      b) write (1 a ")
      c) write (1 a=1, a)
  5. What type is the variable f if, after executing the write (f) statement, the following number was displayed on the screen?
      a) 125
      b) 1.25E+2
  6. How can I display a real number in fixed point format?
  7. Write down the operators for entering two numbers and outputting them in reverse order.
  8. Here is a fragment of the program:

    read(a); read(b); c:=a+b; write(a, b); write (c)

    Simplify it by reducing the number of input and output statements.

  9. Here is a fragment of the program:

    a:=10; b:=a+l: a:=b-a; write (a, b)

    What numbers will be displayed on the computer screen?

  10. Write a program that calculates the area and perimeter of a rectangle based on its two sides.

The program code is shown in Fig. 4.3-9.

Option Strict On Option Explicit On Imports System.Math Public Class Form1 "Function for entering the coordinates of a point in a TextBox Function vvod(ByVal T As TextBox) As Single Return CSng(Val(T.Text)) End Function "Procedure for outputting the result in a TextBox Sub vivod (ByVal Z As Single, ByVal T As TextBox) T.Text = CStr(Z) End Sub "Function for calculating the length of a segment from the coordinates of two points Public Function Segment Length(ByVal x1 As Single, _ ByVal y1 As Single, ByVal x2 As Single, _ ByVal y2 As Single) As Single Return CSng(Sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)) End Function "Procedure for calculating the perimeter and area of ​​a triangle Sub PS(ByVal x1 As Single, ByVal y1 As Single, _ ByVal x2 As Single, ByVal y2 As Single, _ ByVal x3 As Single, ByVal y3 As Single, _ ByRef P As Single, ByRef S As Single) Dim A, B, C, Pp As Single A = Segment Length(x1 , y1, x2, y2)"Calling the function for calculating the length of the negative. B = Length of the Segment (x2, y2, x3, y3) C = Length of the Segment (x3, y3, x1, y1) P = (A + B + C) : Pp = P / 2 S = CSng(Sqrt(Pp * (Pp - A) * (Pp - B) * (Pp - C))) End Sub "Procedure for processing the click event on the button Button1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim x1 As Single, x2 As Single, x3 As Single Dim y1 As Single, y2 As Single, y3 As Single Dim Per As Single, Plo As Single x1 = vvod(TextBox1) : x2 = vvod(TextBox2) : x3 = vvod(TextBox3) y1 = vvod(TextBox6) : y2 = vvod(TextBox7) : y3 = vvod(TextBox8) PS(x1, y1, x2, y2, x3, y3, Per, Plo) vivod(Plo, TextBox4) : vivod(Plo, TextBox5) End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click End End Sub End Class

Running the application and getting results

The result is shown in Fig. 4.3-10.

Proof of the correctness of the result

A=
= 2.82843 B =
= 2

C = = 2 P = A + B + C = 6.828427; Рр = Р/2 = 3.414213 S= = 2


Security questions on the topic

“The structure of VB programs, modules and procedures.

Programming tools for linear structure algorithms"

1. What is a mathematical model?

2. What are the main stages involved in solving problems on a computer?

3. What stages of computer problem solving are carried out without the participation of a computer?

4. What is called a mathematical model of an object or phenomenon?

5. What are the sequential steps in the program development process?

6. What is an algorithm?

7. What basic properties should an algorithm have?

8. What ways are there to describe algorithms?

9. What graphic symbols Is it customary to depict it in algorithm diagrams?

10. How does the interpreter work?

11. How does the compiler work?

12. Which algorithm is called linear?

13. Which algorithm is called cyclic?

14. Which algorithm is called branching?

15. What is the difference between passing the results of functions and procedures?

16. Does obtaining a plausible result prove that the program is correct?

17. What errors may remain undetected if you do not check (view, scroll) the program?

18. How is testing a program different from debugging it?

19. Is it possible to prove the correctness of a program through testing?

20. At what stage of the program are the reference test results calculated?

21. List the main stages of the testing process.

22. What is the difference between syntax errors and semantic errors?

23. What does the machine's lack of syntax error messages indicate?

24. What types of errors is the translator unable to detect?

25. What is the program?

26. At what stage does the search for and elimination of errors in the program take place?

27. What is a programming system?

28. What is the procedure?

29. What are the benefits of a program that has procedures?

30. What procedural programming tools are available in VB?

31. How is data exchanged between individual program procedures?

32. What are actual and formal parameters?

33. How to pass as a parameter: a constant, a variable, an expression?

34. How are global variables used?

35. How are local variables used?

36. What are formal parameters?

37. What are the actual parameters?

38. What happens when a procedure is called?

39. How can parameters be passed to a procedure?

40. What parameters are passed by value?

41. What parameters are passed to the address?

42. What are procedural programming tools?

43. What is the result of executing a function?

44. How should individual variables be declared so that they are local within a function?

45. Which statement is used to exit a function early?

46. What is controlled when calling functions and procedures?

47. How should formal parameters be declared in order to pass a variable parameter by value?

48. How should formal parameters be declared in order to pass a variable parameter to an address?

49. How is a function procedure called from the calling program?

50. What could be the result of the procedure?

51. Which statement is used to exit a procedure early?

52. Where can descriptions of functions and procedures be located?

53. Where will a variable declared using the Public operator be available?

54. Where can the Private operator be located?

55. What is a comment and the rules for writing it?

56. What are the values ​​of the variables at the beginning of the program?

57. Why are procedures and functions described (defined)?

58. How are procedures called?

59. How are functions called?


Related information.


“Printer for printing plastic cards” - The ability to place twice as much information on one plastic card. Price. Printer for printing plastic cards. ZEBRA P110i/ P120i. Review model range. Economical full-color printers for printing plastic cards. High performance Wide range of optional encoding devices.

"PC peripherals" - Peripherals. Printers. Record. Digital cameras. Hierarchy of connection tools. Video adapters. Classification of PU. Universal serial bus. CD-R. Selection options. Built-in flash. Pioneers. Nature. Modem. Video terminal. Scanners. PC peripherals. Classification of mouse-shaped.

“Means for input and output of information” - Scanner. Plotter. CRT. Printers. Keyboard. Digital cameras and cameras. Functions. Universal input device. Information input and output devices. Graphics tablet. Mouse.

“Information output devices” - Computer device. The higher the resolution of the monitor, the higher the image quality. Flaws inkjet printers: High ink consumption; High price refills. Flat-panel liquid crystal (LCD) monitors are compact and emission-free. Information output devices. The monitor is a universal information output device.

"Printers" - Local. Thermoblock, stove, fuser - a unit in which toner is baked into paper. Laser. Matrix (needle-shaped). Over time, they lose their properties and must be regularly replaced by a specialist. Jet. Network. Changed by the user. Printer characteristics. Developer, carrier, developer - the smallest metal particles that transfer toner to the photo roll.

"I/O subsystem" - Breakpoint. Tables. Continuous placement. Attribute. File permissions. Linked list of indexes. Data. Physical organization. Direct memory access. Indexed-sequential file. Mounting. Interrupt driven I/O. Directory access permissions. Organization of parallel work.

There are a total of 27 presentations in the topic

Did you like the article? Share it