Conditional Statements - Selection Statements: Depending On Certain Conditions
Conditional Statements - Selection Statements: Depending On Certain Conditions
//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;
}
Goto Statement
Leaves the switch block and jumps directly to a label of the form
"<labelname>:“
The goto statement causes program execution to jump to the
label following the goto keyword
//switch (myInput)
{
case "continue":
goto begin;
case "quit":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("Your input {0} is incorrect.", myInput);
goto decide;
}
Continue Statements
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.
//for loop
for (int i=0; i < 20; i++)
{
if (i == 10)
break;
if (i % 2 == 0)
continue;
//While Loop
//do/while loop
static void Main(string[] args)
{
string userIsDone = "";
do
{
Console.WriteLine("In do/while loop");
Console.Write("Are you done? [yes] [no]: ");
userIsDone = Console.ReadLine();
}while(userIsDone.ToLower() != "yes"); // Note the semicolon!
}
The for Loop
A for loop works like a while loop, except that the syntax of the for loop
includes initialization and condition modification.
for loops are appropriate when you know exactly how many times you want to
perform the statements within the loop.
The contents within the for loop parentheses hold three sections separated by
semicolons.
Syntax:
(<initializer list>; <boolean expression>; <iterator list>) { <statements> }
//For loop
for (int i=0; i < 20; i++)
{
if (i == 10)
break;
if (i % 2 == 0)
continue;