Chapter 7 Program Control Statements
What’s in This Chapter
- The
if-elseandswitchstatements - The
?:and??operators - The
for,while,do, andforeachloops - Enumerators
- The
breakandcontinuestatements
Wrox.com Downloads for This Chapter
Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/csharp5programmersref on the Download Code tab.
Program control statements tell an application which other statements to execute under different circumstances. They control the path that execution takes through the code. They include commands that tell the program to execute some statements but not others and to execute certain statements repeatedly.
The two main categories of control statements are decision statements (or conditional statements) and looping statements. The following sections describe in detail the decision and looping statements provided by C#.
Decision Statements
A decisions statement or conditional statement represents a branch in the program. It marks a place where the program can execute one set of statements or another, or possibly no statements at all, depending on some condition. These include several kinds of if statements and switch statements.
if-else Statements
The if-else statement has the following syntax:
if (condition1) statement1;
else if (condition2) statement2;
else if (condition3) statement3;
...
else statementElse;
The conditions are logical expressions ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access