Control Statements are elements in
source code that control the flow of program execution.
Types
of Control Statement:
1. Selection
2. Loop
1.
Selection
a.
If Statement
b. Switch statement
a.
If Statement:
It
selects a statement for execution based on the value of a Boolean expression. Mentioned its types
below,
a.
Simple
If Statement.
b.
if – else Statement.
c.
Else –
If Statement.
Simple If statement:
using System;
public class
IfTest
{
public static void Main()
{
Console.Write("Enter a character: ");
char
c = (char)Console.Read();
if
(Char.IsLetter(c))
if
(Char.IsLower(c))
Console.WriteLine("The character is lowercase.");
}
}
if-else Statement: using
System;
public class IfTest
{
public static void Main()
{
Console.Write("Enter a character: ");
char
c = (char)Console.Read();
if
(Char.IsLetter(c))
if
(Char.IsLower(c))
Console.WriteLine("The character is lowercase.");
else
Console.WriteLine("The character is uppercase.");
else
Console.WriteLine("The character is not an alphabetic character.");
}
}
else-if Statement:
using System;
public class IfTest
{
public static void Main()
{
Console.Write("Enter a character: ");
char
c = (char)Console.Read();
if
(Char.IsUpper(c))
Console.WriteLine("The character is uppercase.");
else
if (Char.IsLower(c))
Console.WriteLine("The character is lowercase.");
else
if (Char.IsDigit(c))
Console.WriteLine("The character is a number.");
else
Console.WriteLine("The character is not alphanumeric.");
}
}
b. Switch Statement:
The switch statement
is a control statement that handles multiple selections by passing control to
one of the case statements within its body using System;
class SwitchTest
{
public static void Main()
{
Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
Console.Write("Please enter your selection: ");
string
s = Console.ReadLine();
int
n = int.Parse(s);
int
cost = 0;
switch
(n)
{
case
1:
cost += 25;
break;
case
2:
cost += 25;
goto
case 1;
case
3:
cost += 50;
goto
case 1;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
if
(cost != 0)
Console.WriteLine("Please insert {0} cents.", cost);
Console.WriteLine("Thank you for your business.");
}
}
c.
Control Statement-Loop
a.
While Statement
b.
Do-While Statement
c.
For Statement
d.
Foreach Statement
For each example:
foreach (data-type variable1 in
collection-variable)
{
statement[s];
}
The foreach loop will be iterated for each and every element
contained in the collection one at a time.
Variable1 is used to represent the current element during a
given iteration.
The foreach iteration is forward only. That is, once the
iteration moves to the next element
then we cannot visit the previous element.This is because no
index variable is used to
access the elements of the collection and the iteration is
implicit.
The foreach iteration is read only. That is, we cannot change
the value of an element during iteration
int[]arr = new int[5]{ 10, 20,
30, 40, 50};
int count
= 1;
foreach(int v in arr)
{
Console.Write(v + "");
//v = count;
not possible
count
= count + 1;
}
Output: 10 20 30 40 50
|