cis 3301 c# lesson 3 control statements - selection

15
CIS 330 1 C# Lesson 3 Control Statements - Selection

Upload: timothy-francis

Post on 18-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 1

C# Lesson 3

Control Statements - Selection

Page 2: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 2

Objectives

• Learn the if statements.

• Learn the switch statement.

• Learn how break is used in switch statements.

• Understand proper use of the goto statement

Page 3: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 3

The if Statement

• An if statement allows you to take different paths of logic, depending on a given condition

• Condition: An expression that evaluates to true or false

• Basic Syntax: if (boolean expression) { statements }

• When the condition evaluates to a boolean true, a block of code for that true condition will execute

• else block executes if condition false

Page 4: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 4

The if Statement (cont)if (myInt != 0){

 Console.WriteLine("Your number {0} is not equal to zero.", myInt);

}else{

Console.WriteLine("Your number {0} is equal to zero.", myInt);

} // Multiple Case Decision

if (myInt < 0 || myInt == 0){ Console.WriteLine("Your number {0} is less than or equal

to zero.", myInt);}else if (myInt > 0 && myInt <= 10){ Console.WriteLine("Your number {0} is in the range from 1

to 10.", myInt);}

Page 5: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 5

The if Statement (cont)• In C#, the condition must evaluate to a

boolean value of either true or false

• Conditional OR (||) will evaluate the second sub-expression only if the first sub-expression evaluates to false

• Conditional AND (&&) operator will evaluate the second sub-expression only when the first sub-expression evaluates to true

Page 6: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 6

The Switch Statement

• The switch statement executes a set of logic depending on the value of a given parameter

• Types of the values a switch statement operates on can only be booleans, enums, integral types, and strings

Page 7: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 7

The Switch Statement (cont)switch (myInt){

case 1:       Console.WriteLine("Your number is {0}.", myInt);       break;case 2:       Console.WriteLine("Your number is {0}.", myInt);       break;case 3:       Console.WriteLine("Your number is {0}.", myInt);       break;default:       Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);       break;

}

Page 8: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 8

C# Branching StatementsBranching statement Description

break Leaves the switch block

continue

Leaves the switch block, skips remaining logic in enclosing loop, and goes back to loop condition to determine if loop should be executed again from the beginning. Works only if switch statement is in a loop as described in Lesson 04: Control Statements - Loops.

Goto (Not Recommended)

Leaves the switch block and jumps directly to a label of the form "<labelname>:"

returnLeaves the current method. Methods are described in

more detail in Lesson 05: Methods.

throwThrows an exception, as discussed in

Lesson 15: Introduction to Exception Handling.

Page 9: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 9

C# Lesson 4

Control Statements - Loops

Page 10: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 10

Objectives

• Learn the while loop.

• Learn the do loop.

• Learn the for loop.

• Learn the foreach loop.

• Complete your knowledge of the break statement.

• Teach you how to use the continue statement

Page 11: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 11

The while Loop • While loop will check a condition and then

continues to execute a block of code as long as the condition evaluates to a boolean value of true

• Syntax: while (<boolean expression>)

{

<statements>

}

Page 12: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 12

The while Loop (cont)

int myInt = 0;

while (myInt < 10)

{ Console.Write("{0} ", myInt); myInt++;

}

Page 13: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 13

The do Loop • Similar to the while loop, except that it

checks its condition at the end of the loopdo{

// Print A Menu      Console.WriteLine("My Address Book\n");

   Console.WriteLine("A - Add New Address");   Console.WriteLine("D - Delete Address");   Console.WriteLine("M - Modify Address");   Console.WriteLine("V - View Addresses");   Console.WriteLine("Q - Quit\n");

  Console.Write("Press Enter key to continue...");   Console.ReadLine();   Console.WriteLine();

} while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit

Page 14: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 14

The for loop• Loop includes initialization and condition

modification• Syntax: for (<initializer list>; <boolean

expression>; <iterator list>) { <statements> }

      for (int i=0; i < 20; i++)  {    if (i == 10)       break;

    if (i % 2 == 0)       continue;    Console.Write("{0} ", i);  }

Page 15: CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 330 15

The foreach Loop• Loop used to iterate through the items in a

list • Syntax: foreach (<type> <item name> in

<list>) { <statements> }  string[] names = {"Cheryl", "Joe", "Matt", "Robert"};

foreach (string person in names){  Console.WriteLine("{0} ", person);

}