5 Programs With Selection Logic (Part 1)
5 Programs With Selection Logic (Part 1)
Note:
We do not even need the if statement (there is usually more than one
way to express the logic of a program)
if...else Construct
The if…else selection statement allows you to specify that different
actions are to be performed when the condition is true and when it’s false.
In general, the if...else construct in C takes the form:
if (<condition>) <statement1> else <statement2>
Or
if (<condition>) <statement1>
else <statement2>
Or
if (<condition>) {
...
}
else {
...
}
if...else Construct
Example: A student is given 3 tests, each marked out of 100. The
student passes if his average mark is greater than or equal to 50 and fails
if his average mark is less than 50. Prompt for the 3 marks and print Pass
if the student passes and Fail if he fails.
A sample run of this program is as following:
Sol: