lecture 2 review of 1301 and c# richard gesick. common data types int, long, double, float, char,...

14
Lecture 2 Review of 1301 and C# Richard Gesick

Upload: holly-lester

Post on 19-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

To declare a variable ; Example: int myNum; You can also initialize it when you delcare it: int myNum = 42; string myString="josh";

TRANSCRIPT

Page 1: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Lecture 2

Review of 1301 and C#

Richard Gesick

Page 2: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Common data types

int, long, double, float, char, bool, string

Page 3: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

To declare a variable <type> <name>;Example:int myNum;You can also initialize it when you delcare it:int myNum = 42;string myString="josh";

Page 4: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Console I/O Console.Write(X);Console.WriteLine(X);string A = Console.ReadLine();

Note that ReadLine() returns a string, so if you want to get something else from the user (and int for example), you would have to convert it. For 1302, you may assume the user will give valid input, but later in the course we will explore exception handling.

Page 5: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Conversion int myNum = Int32.Parse(Console.ReadLine());

This takes in input from the user (as a string) and then passes this string to the Parse method of the Int32 class; this Parse method returns an int, so this is a valid assignment since now the left and the right side of the assignment (=) operator are of the same type.

Page 6: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

User interactionstring name;Console.Write("What is your name? ");name = Console.ReadLine();Console.WriteLine("Hello " + name);

Notice in the above that the + operator acts as string concatenation.

Page 7: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

ConditionalsConsole.Write("How old are you? ");int age = Int32.Parse(Console.ReadLine());

if (age < 18) Console.WriteLine("You cannot drink or vote.");else if (age < 21) Console.WriteLine("You can vote but not drink.");else Console.WriteLine("You can vote and drink, but

don't vote shortly after drinking!");

Page 8: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

{ }Notice in the previous that we don't have to use curly-brackets to define the block in the if-else because we only want to control one line of code, but if you wanted to do more than one statement, use { } to mark the body of the if-else statement blocks.

Page 9: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Declaring Methods You declare a method in C# using the following pattern:<return type> <name> (<parameter(s)>){ <body of method>}

Page 10: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Method examples 1 a menu

void PrintMenu(){ Console.WriteLine("1. Display result"); Console.WriteLine("2. Add a number"); Console.WriteLine("3. Delete a number"); Console.WriteLine("4. QUIT");}

Page 11: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

Method examples 2 choice

int GetChoice(){ int choice; do { Console.Write("Enter your choice (1-4) : "); choice = Int32.Parse(Console.ReadLine()); } while ((choice < 1) || (choice > 4)); return choice;}

Page 12: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

for loops

for(<initialize>; <conditional>; <post-activity>){ <body/work>}

for ( int i=0; i < 100; i += 2){ Console.WriteLine(i);}

Page 13: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

do while loop

do { <body/work>} while (<conditional>);

int i = 10;do { Console.WriteLine(i); i--;} while (i > 0);

Page 14: Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool,…

while loop

while (<conditional>){ <body/work>}

int i = 0;while (i != 0){ Console.WriteLine(i); i -= 1;}