Lab 2 Getting Started With C#: The Objective of This Lab Is To Understand The Following Concepts
Lab 2 Getting Started With C#: The Objective of This Lab Is To Understand The Following Concepts
The C# simple types consist of the Boolean type and three numeric types - Integrals,
Floating Point, Decimal, and String. The term "Integrals" refers to the classification of types
that include sbyte, byte, short, ushort, int, uint, long, ulong, and char.
Boolean types are declared using the keyword, bool. They have two values: true or false,
which are official keywords.
In C#, an integral is a category of types. For anyone confused because the word Integral sounds
like a mathematical term, from the perspective of C# programming, these are actually defined
as Integral types in the C# programming language specification. They are whole numbers,
either signed or unsigned, and the char type. The char type is a Unicode character, as defined
by the Unicode Standard.
byte 8 0 to 255
ushort 16 0 to 65535
uint 32 0 to 4294967295
ulong 64 0 to 18446744073709551615
char 16 0 to 65535
A C# floating point type is either a float or double. They are used any time you need to
represent a real number, as defined by IEEE 754. Decimal types should be used when
representing financial or money values. Floating point types are used when you need to
perform operations requiring fractional representations.
The Floating Point and Decimal Types with Size, precision and Range
Type Size (in bits) precision Range
float 32 7 digits 1.5 x 10-45 to 3.4 x 1038
double 64 15-16 digits 5.0 x 10-324 to 1.7 x 10308
decimal 128 28-29 decimal 1.0 x 10-28 to 7.9 x 1028
places
A string is a string of text characters. You typically create a string with a string literal, enclosed
in quotes: "This is an example of a string."
using System;
Console.WriteLine("Hello World!");
int myInt = Int32.Parse( myString ); //Convert from string to the correct data type using
Int32.Parse()
Or
// convert from string to the correct data type using
Convert.ToInt32()
int myInt = Convert.ToInt32( myString );
Console.WriteLine(variableName) will print the variable. You can use the values of some
variables at some positions of a string:
Example 2.1
You can control the output format by using the format specifiers:
An if statement allows you to take different paths of logic, depending on a given condition.
Another form of selection statement is the switch statement, which executes a set of logic
depending on the value of a given parameter.
C# Branching Statements
Branching Description
statement
break Leaves the switch block
continue Leaves the switch block, skips remaining logic in enclosing loop, and goes back
to loop condition to determine if loop should be executed again from the
beginning. Works only if switch statement is in a loop.
goto Leaves the switch block and jumps directly to a label of the form
"<labelname>:"
return Leaves the current method.
throw Throws an exception.
class WhileLoop
{
public static void Main(string[] args)
{
int myInt = 0;
while (myInt < 10)
{
Console.Write("{0} ", myInt);
myInt++;
}
Console.ReadLine();
}
}
The do Loop
A do loop is similar to the while loop, except that it checks its condition at the end of the loop.
switch(myChoice)
{
case "A": case "a":
Console.WriteLine("You wish to add an address.");
break;
case "D": case "d":
A for loop works like a while loop, except that the syntax of the for loop includes initialization
and condition modification
class ForLoop
{
static void Main(string[] args)
{
for (int i=0; i < 20; i++)
{
if (i == 10)
break;
if (i % 2 == 0) continue;
Console.Write("{0} ", i);
}
Console.ReadLine();
}
}
One of the tools you can use is the Standard toolbar that is equipped with various debugging
buttons:
In some cases, you can suspend debugging. When this has happened, to resume debugging:
There are various debugging approaches available in Microsoft Visual Studio. Sometimes you
will want to suspend or stop debugging.
Just as done when reading code with your eyes, the most basic way to monitor code is to execute
one line at a time and see the results displayed before your eyes. To support this operation, the
debugger provides what is referred to as stepping into.
To execute code one line at a time, while the file that contains it is displaying:
The Step Into feature is a good tool to monitor the behavior of variables inside a method.. Instead
of executing one line at a time, the debugger allows you to execute a whole method at a time or
to execute the lines in some methods while skipping the others. To support this, you use a feature
named Step Over.
When executing a program, you can specify a section or line where you want the execution to
pause, for any reason you judge necessary.
To create a breakpoint, first identify the line of code where you want to add it. Then:
You can combine the Step Into and/or the Step Over feature with breakpoints. That is, you can
examine each code line after line until you get to a specific line. This allows you to monitor the
values of variables and see their respective values up to a critical section. To do this, first create
one or more breakpoints, then proceed with steps of your choice.
4.6.1 Locals
It automatically displays the list of variables within the scope of current methods. If your
debugger currently hits a particular breakpoint and if you open the "Autos" window, it will show
you the current scope object variable along with the value.
These variables are automatically detect by the VS debugger during the debugging. VisualStudio
determines which objects or variables are important for the current code statement and based on
that, it lists down the "Autos" variable.
Watch windows are used for adding variables as per requirement. It displays variables that you
have added. You can add as many variables as you want into the watch window. To add
variables in the watch window, you need to "Right Click" on variable and then select "Add
To You can also use Drag and Drop to add variables in watch windows or Add Quick
watch by pressing Shift+F9. If you want to delete any variable from watch window, just right
click on that variable and select "Delete Watch". From the debug window, you can also edit the
variable value at run time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sep18
class Program
highest = marks[k];
return highest;
lowest = marks[j];
return lowest;
}
static void Main(string[] args)
names[i] = Console.ReadLine();
marks[i] = int.Parse(Console.ReadLine());
enroll[i] = Console.ReadLine();
Console.Clear();
Console.WriteLine("\n");
Console.WriteLine("\n");
int hmarks = 0;
hmarks = highest(marks);
int lmarks = 0;
lmarks = lowest(marks);