0% found this document useful (0 votes)
122 views

Lab 01

The document provides an overview of basic C# programming concepts including: - Identifier naming rules and examples of valid and invalid names - The basic structure of a C# program with namespaces, classes, and the Main method - Common data types in C# like int, float, string - How to declare variables and constants, including examples - Arithmetic expressions and operators in C# and precedence rules - Using output statements like Console.Write and Console.WriteLine
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Lab 01

The document provides an overview of basic C# programming concepts including: - Identifier naming rules and examples of valid and invalid names - The basic structure of a C# program with namespaces, classes, and the Main method - Common data types in C# like int, float, string - How to declare variables and constants, including examples - Arithmetic expressions and operators in C# and precedence rules - Using output statements like Console.Write and Console.WriteLine
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

204111 Computers and Programming

Section 450

Lab #1 C# Basic
Student ID Sheets Owner Group partner Name Signature

1. Identier Naming Rules in C#


A name must consist of only letters (AZ,az), digits (09), or underscores ( ) The rst character must be either a letter or an underscore A name can be at most 63 characters in length A name must not be identical to a reserved word such as class, namespace, int, void,

static

Exercise 1.1: Consider the following names and check if each of them is a valid name for a C# identier Identier Name XXX $$$ Y string ij Student ID HelloWorld!! first-time null 204111Class Section3 sECTION3 w*h do Valid in C#? yes no Reason not violating the naming rules consists of special characters $

2. Basic Structure of C# Programs


Here is an example of C# program structure.

namespace ___ ( A ) ___ { class ___ ( B ) ___ { static void Main () {


___ ( C ) ___ } } }

Lab #1

1 of 8

204111 Computers and Programming

Section 450

From the above, the positions (A), (B), and (C) have the following meanings: (A) is for the namespaces name (B) is for the classs name (C) is for statements telling the computer what to do For simple programs, the namespace part can even be omitted as follows.

class ___ ( B ) ___ { static void Main () {


___ ( C ) ___ } } Exercise 2.1: Complete the following tasks: Write a program that can compile and run. Your program must meet these requirements: namespace has the name TestingNamespace class has the name TestingClass The program outputs no results

Write another program that can also compile and run, but now it must meet these requirements: There is no namespace class has the name SayHello The program outputs a phrase Hello Section 3

Lab #1

2 of 8

204111 Computers and Programming

Section 450

3. Data Types in C#
C# provides a number of data types for storing dierent kinds of data. Some commonly used types are listed in the table below. Type

char bool byte int uint long ulong float double string

Description Single character Truth value true or false Unsigned integer between 0 and 255 Signed integer between -2,147,483,648 and 2,147,483,647 Unsigned integer between 0 and 4,294,967,295 Signed integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 Unsigned integer between 0 and 18446744073709551615 Real number Double-precision real number Sequence of characters

4. Variables and Constants


A variable is an identier used to represent data that can be changed while the program is running. In C#, a variable must be declared with a specic type as follows: DataType variableName ; where variableName is the name of the variable, and DataType species the type of data the variable can store. In addition, a variable can be declared along with its initial value. For this case, the declaration is of the form: DataType variableName = initialValue ; where initialValue indicates the initial value of the variable variableName. Example 4.1: Declare a variable distance to be of type uint without an initial value

uint myvar ;
Example 4.2: Declare a variable salary to be of type long with an initial value 30000

long myvar = 30000;


Exercise 4.1: Declare a variable x to be of type float without an initial value

"Peter"

Exercise 4.2: Declare a variable myName to be of type string with an initial value

Lab #1

3 of 8

204111 Computers and Programming

Section 450

A constant is an identier used to represent data that can be used throughout the program. Unlike variables, the value of a constant is xed and cannot be changed. Constants are useful when we want to use the same value over and over again, but we dont want to write out that value every time it is referred to. In C#, a constant must be declared with a specic type, an initial value, and a keyword const. DataType constantName = value ; where constantName is the name of the constant, DataType indicates the type of data the constant represents, and value species the value of the constant. Example 4.3: Declare a constant myconst to be of type double and have a xed value of 2.71828

const double myconst = 2.71828;


Exercise 4.3: Declare a constant PI to be of type double and have a xed value of

3.1415926535

Example 4.4: More examples of variable and constant declaration. Note that multiple variables (or constants) of the same type can be declared at the same time by separating them with commas.

