disc 2 - 3 If I leave out the else both operations continue...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1. An if-else statement is widely used to perform a task given a wide range of user inputs. However, the switch statement is better utilized when a specific integer in a specified range will be used. For instance, if the user must select a choice 1-9, the switch is the better option and results in simpler code. 2. Either can be coded incorrectly. If the == statement is instead coded as =, it will result in a reassignment of the variable, instead of a relational expression. Also, not using the default statement when ending the switch, will result in losing control in the program and an error. Also, not properly using braces may put if-else statements inside other if-else statements.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3. If I leave out the else, both operations continue if the condition is met. However, only the second operation went forward without the condition met. Also, with the condition (5 == num), when the = is used vice ==, it gives me an error of "non-lvalue in assignment." 4. The code should have the main commands indented once, and commands within if-else and switches indented twice. Curly braces should be used for grouping statements in groups to be performed. I.e. { printf("Enter a number:"); scanf("%d", %num); if (1 == num) printf("Number is 1"); else if (2 == num) printf("Number is 2"); else printf("Number is not 1 or 2"); }...
View Full Document

This document was uploaded on 01/22/2011.

Ask a homework question - tutors are online