C#
Control Statements
PF 102
Control statements
Linear (sequential) program execution
Selection structure and repetition structure
Structured programming
◦ Controlled entry and exit out of a module.
◦ Avoid goto statements
Selection structures in C#
if
 single selection statement
if…else
double selection statement
switch
multiple – selection statement
Examples:
if (grade >= 60)
Console.writeline(“Passed!”);
If (grade >=0)
Console.writeline(“Passed!”);
else
Console.writeline(“Failed!”);
Conditional Operator
Console.writeline(grade >= 60 ?“Passed!” : “Failed!”);
Nested
if (grade >=90)
Console.writeline(“A”);
else if (grade >=80)
Console.writeline(“B!”);
…
else
Console.writeline(“F!”);
Repetiton Structure - while
While (condition){
statement 1;
statement 2;
statement 3;
…
}
Example
length = Convert.ToInt16(Console.ReadLine());
while (length > 0)
{
Console.Write("Enter Height of the Wall: ");
height = Convert.ToInt16(Console.ReadLine());
PaintAWall thisWall = new PaintAWall(length, height, pricePerGal);
thisWall.CalculateCost(ref paintCost,ref laborCost,ref galPaint, ref sqFt);
Console.Write("Enter Length and Height for Wall # : “ +
Convert.ToString(numWalls+1));
Console.Write("nEnter Length of the Wall (0 to quit): ");
length = Convert.ToInt16(Console.ReadLine());
}
Counter controlled vs
Sentinel controlled
A while loop, use LCV as a counter
Counter =1
While (counter <=10)
{
…
Counter ++
}//does it 10 times
Sentinel controlled is good when one does not know exact number of
times to execute a loop
Explicitly and Implicitly converting
between simple types
Integer and integer division yields integer result.
Suppose average is a floating point number:
Average = total/num. Average will only get an integer if
total and num are integers.
int sum = 200, num = 3;
float average;
average = sum / num;
Console.WriteLine(average);
Will print 66
Unary Cast Operator
int sum = 200, num = 3;
float av;
av = (float) sum / num;
Console.WriteLine(av);
Will print 66.6666
Float/float or float/int or int/float will yield a float.
C# implicitly promotes the one int to float.
Nested control statements
See example of a multiplication table generation
class MultiplicationTable{
static void Main(string[] args){
int i=2, j, k;
while (i <= 12){
for (j = 1; j <= 10; j++){
Console.WriteLine(i + " x " + j + " = " + i * j );
}
Console.WriteLine("n");
i++;
}
}
}
}
Switch Statements
Switch (expression) {
case value1: //Statements
break;
case value 2: //Statements
break;
case value 3: //Statements
case value n: //Statements
break;
default: //Statements
}

C# Control Statements, For loop, Do While.ppt

  • 1.
  • 2.
    Control statements Linear (sequential)program execution Selection structure and repetition structure Structured programming ◦ Controlled entry and exit out of a module. ◦ Avoid goto statements
  • 3.
    Selection structures inC# if  single selection statement if…else double selection statement switch multiple – selection statement
  • 4.
    Examples: if (grade >=60) Console.writeline(“Passed!”); If (grade >=0) Console.writeline(“Passed!”); else Console.writeline(“Failed!”); Conditional Operator Console.writeline(grade >= 60 ?“Passed!” : “Failed!”);
  • 5.
    Nested if (grade >=90) Console.writeline(“A”); elseif (grade >=80) Console.writeline(“B!”); … else Console.writeline(“F!”);
  • 6.
    Repetiton Structure -while While (condition){ statement 1; statement 2; statement 3; … }
  • 7.
    Example length = Convert.ToInt16(Console.ReadLine()); while(length > 0) { Console.Write("Enter Height of the Wall: "); height = Convert.ToInt16(Console.ReadLine()); PaintAWall thisWall = new PaintAWall(length, height, pricePerGal); thisWall.CalculateCost(ref paintCost,ref laborCost,ref galPaint, ref sqFt); Console.Write("Enter Length and Height for Wall # : “ + Convert.ToString(numWalls+1)); Console.Write("nEnter Length of the Wall (0 to quit): "); length = Convert.ToInt16(Console.ReadLine()); }
  • 8.
    Counter controlled vs Sentinelcontrolled A while loop, use LCV as a counter Counter =1 While (counter <=10) { … Counter ++ }//does it 10 times Sentinel controlled is good when one does not know exact number of times to execute a loop
  • 9.
    Explicitly and Implicitlyconverting between simple types Integer and integer division yields integer result. Suppose average is a floating point number: Average = total/num. Average will only get an integer if total and num are integers. int sum = 200, num = 3; float average; average = sum / num; Console.WriteLine(average); Will print 66
  • 10.
    Unary Cast Operator intsum = 200, num = 3; float av; av = (float) sum / num; Console.WriteLine(av); Will print 66.6666 Float/float or float/int or int/float will yield a float. C# implicitly promotes the one int to float.
  • 11.
    Nested control statements Seeexample of a multiplication table generation class MultiplicationTable{ static void Main(string[] args){ int i=2, j, k; while (i <= 12){ for (j = 1; j <= 10; j++){ Console.WriteLine(i + " x " + j + " = " + i * j ); } Console.WriteLine("n"); i++; } } } }
  • 12.
    Switch Statements Switch (expression){ case value1: //Statements break; case value 2: //Statements break; case value 3: //Statements case value n: //Statements break; default: //Statements }