const int FreezingPoint = 32; int x , y ; int wd = 5 , ht = 8; const double PI = 3.1424; char ch = A ; string mynote = " Hello , Kitty " ; int j = 5;
Exercise 4.4: Create a new le in SharpDevelop and type the following source code

using System ; class CircleArea { static void Main () { const double PI = 3.1415926535;
radius = 12.5; area = PI * radius * radius ; Console . WriteLine ( " Circle area = {0} " , area ); } }

Lab #1

4 of 8

204111 Computers and Programming

Section 450

Then try to compile the program. What errors are reported, and at which lines? What do you think is the cause?

Fix the above code so that it compiles and runs. How did you x the errors?

Exercise 4.5: Create a new le in SharpDevelop and type the following source code

using System ; class MoreExercise { static void Main () { int f = 1.5; char s = " Harry Potter " ; char c = A ;
Console . WriteLine ( " f value = {0} , s = {1} , c = {2} " , f , s , c ); } } Fix the above code by changing only data types in the variable declarations, so that it compiles and runs. How did you x the errors?

Lab #1

5 of 8

204111 Computers and Programming

Section 450

Exercise 4.6: For each of the data item given below, choose an appropriate data type and write a C# statement to declare the specied variable Variable myAge to store your own age

Variable income to keep track of Peters personal income

Variable temp c to store temperature in degree Celcius

Variable temp k to store temperature in Kelvin

Variable name to store Aums full name

5. Arithmetic Expressions
An expression is a single term or a combination of terms that can be evaluated to another value. Below are examples of single-term expressions: A number, such as 3000, 1.414 A string literal, such as "Hello, World" A boolean keyword, which can be true and false A single variable or constant, such as myName, salary In this lesson, we will mainly focus on those expressions that evaluate to numerical values. These expressions are called arithmetic expressions. C# provides a collection of arithmetic operators that allow us to build a more complex expressions from simpler ones. Arithmetic operators in C# are as follows:

Lab #1

6 of 8

204111 Computers and Programming

Section 450

Operator Add Subtract Multiply Divide Modulo Negate Group

Symbol + * / % ()

Type Binary Binary Binary Binary Binary Unary N/A

Example x+4 32-x x*2 x/2 x/6 -x (x+2)*3

Value when x = 20 24 12 40 10 2 -20 66

When more than one arithmatic operators are used within the same expression, C# evaluates the expression in specic order, according to the following precedence rules: 1. ( ) 2. *, / and % 3. + and 4. For equal precedence, evaluate from left to right Exercise 5.1: Create a new le in SharpDevelop and type the following source code 1 using System ; 2 class Test { 3 static void Main () { 4 double x = 3.0 , y = 2.0; 5 int a = 10 , b = 2; 6 double tmp = a + b + x + y ; 7 Console . WriteLine ( tmp ); 8 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ 9 Console . ReadLine (); 10 } 11 } Notice that line 6 is blank. Now ll the blank with each of the statements listed in the table below, one at a time. Run the program and put the last result in the right column. Statement Console.WriteLine(a); Console.WriteLine(x+a); Console.WriteLine(a/b); Console.WriteLine(y/x); Console.WriteLine(y%x); Console.WriteLine((a+b)/b%a); Console.WriteLine(9.0/5.0*(a-x)); Console.WriteLine(x+y-x*y%x); Console.WriteLine(57%50/25); Result 10

Lab #1

7 of 8

204111 Computers and Programming

Section 450

6. Output Statements
Exercise 6.1: (Basic usage of Console.Write and Console.WriteLine) Write a C# program that outputs the following result:

Live as if you were to die tomorrow. Learn as if you were to live forever. -Mahatma Gandhi Then copy the code you wrote in SharpDevelopinto the box below.

Exercise 6.2: (Advanced usage of Console.Write and Console.WriteLine) Consider the following incomplete code: 1 using System ; 2 3 class SayHi { 4 static void Main () { 5 string yourName = ___ ( a ) ___ ; 6 uint yourAge = ___ ( b ) ___ ; 7 Console . WriteLine ( " Hello {1}. 8 ___ ( c ) ___ , ___ ( d ) ___ ); 9 } 10 }

You are {0} years old . " ,

Fill in the blanks (1),..., (4) so that the program will say hello to you and print out your own age. For example, if your name is Arthur and you are 18 years old, the result should be: Hello Arthur. You are 18 years old.

Now copy what you lled in the blanks into this table. Position __(a)__ __(b)__ __(c)__ __(d)__ Your answer

Lab #1

8 of 8

You might also like