Transcribed Image Text:### 3.8.1: If-else statement: Fix errors.
#### Instruction
Re-type the code and fix any errors. The code should convert non-positive numbers to 1.
```c
if (userNum > 0)
printf("Positive.\n");
else
printf("Non-positive, converting to 1.\n");
userNum = 1;
printf("Final: %d\n", userNum);
```
### Example Code with Syntax Errors
```c
#include <stdio.h>
int main(void) {
int userNum;
scanf("%d", &userNum);
if(userNum > 0)
printf("Positive.\n");
else
printf("Non-positive, converting to 1.\n");
userNum = 1;
printf("Final:%d\n", userNum);
return 0;
}
```
### Explanation
The above code has a logical and syntactic error. The `else` statement will always execute because the `userNum = 1;` line is not within the scope of the `else` block. To correct it, place braces `{}` around the `else` block.
### Correct Code
```c
#include <stdio.h>
int main(void) {
int userNum;
scanf("%d", &userNum);
if(userNum > 0) {
printf("Positive.\n");
} else {
printf("Non-positive, converting to 1.\n");
userNum = 1;
}
printf("Final:%d\n", userNum);
return 0;
}
```
### Error Messages
- **Failed to Compile:**
```
main.c:17:4: error: expected identifier or '(' before 'return'
17 | return 0;
| ^~~~~~
main.c:18:1: error: expected identifier or '(' before '}' token
18 | }
| ^
```
- **Explanation:**
The errors reported here suggest a missing syntax that should be addressed by using braces properly around the if-else statements.
### Additional Note:
Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often highlight errors at the end of the code block when the true issue lies within misused statements or blocks.
Process by which instructions are given to a computer, software program, or application using code.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.