1 / 15

Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa

CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises. Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu. Objectives. In this chapter, you will do exercises about: more control structures Repetition statements

teagued
Download Presentation

Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

  2. Objectives • In this chapter, you will do exercises about: • more control structures • Repetition statements • for, do … while • Selection • Switch • break and continue statements • logical operators in C#

  3. Multiple Choices • Typically, ______ statements are used for counter-controlled repetition. • A. for B. while C. repetition D. until • Typically, ______ statements are used for sentinel-controlled repetition. • A. for B. while C. repetition D. until • The do … while statement tests the loop-continuation condition ________ executing the loop’s body; therefore, the body always executes at least once. • A. before B. after C. at the same time D. None of the above • The _________statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. • A. break B. exit C. continue D. return

  4. Multiple Choices (cont'd) • The _____operator can be used to ensure that two conditions are both true before choosing a certain path of execution • A. and B. && C. or D. || • If the loop continuation condition in a for header is initially ___________, the for statement’s body does not execute • A. true B. false C. 0 D. 1 • Which of the following is the appropriate for statement for varying the control variable over the following sequence of values: 25, 20, 15, 10, 5? • A. for (i = 5; i<25; i+=5) B. for (i = 5; i<=25; i-=5) • C. for (i = 25; i<=5; i-=5) D. for (i = 25; i>=5; i-=5) • An infinite loop occurs when the loop-continuation condition in a while or do…while statement______. • A. never becomes true B. never becomes false C. is false D. is true for finite times

  5. True/False Statements • The default label is required in the switch selection statement. • The break statement is required in every case of a switch statement • The expression ((x>y)&&(a<b)) is true if either (x>y) is true or (a<b) is true. • An expression containing the || operator is true if either or both of its operands are true. • The integer after the comma (,) in a format item (e.g., {0, 4}) indicates the field width of the displayed string. • To test for a range of values in a switch statement, use a hyphen (-) between the start and end values of the range in a case label.

  6. Recall: Example of Multiple Selection Statement publicvoid CalculateGrade(int grade) { switch (grade/10) { case 9: // 90-99 case 10: // 100 Console.WriteLine("Grade: A"); break; case 8: Console.WriteLine("Grade: B"); break; // 80-89 case 7: Console.WriteLine("Grade: C"); break; // 70-79 case 6: Console.WriteLine("Grade: D"); return; // 60-69 default: Console.WriteLine("Grade: F"); break; // < 60 } }

  7. True/False Statements (cont'd) • Listing cases consecutively with no statements between them enables the cases to perform the same set of statements. • Boolean logical operator (|) performs short-circuit evaluation. • The && operator has a higher precedence than the || operator. • The following statement alters the control variable from 0 to 50 in increment of 5? • for (int i=0;i<49; i+=5)

  8. What Does the Code Do? public class Printing { public static void Main(string[] args) { for (inti = 1; i<=10; i++) { for (int j = 1; j<=5; j++) Console.Write ('@'); Console.WriteLine(); } } }

  9. What Does the Code Do? (cont'd) // What is the output of the following code? int x=1; int y=2; if ( x == 2 && y=4) Console.WriteLine ("expression is true"); else Console.WriteLine ("expression is false"); Console.WriteLine ("y="+y);

  10. Debugging Errors • The following code is to display whether integer value is odd or even Switch( value%2) { case 0; Console.WriteLine("Odd integer"); case 1: Console.Writeline("Even integer") }

  11. Debugging Errors (cont'd) i=1 while (i <= 10); ++i; } --------------------------------------------------------- For (k=0.1; k!=1.0; k+=0.1) Console.WriteLine(k);

  12. Write a Program • Use nested for statement to write a program to display the triangle of asterisks in outputTextBox. Use the following statements: • outputTextBox.AppendText("*"):displays asterisks one at a time • outputTextBox.AppendText(Environment.NewLine) • outputTextBox.AppendText(" "): inserts a space * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  13. Write a Program (cont'd) • Use nested for statement to write a program to display a rectangle in outputTextBox. Use the following statements: • outputTextBox.AppendText("+") • outputTextBox.AppendText(Environment.NewLine) • outputTextBox.AppendText(" "): inserts a space + - - - - - - - - - - + | | | | | | + - - - - - - - - - - +

  14. Exercises After the Class • Chapter 6 in your textbook • Self-Review Exercises • Quick Quiz • Exercises

More Related