CSCI 3328 Object Oriented Programming in C Chapter

  • Slides: 13
Download presentation
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part

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

Objectives • In this chapter, you will do some exercises about: – basic control

Objectives • In this chapter, you will do some exercises about: – basic control structures in Visual C# 2

Precedence of Operators • • . new ++ (postfix) -- (postfix) ++ -- +

Precedence of Operators • • . new ++ (postfix) -- (postfix) ++ -- + - (unary) * / % + < <= > >= == != ? : = += -= *= /= %= high low 3

Multiple Choices • Which of the following is not an attribute of an airplane?

Multiple Choices • Which of the following is not an attribute of an airplane? – A. length B. wingspan C. fly D. number of seats • All applications can be written in terms of three types of control structures except for: – A. sequence B. selection C. random D. repetition • The ____ statement is used to execute one action when a condition is true and another when that condition is false. – A. repetition B. selection C. inheritance D. conditional • Repeating a set of instructions a specific number of times is called _______ repetition. – A. EOF-controlled B. flag-controlled C. counter-controlled D. sentinel- • When it is not known in advance how many times a set of instructions will be repeated, a(n) ____ value cannot be used to terminate the repetition. – A. flag B. signal C. counter D. sentinel 4

Multiple Choices (cont'd) • The _______ structure is built into C# -- by default,

Multiple Choices (cont'd) • The _______ structure is built into C# -- by default, statements execute in the order they appear – A. sequence B. selection C. repetition D. goto • Instance variables of type int are given the value ______ by default. – A. 0 B. NULL C. "" D. 1 • C# is a ______ language – it requires all variables to have a type. – A. strongly-typed B. weak-typed C. wield-typed D. object-typed • If the increment operator is _____ to a variable, the variable is incremented by 1 first, then its new value is used in the expression – A. prefixed B. postfixed C. infixed D. None of the above • The expression to the right of an assignment operator (=) is evaluated ______the assignment occurs. – A. before B. after C. at the same time D. none of the above 5

True/False Statements • An algorithm is a procedure for solving a problem in terms

True/False Statements • An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute. • A set of statements contained within a pair of parentheses is called a block. • A selection statement specifies that an action is to be repeated while some condition remains true. • A nested control statement appears in the body of another control statement. 6

True/False Statements (cont'd) • C# provides the arithmetic compound assignment operators +=, -=, *=,

True/False Statements (cont'd) • C# provides the arithmetic compound assignment operators +=, -=, *=, /=, and %= for abbreviating assignment expressions. • Specifying the order in which statements (actions) execute in an application is called program control. • The unary cast operator (double) creates a temporary integer copy of its operand. • Instance variables of type bool are given the value true by default • Pseudo code helps you think out an application before attempting to write it in a programming language. 7

What are the Precedence of Operators in Expressions? • 15*3%12+45/(9%2)*-2 • Product *= x++;

What are the Precedence of Operators in Expressions? • 15*3%12+45/(9%2)*-2 • Product *= x++; 8

Debug Errors public static main(string []args) { Int x=0; x=y; X=15*3%12+45/(8%2)*-2; Write. Line(x); }

Debug Errors public static main(string []args) { Int x=0; x=y; X=15*3%12+45/(8%2)*-2; Write. Line(x); } 9

Debug Errors (cont'd) public static main(string []args) { if (gender = 1) Console. Write.

Debug Errors (cont'd) public static main(string []args) { if (gender = 1) Console. Write. Line("Woman"); else; Console. Write. Line("Man"); } 10

Debug Errors (cont'd) public static main(string []args) { int counter = 0; While (counter

Debug Errors (cont'd) public static main(string []args) { int counter = 0; While (counter > 10) { Console. Write. Line(counter); return; } 11

Programming • Write 4 different C# statements that each add 1 to int variable

Programming • Write 4 different C# statements that each add 1 to int variable x. 12

Programming (cont'd) • Write a console program using nested while loop to output the

Programming (cont'd) • Write a console program using nested while loop to output the following pattern to screen: * ** ***** 13