C TutorialLesson 13Free preview
C If...Else
This lesson is part of C · All levels
Summary
If and else branching allows the program to select the execution path based on certain conditions. The simple if form checks one condition, if...else provides two branches, whereas if...else if...elseif...else handles many consecutive options. The condition is written in brackets and terminated in curly braces, where the non-zero value is considered true. A neat twist makes the program able to make logical decisions.
Key points
- If checking the condition; if the value is correct, the block in it is run
- Else provides an alternative branch when the condition if is worth wrong
- Chain else if used to handle more than two possibilities
- Comparison operator == compares value, not = which means assignment
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.