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.

Computer Science & Information Technology

You might also like to view...

Cellular networks use encryption to ensure the data is secure as it travels through the public network

Indicate whether the statement is true or false

Computer Science & Information Technology

HTML is a ____ language with the functionality of hypertext.

A. background B. header C. keytext D. markup

Computer Science & Information Technology