What does l if mean in c. Conditional statement if else
Consistent execution is so common that we rarely remember it as a control structure. The sequence of statements is executed in the order of their natural location in the program, with a possible deviation to call an external fragment (function), but with a mandatory return to the point of call.
In the simplest case, branching is described in the C language using a conditional operator. having the form:
if (expression) operator_1; else operator_2; |
where is the part else may be absent. First the "expression" is evaluated in brackets; if it is true then it is executed operator_1. If " expression" false (equal to zero - NULL), That operator_1 is skipped and executed operator_2. If in place of conditionally executed operators there should be a group of several language operators, then they are enclosed in curly braces - { }. Often the "expression" in parentheses represents a condition specified using relational and logical operators. Relational operations are denoted in C as follows:
= = equal; ! = not equal;< меньше; >more;
< = меньше или равно; >= greater than or equal to.
Symbol !
in C language denotes logical negation. There are two more logical operations: ||
means or &&
- logical AND. Relational operations take precedence over arithmetic operations, so an expression of the form k > n%i calculated as k > (n%i). A priority &&
higher than ||, but both logical operations are performed after relational and arithmetic operations. In doubtful cases, it is better to use parentheses.
To illustrate the use of the conditional operator, consider a program to determine the largest of three numbers. First if the operator represents a complete conditional construction, in the second case else absent. Note that the semicolon ends the assignment statement max=x, does not violate unity if- operator. If else- the branch is skipped in nested conditions, their interpretation may be ambiguous. To avoid ambiguity, solve it this way: else matches the nearest if, not having his own else.
Example 1.3.
Let's look at an example of a program that uses several nested conditional statements. In this program there is a line float A, B, X declares these three variables as real values. Function format string scanf instructs you to enter two real numbers, which will become the values of the variables A and B respectively.
Example 1.4
/*SOLUTION OF EQUATION AX=B*/ #include main() { float A,B,X; |
See what branching looks like when the nesting depth of conditional statements is three (Example 1.5). If at least one condition is true, then all the remaining ones are, of course, skipped. When the nesting depth of conditional statements exceeds three, branching loses clarity and clarity.
To implement multitasking branching, they usually resort to a control structure choice ( switch) (see paragraph 9.4). When the branch control structure becomes particularly confusing, curly braces can provide some clarity. They are required when conditional operator contains more than one statement or function, for example
Laboratory work
On the topic of: " Condition operator if - else "
1. Purpose and syntax
The if-else condition statement is used to select the direction of the program's operation depending on the conditions prevailing at a given point in the program at the time of its execution.
General form of writing a conditional operator
if ( <условие>)
<блок операторов 1>;
<блок операторов 2>;
If at the time of execution<условие>true, the program transfers control<блоку операторов 1>and, further, to the first statement outside the if-else construct. Wherein<блок операторов 2>is not executed. Otherwise, if<условие>false, executed<блок операторов 2>, A<блок операторов 1>skipped. Corresponding block diagram
Curly braces in the if-else statement syntax are used to separate blocks 1 and 2 in the text. Try to place the closing brace below the opening brace to improve readability program code. For the same purpose, the text inside curly braces must be shifted to the right several positions.
As a condition in if-else statements any can be used logical expressions, taking the values “true” or “false” (true – false). Below is a table showing the simplest comparison operations between integers and real numbers
Example 1. It is required to write a program that converts temperature on the Celsius scale T C (°C) to temperature on the Kelvin scale T K (K). The T C value is entered by the user from the keyboard.
Solution. We use well-known formula transformation – T K = T C – T 0, where T 0 = –273 °C is the temperature of absolute zero. We will consider the entered T C to be incorrect if it is less than T 0 .
// – KelvinvsCelsius–
#include
#include
#pragma argsused
floatT0 = -273; // declare and initialize T0
floatTc, Tk; // declare real Tc and Tk
cout<<» VvediteTc=»; // выводим приглашение
cin>>Tc; // request Tc
if
(
Tc
<
T
0)
// check condition Tc cout<<» Tc < T0!»; // условие истинно, выводим на }
// error message screen Tk = Tc-T0; // condition is false, calculate cout<< «Tk =» << Tk; // Tk и выводим на экран getch(); // delay before pressing a key return 0; // end the program Type the above code, compile it and run the program. Examine the results of operation at various values of T C . 2. Shortened recording options In programming, it is common to find that some action is required in response to existing conditions (for example, if incorrect input data is received from the user, then issue an error message and exit the program). In such cases, C++ programs may use a shorthand notation of the conditional statement with the missing else block. The general form of such a record if
(
<условие>)
<блок операторов>; Here, if the condition is true, control is transferred to the block of statements in curly braces. If the condition is false, this block is skipped. The corresponding block diagram differs from the previous one in the absence of one “arm” Another shorthand option is used when any of the if or else blocks consist of only one statement. In this case, the absence of curly braces delimiting this block is allowed. if
(
<условие>)
operator 1; operator 2; Here, operators 1 and 2 can be not only simple one-line arithmetic operations or I/O operators, but also complex multi-line constructs, such as other (nested) conditional statements or loop operators, which will be discussed below. 3. Nested statements Conditional statements can be nested within each other, in accordance with the software algorithm that they implement. An arbitrary degree of their “nesting” is allowed. If one if-else statement is nested within another, then the first statement is included in the second fully, and not just any one of its if or else parts. Partial overlap of their individual blocks is unacceptable. In the example above, one of the statements (shown in bold) is nested within another. Entry B) is incorrect due to the fact that the else block of the inner conditional statement partially overlaps with both the if and else blocks of the outer statement. Example 2. The user enters three integers a, b, c from the keyboard. It is necessary to display the largest of these numbers. Solution. One of the possible algorithms for solving this problem is shown in the following block diagram. The scheme can be implemented programmatically using nested if-else statements // – Selecting the largest of 3 numbers – #include #include #pragma argsused int main (int argc, char* argv) float a, b, c; // declare three variables cout<< «Vvedite a –»; // вводимзначения a, b, c cout<< «Vvedite b –»; cout<< «Vvedite c –»; if (a>b) // if a > b if (a>c) // if a > c