Here is a collection of if and if-else statements with semicolons in various places. Assume all variables have been declared and initialized. Which of these are correct and are likely to give the programmers intent? Which are correct but unlikely to give the programmer's intent? Give the error for the remaining.
a) ```
if ( a > b );
a = b;
else
b = a;
```
b) ```
if(a > b )
a = b;
else;
b = a;
```
c) ```
if(a > b )
a = b;
else
b = a;
```
d) ```
if(a > b)
a = b
else
b = a;
```
e) ```
if( x !=0 )
a = a / x
```
c) ```
if(a > b )
a = b;
else
b = a;
```
a) Compiler error: The semicolon at the end of the if line introduces a null statement, making the else keyword an error. The error will not be detected by the compiler until the else is encountered.
b) Compiles with no errors, but is unlikely to do what the code author intended: The indentation suggests that the programmer meant the a=b and b=a lines to be alternatives chosen based on whether a>b. The semicolon at the end of the else causes the statement that appears to be the else clause always to be executed.
c) correct, and apparently does what the programmer intended.
d) Here the compiler will find an error at (or after) the else, since this is the first token after the assignment a=b, where a semicolon is needed.
e) This is the same error as in d), but the error will be detected at or after the last x on the second line.
You might also like to view...
Constant variables that might be used in different functions should be _________
Fill in the blank(s) with the appropriate word(s).
Optional parentheses in expressions are said to be
a. redundant. b. binary operators. c. implied. d. declared.
The range for a graph is also referred to as a ____ or data series.
A. data set B. formula C. cell reference D. data list
To delete a node, we must first locate it.
Answer the following statement true (T) or false (F)