15
CSCI 3328 Object CSCI 3328 Object Oriented Programming in Oriented Programming in C# C# Chapter 5: C# Control Chapter 5: C# Control Statement – Part II – Statement – Part II – Exercises Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539 [email protected]

CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

Embed Size (px)

Citation preview

Page 1: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

CSCI 3328 Object Oriented CSCI 3328 Object Oriented Programming in C# Programming in C#

Chapter 5: C# Control Statement – Chapter 5: C# Control Statement – Part II – Exercises Part II – Exercises

1

Xiang Lian

The University of Texas Rio Grande Valley

Edinburg, TX 78539

[email protected]

Page 2: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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#

2

Page 3: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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

3

Page 4: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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

4

Page 5: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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.

5

Page 6: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

Recall: Example of Multiple Selection Statement

public void 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

}

}

6

Page 7: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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)

7

Page 8: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

What Does the Code Do?

public class Printing{

public static void Main(string[] args){

for (int i = 1; i<=10; i++){

for (int j = 1; j<=5; j++)Console.Write ('@');

Console.WriteLine();}

}}

8

Page 9: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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);

9

Page 10: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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")

}

10

Page 11: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

Debugging Errors (cont'd)

i=1

while (i <= 10);

++i;

}

---------------------------------------------------------

For (k=0.1; k!=1.0; k+=0.1)

Console.WriteLine(k);

11

Page 12: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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

12

** ** * ** * * ** * * * ** * * * * ** * * * * * ** * * * * * * ** * * * * * * * ** * * * * * * * * *

* * * * * * * * * ** * * * * * * * ** * * * * * * ** * * * * * ** * * * * ** * * * ** * * ** * ** **

* * * * * * * * * ** * * * * * * * *

* * * * * * * ** * * * * * *

* * * * * ** * * * *

* * * ** * *

* **

** *

* * ** * * *

* * * * ** * * * * *

* * * * * * ** * * * * * * *

* * * * * * * * ** * * * * * * * * *

Page 13: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

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

13

+ - - - - - - - - - - + | | | | | |+ - - - - - - - - - - +

Page 14: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

Exercises After the Class

• Chapter 6 in your textbook– Self-Review Exercises

– Quick Quiz

– Exercises

14

Page 15: CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley

15