The if Statement
if statement is used to take different paths of logic, depending on the conditions.
if statement is used to take different paths of logic, depending on the conditions.
|
The switch Statement
Another form of selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter. The types of the values a switch statement operates on can be booleans, enums, integral types, and strings.
Another form of selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter. The types of the values a switch statement operates on can be booleans, enums, integral types, and strings.
using System;
class SwitchTest
{
public static void Main()
{
Console.WriteLine("milk bottel size: 1=Small 2=Medium 3=Large");
Console.Write("Please enter your selection: ");
string s = Console.ReadLine();
int n = int.Parse(s);
int price = 0;
switch(n)
{
case 1:
price += 25;
break;
case 2:
price += 25;
goto case 1;
case 3:
price += 50;
goto case 1;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
if (price != 0)
Console.WriteLine("Please insert {0} cents.", price);
Console.WriteLine("Thank you for your business.");
}
} |
The while Loop
While loop is used to 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>)While loop is used to 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:-
{
<statements>
}
When the boolean expression evaluates to false, the while loop statements are skipped and execution begins after the closing brace of that block of code.
using System;
class whiletest
{
static void Main()
{
//
// Continue in while loop until index is equal to ten.
//
int i = 0;
while (i < 10)
{
Console.Write("While statement ");
//
// Write the index to the screen.
//
Console.WriteLine(i);
//
// Iterate the variable.
//
i++;
}
}
} |
The do Loop
A do loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the do loop is guaranteed to execute at least one time.
A do loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the do loop is guaranteed to execute at least one time.
using System;
class DoWhileLoopDemo
{
public static void Main()
{
int i = 0; // Initialize counter variable
do
{
if ((i % 2) == 0)
{
Console.WriteLine(i);
}
i++; //Increment the counter
}
while (i <= Limit); // Condition check
}
} |
The for Loop
It 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 (<initializer list>; <boolean expression>; <iterator list>) { <statements> }.
It 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 (<initializer list>; <boolean expression>; <iterator list>) { <statements> }.
using System;
class ForLoop
{
public static void Main()
{
for (int i=0; i < 20; i++)
{
if (i == 10)
break;
if (i % 2 == 0)
continue;
Console.Write("{0} ", i);
}
Console.WriteLine();
}
} |
Foreach Statement:
The foreach statement is new to the C family of languages; it is used for looping through the elements of an array or a collection.
The foreach statement is new to the C family of languages; it is used for looping through the elements of an array or a collection.
class ForEachTest
{
static void Main(string[] args)
{
int[] num = new int[] { 0, 1, 2, 3, 4, 5, 6,7,8 };
foreach (int i in num)
{
System.Console.WriteLine(i);
}
}
}
No comments:
Post a Comment