Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 – Introduction to C# Programming

Similar presentations


Presentation on theme: "Chapter 3 – Introduction to C# Programming"— Presentation transcript:

1 Chapter 3 – Introduction to C# Programming
Outline Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts Arithmetic Decision Making: Equality and Relational Operators

2 3.1 Introduction Console applications No visual components
Only text output Two types MS-DOS prompt Used in Windows 95/98/ME Command prompt Used in windows 2000/NT/XP Windows applications Forms with several output types Contain Graphical User Interfaces (GUIs)

3 3.2 Simple Program: Printing a line of text
Comments Comments can be created using //… Multi-lines comments use /* … */ Comments are ignored by the compiler Used only for human readers Namespaces Groups related C# features into a categories Allows the easy reuse of code Many namespaces are found in the .NET framework library Must be referenced in order to be used White Space Includes spaces, newline characters and tabs

4 3.2 Simple Program: Printing a line of text
Keywords Words that cannot be used as variable or class names or any other capacity Have a specific unchangeable function within the language Example: class All keywords are lowercase Classes Class names can only be one word long (i.e. no white space in class name ) Class names are capitalized, with each additional English word capitalized as well (e.g., MyFirstProgram ) Each class name is an identifier Can contain letters, digits, and underscores (_) Cannot start with digits Can start with the at symbol

5 3.2 Simple Program: Printing a line of text
Class bodies start with a left brace ({) Class bodies end with a right brace (}) Methods Building blocks of programs The Main method Each console or windows application must have exactly one All programs start by executing the Main method Braces are used to start ({) and end (}) a method Statements Anything in quotes (“) is considered a string Every statement must end in a semicolon (;)

6 3.2 Simple Program: Printing a line of text
Graphical User Interface GUIs are used to make it easier to get data from the user as well as display data to the user Message boxes Within the System.Windows.Forms namespace Used to prompt or display information to the user

7 Welcome1.cs Program Output
1 // Fig. 3.1: Welcome1.cs 2 // A first program in C#. 3 4 using System; 5 6 class Welcome1 7 { static void Main( string[] args ) { Console.WriteLine( "Welcome to C# Programming!" ); } 12 } These are two single line comments. They are ignored by the compiler and are only used to aid other programmers. They use the double slash (//) This is a string of characters that Console.WriteLine instructs the compiler to output This is a blank line. It means nothing to the compiler and is only used to add clarity to the program. This is the using directive. It lets the compiler know that it should include the System namespace. This is the beginning of the Welcome1 class definition. It starts with the class keyword and then the name of the class. This is the start of the Main method. In this case it instructs the program to do everything Welcome1.cs Program Output Welcome to C# Programming!

8 3.2 Simple Program: Printing a Line of Text
Fig. 3.2 Visual Studio .NET-generated console application.

9 3.2 Simple Program: Printing a Line of Text
Fig. 3.3 Execution of the Welcome1 program.

10 Welcome2.cs Program Output
1 // Fig. 3.4: Welcome2.cs 2 // Printing a line with multiple statements. 3 4 using System; 5 6 class Welcome2 7 { static void Main( string[] args ) { Console.Write( "Welcome to " ); Console.WriteLine( "C# Programming!" ); } 13 } Console.WriteLine will pick up where the line ends. This will cause the output to be on one line even though it is on two in the code. Welcome2.cs Program Output Welcome to C# Programming!

11 Welcome3.cs Program Output
1 // Fig. 3.5: Welcome3.cs 2 // Printing multiple lines with a single statement. 3 4 using System; 5 6 class Welcome3 7 { static void Main( string[] args ) { Console.WriteLine( "Welcome\nto\nC#\nProgramming!" ); } 12 } The \n escape sequence is used to put output on the next line. This causes the output to be on several lines even though it is only on one in the code. Welcome3.cs Program Output Welcome to C# Programming!

12 3.2 Simple Program: Printing a Line of Text

13 Welcome4.cs Program Output
1 // Fig. 3.7: Welcome4.cs 2 // Printing multiple lines in a dialog Box. 3 4 using System; 5 using System.Windows.Forms; 6 7 class Welcome4 8 { static void Main( string[] args ) { MessageBox.Show( "Welcome\nto\nC#\nprogramming!" ); } 13 } The System.Windows.Forms namespace allows the programmer to use the MessageBox class. This will display the contents in a message box as opposed to in the console window. Welcome4.cs Program Output

14 3.2 Simple Program: Printing a Line of Text
Add Reference dialogue Fig. 3.8 Adding a reference to an assembly in Visual Studio .NET (part 1).

15 3.2 Simple Program: Printing a Line of Text
References folder Solution Explorer System.Windows.Forms reference Fig. 3.8 Adding a reference to an assembly in Visual Studio .NET (part 2).

16 3.2 Simple Program: Printing a Line of Text
Text field Menu Button Label Menu bar Fig. 3.9 Internet Explorer’s GUI.

17 3.2 Simple Program: Printing a Line of Text
OK button allows the user to dismiss the dialog. Dialog is automatically sized to accommodate its contents. Mouse cursor Close box Fig Dialog displayed by calling MessageBox.Show.

18 3.3 Another Simple Program: Adding Integers
Primitive data types Data types that are built into C# String, Int, Double, Char, Long 15 primitive data types (chapter 4) Each data type name is a C# keyword Same type variables can be declared on separate lines or on one line Console.ReadLine() Used to get a value from the user input Int32.Parse() Used to convert a string argument to an integer Allows math to be preformed once the string is converted

