Successfully reported this slideshow.
Upcoming SlideShare
×

# Lesson2

579 views

Published on

Learn C#

Published in: Engineering
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Lesson2

1. 1. Lesson 2 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-2
2. 2. Agenda • Arrays • Conditional statements: if, if-else, switch • Loops: for, foreach, while and do…while
3. 3. One-dimensional arrays • An array is a special variable, which can hold more than one value at a time int[] a = new int[6]; a[0] = 12; a[1] = 2; a[2] = 1; a[3] = 232; a[4] = 16; a[5] = 99; Console.WriteLine(a[5]);//prints '99'
4. 4. if and if-else statements Condition Previous calculations Some calculations Some calculations True False Other program calculations Condition Previous calculations Some calculations True Other programs calculations ifIf-else
5. 5. switch statement expression code block 1 code block 2 code block 3 code block N case 1 case 1 case 1 default Console.WriteLine("Enter girl's name:"); string girlName = Console.ReadLine(); switch (girlName) { case "Kate": Console.WriteLine("Just ignore her."); break; case "Samanta": Console.WriteLine("Give her your phone number!"); break; case "Melissa": Console.WriteLine("She has a birthday pretty soon!"); break; default: Console.WriteLine("Sorry, I don't know {0}", girlName); break; } Console.WriteLine("Enter girl's name:"); string girlName = Console.ReadLine(); if (girlName == "Kate") { Console.WriteLine("Just ignore her."); } else if (girlName == "Samanta") { Console.WriteLine("Give her your phone number!"); } else if (girlName == "Melissa") { Console.WriteLine("She has a birthday pretty soon!"); } else { Console.WriteLine("Sorry, I don't know {0}", girlName); }
6. 6. Loop operators: while, do-while Condition Previous calculations body True False Other calculations Condition Previous calculations body True False Other calculations int moneyInWallet = 100; int iPhonePrice = 499; while (moneyInWallet < iPhonePrice) { //keep on making money moneyInWallet = moneyInWallet + 1; } while int moneyInWallet = 0; int iPhonePrice = 499; do { //keep on making money moneyInWallet++; //increment } while (moneyInWallet > iPhonePrice); do-while
7. 7. Loop operators: for Condition initialization body True False Counter change Other calculations for (int index = 1; index < 10; index++) {//Body Console.WriteLine("Number {0}", index); }
8. 8. if and for demo for condition initialization True False Counter change Other calculations If condition Some actions True False for (int index = 0; index <= 10; index++) { if (index%2 == 0) { Console.WriteLine(\$"Number {index} in an even number"); } }
9. 9. Loops operators • break • continue
10. 10. Thank you! Questions?