Chapter 7 Program Control Statements
What’s in This Chapter
- The
if-else
andswitch
statements - The
?:
and??
operators - The
for
,while
,do
, andforeach
loops - Enumerators
- The
break
andcontinue
statements
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 ...
Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.