Tuesday, January 14, 2014

Outline

4.1 Introduction

4.2 Algorithms

4.3 Pseudocode

4.4 Control Structures

4.5 if Selection Structure

4.6 if/else Selection Structure

4.7 while Repetition Structure

4.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition)

4.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition)

4.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case
Study 3 (Nested Control Structures)

4.11 Assignment Operators

4.12 Increment and Decrement Operators

4.13 Introduction to Windows Application Programming
•   A console application is an application that, predominantly, displays text output in either a console
window (or MS-DOS window). This is also called a command prompt.

•   Programmers insert comments to document programs and improve program readability. Every
program should begin with a comment describing the purpose of the program.

•   A comment that begins with // is called a single-line comment, because the comment terminates
at the end of the current line. A // comment can begin in the middle of a line and continue until
that line’s end. Multiple-line comments begin with the delimiter /* and end with delimiter */.
The compiler ignores all text between the delimiters of the comment.

•   A namespace groups various C# features into related categories, providing programmers with the
ability to locate them quickly.

This  section introduces C#’s if  structure,  which allows a program  to make a decision
based on the truth or falsity of some condition. If the condition is met (i.e., the condition is
true), the statement in the body of the if structure executes. If the condition is not met (i.e.,
the condition is false), the body statement does not execute. Conditions in if structures can
be  formed  by  using  the  equality  operators  and  relational  operators,  summarized  in
Fig. 3.18. The relational operators all have the same level of precedence and associate from
left to right. The equality operators both have the same level of precedence, which is lower
than the precedence of the relational operators. The equality operators also associate from
left to right.

          Common Programming Error 3.7
It is a syntax error if the operators ==, !=, >= and<=  contain spaces between their symbols
(as in = =, ! =, > =, < =).                                                                                             
     
         Common Programming Error 3.8
Reversing the operators !=, >= and <= (as in =!, => and =<) is a syntax error.     


Most programs perform arithmetic calculations. Figure 3.15 summarizes the arithmetic op-
erators. Note the use of various special symbols not used in algebra. The asterisk (*) indi-
cates multiplication, and the percent sign (%) represents the modulus operator, which is
discussed shortly. The arithmetic operators in Fig. 3.15 are binary operators, because they
each require two operands. For example, the expression sum + value contains the binary
operator + and the two operands sum and value.



Variable names, such as number1, number2 and sum, actually correspond to locations
in the computer’s memory. Every variable has a name, a type, a size and a value.
     In the addition program in Fig. 3.11, the statement (line 26)

                number1 = Int32.Parse( firstNumber );

converts to an int the string that the user entered. This int is placed into a memory loca-
tion to which the name number1 has been assigned by the compiler. Suppose the user en-
ters the string 45 as the value for firstNumber. The program converts firstNumber
to an int, and the computer places the integer value 45 into location number1, as shown
in Fig. 3.12.
     When a value is placed in a memory location, this value replaces the previous value in
that location. The previous value is lost (or destroyed).
         Our next application (Fig. 3.11) inputs two integers (whole numbers) typed by a user at the
keyboard, computes the sum of these values and displays the result. As the user types each
integer and presses the Enter key, the integer is read into the program and added to the total.
Lines 1–2 are single-line comments stating the figure number, file name and purpose of the
program.
        As stated previously, every C# program consists of at least one class definition. Line
6 begins the definition of class Addition. Lines 7–37 define the body of the class. Recall
that all class definitions start with an opening left brace ({) and end with a closing right
brace (}).

 C# uses some notations that might appear strange to non programmers. We begin by con-
side ring a simple program that displays a line of text. The program and its output are shown
in Fig. 3.1. The program is followed by an output window that displays the program’s 
results. When you execute this program, the output will appear in a console window.

Subscribe to RSS Feed Follow me on Twitter!