130 likes | 328 Views
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises. UTPA – Fall 2012. Objectives. In this chapter, you will do some exercises about: basic control structures in Visual C#. Precedence of Operators. . new ++ (postfix) -- (postfix)
E N D
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012
Objectives • In this chapter, you will do some exercises about: • basic control structures in Visual C#
Precedence of Operators • . new ++ (postfix) -- (postfix) • ++ -- + - (unary) • * / % • + - • < <= > >= • == != • ? : • = += -= *= /= %= high low
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-controlled • 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
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
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.
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.
What are the Precedence of Operators in Expressions? • 15*3%12+45/(9%2)*-2 • Product *= x++;
Debug Errors public static main(string []args) { Int x=0; x=y; X=15*3%12+45/(8%2)*-2; WriteLine(x); }
Debug Errors (cont'd) public static main(string []args) { if (gender = 1) Console.WriteLine("Woman"); else; Console.WriteLine("Man"); }
Debug Errors (cont'd) public static main(string []args) { int counter = 0; While (counter > 10) { Console.WriteLine(counter); return; }
Programming • Write 4 different C# statements that each add 1 to int variable x.
Programming (cont'd) • Write a console program using nested while loop to output the following pattern to screen: * ** *** **** *****