19 This is the start of class Addition
1 // Fig. 3.11: Addition.cs 2 // An addition program. 3 4 using System; 5 6 class Addition 7 { static void Main( string[] args ) { string firstNumber, // first string entered by user secondNumber; // second string entered by user 12 int number1, // first number to add number2, // second number to add sum; // sum of number1 and number2 16 // prompt for and read first number from user as string Console.Write( "Please enter the first integer: " ); firstNumber = Console.ReadLine(); 20 // read second number from user as string Console.Write( "\nPlease enter the second integer: " ); secondNumber = Console.ReadLine(); 24 // convert numbers from type string to type int number1 = Int32.Parse( firstNumber ); number2 = Int32.Parse( secondNumber ); 28 // add numbers sum = number1 + number2; 31 This is the start of class Addition Two string variables defined over two lines The comment after the declaration is used to briefly state the variable purpose Addition.cs These are three ints that are declared over several lines and only use one semicolon. Each is separated by a coma. This line is considered a prompt because it asks the user to input data. Int32.Parse is used to convert the given string into an integer. It is then stored in a variable. The two numbers are added and stored in the variable sum. Console.ReadLine is used to take the users input and place it into a variable.

20 Addition.cs Program Output
// display results Console.WriteLine( "\nThe sum is {0}.", sum ); 34 } // end method Main 36 37 } // end class Addition Putting a variable out through Console.WriteLine is done by placing the variable after the text while using a marked place to show where the variable should be placed. Addition.cs Program Output Please enter the first integer: 45 Please enter the second integer: 72 The sum is 117.

21 3.4 Memory Concepts Memory locations
Each variable is a memory location Contains name, type, size and value When a new value is enter the old value is lost Used variables maintain their data after use

22 3.4 Memory Concepts number1 45
Fig Memory location showing name and value of variable number1.

23 Arithmetic operations
Not all operations use the same symbol Asterisk (*) is multiplication Slash (/) is division Percent sign (%) is the modulus operator Plus (+) and minus (-) are the same Must be written in a straight line There are no exponents Division Division can vary depending on the variables used When dividing two integers the result is always rounded down to an integer To be more exact use a variable that supports decimals

24 3.5 Arithmetic Order Parenthesis are done first
Division, multiplication and modulus are done second Left to right Addition and subtraction are done last

25 3.4 Memory Concepts number1 45 number2 72
Fig Memory locations after values for variables number1 and number2 have been input.

26 3.4 Memory Concepts number1 45 number2 72 sum 117
Fig Memory locations after a calculation.

27 3.5 Arithmetic x y

28 3.5 Arithmetic

29 3.5 Arithmetic Step 1. Step 2. Step 5. Step 3. Step 4. Step 6. y = 2 * 5 * * 5 + 7; 2 * 5 is (Leftmost multiplication) y = 10 * * 5 + 7; 10 * 5 is (Leftmost multiplication) y = * 5 + 7; 3 * 5 is (Multiplication before addition) y = ; is (Leftmost addition) y = ; is (Last addition) y = 72; (Last operation—place 72 into y) Fig Order in which a second-degree polynomial is evaluated.

30 3.6 Decision Making: Equality and Relational Operators
The if structure Used to make a decision based on the truth of the condition True: a statement is performed False: the statement is skipped over The start of an if statement should not end in a semicolon (;) Fig lists the equality and rational operators There should be no spaces separating the operators

31 3.6 Decision Making: Equality and Relational Operators

32 If number1 is the same as number2 this line is preformed
1 // Fig. 3.19: Comparison.cs 2 // Using if statements, relational operators and equality 3 // operators. 4 5 using System; 6 7 class Comparison 8 { static void Main( string[] args ) { int number1, // first number to compare number2; // second number to compare 13 // read in first number from user Console.Write( "Please enter first integer: " ); number1 = Int32.Parse( Console.ReadLine() ); 17 // read in second number from user Console.Write( "\nPlease enter second integer: " ); number2 = Int32.Parse( Console.ReadLine() ); 21 if ( number1 == number2 ) Console.WriteLine( number1 + " == " + number2 ); 24 if ( number1 != number2 ) Console.WriteLine( number1 + " != " + number2 ); 27 if ( number1 < number2 ) Console.WriteLine( number1 + " < " + number2 ); 30 if ( number1 > number2 ) Console.WriteLine( number1 + " > " + number2 ); 33 Comparison.cs Combining these two methods eliminates the need for a temporary string variable. If number1 is the same as number2 this line is preformed If number1 does not equal number2 this line of code is executed. If number1 is less than number2 the program will use this line If number1 is greater than number2 this line will be preformed

33 Comparison.cs Program Output
if ( number1 <= number2 ) Console.WriteLine( number1 + " <= " + number2 ); 36 if ( number1 >= number2 ) Console.WriteLine( number1 + " >= " + number2 ); 39 } // end method Main 41 42 } // end class Comparison If number1 is less than or equal to number2 then this code will be used Comparison.cs Program Output Lastly if number1 is greater than or equal to number2 then this code will be executed Please enter first integer: 2000 Please enter second integer: 1000 2000 != 1000 2000 > 1000 2000 >= 1000 Please enter first integer: 1000 Please enter second integer: 2000 1000 != 2000 1000 < 2000 1000 <= 2000 Please enter first integer: 1000 Please enter second integer: 1000 1000 == 1000 1000 <= 1000 1000 >= 1000

34 3.6 Decision Making: Equality and Relational Operators


Download ppt "Chapter 3 – Introduction to C# Programming"

Similar presentations


Ads by Google