3 c# control statements dr. john p. abraham professor utpa
TRANSCRIPT
![Page 1: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/1.jpg)
3 C# Control Statements
Dr. John P. AbrahamProfessor
UTPA
![Page 2: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/2.jpg)
Control statements
• Linear (sequential) program execution• Selection structure and repetition structure• Structured programming– Controlled entry and exit out of a module.– Avoid goto statements
![Page 3: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/3.jpg)
Selection structures in C#
• If – single selection statement• If..else – double selection statement• Swich – multiple – selection satement
![Page 4: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/4.jpg)
Examples:
if (grade >= 60) Console.writeline(“Passed!”);
If (grade >=0) Console.writeline(“Passed!”);
elseConsole.writeline(“Failed!”);
Conditional OperatorConsole.writeline(grade >= 60 ?“Passed!” : “Failed!”);
![Page 5: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/5.jpg)
Nested
if (grade >=90) Console.writeline(“A”);
else if (grade >=80) Console.writeline(“B!”);…else
Console.writeline(“F!”);
![Page 6: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/6.jpg)
Repetiton Structure - while
Read LCV (initialize)While (condition){
BlockRead LCV again (change value)
}
![Page 7: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/7.jpg)
Examplelength = 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()); }
![Page 8: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/8.jpg)
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
![Page 9: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/9.jpg)
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 av; av = sum / num; Console.WriteLine(av);Will print 66
![Page 10: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/10.jpg)
Unary Cast Operatorint sum = 200, num = 3;
float av; av = (float) sum / num; Console.WriteLine(av);Will print 66.6666Float/float or float/int or int/float will yield a
float.C# implicitly promotes the one int to float.
![Page 11: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/11.jpg)
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++; } } }}
![Page 12: 3 C# Control Statements Dr. John P. Abraham Professor UTPA](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f045503460f94c18e1e/html5/thumbnails/12.jpg)
Switch Statements