review of lecture 1 - kolosovpetro.github.io · operators 0 assignment 0 rzutowanie int x = 23; x =...

24

Upload: others

Post on 24-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x
Page 2: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Review of Lecture 1

0 Variables

0 Basic types

0 Basic interaction with user

Page 3: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Outline

0 Operators

0 Conditional statements

0 Loops

0 Excercises

Page 4: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Operators

0 Assignment

0 Rzutowanie

int x = 23; x = x + 1;

float f = 23; int i = (int)f;

Assignment returns a value! int y = x = x + 5;

Page 5: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Operators

0 Arithmetic

0 String concatenation "str1" + "str2"

z = x + y; z = x - y; z = x * y; z = x / y; z = x % y; x++; x--; ++x; --x;

string a = "Bond, "; Console.WriteLine(a + "James Bond");

Page 6: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Operator

0 Comparison

0 Logical and conditional logical

x > y x < y x >= y x <= y x == y x != y

p & q p | q p ^ q p && q p || q !p

Page 7: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Operators

0 Assignment cont.

x += y; //x = x + y; x -= y; //x = x - y; x *= y; //x = x * y; x /= y; //x = x / y; x %= y; //x = x % y; p &= q; //p = p & q; p |= q; //p = p | q; p ^= q; //p = p ^ q;

Page 8: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements - if

if (logical condition) { instructions... }

Page 9: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements - if

int points = int.Parse(Console.ReadLine()); if (points > 5) { Console.WriteLine("Congratulations! You passed!"); } Console.WriteLine("Your score is: " + points);

{} = block of code

Page 10: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements - if

int points = int.Parse(Console.ReadLine()); if (points > 5) { Console.WriteLine("Congratulations! You passed!"); } else { Console.WriteLine("Unfortunately, you didn’t pass."); } Console.WriteLine("Your score is: " + points);

Page 11: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements - if

int points = int.Parse(Console.ReadLine()); if (points == 10) { Console.WriteLine("Great score!!!"); } else if (points > 5) { Console.WriteLine("Congratulations! You passed!"); } else { Console.WriteLine("Unfortunately, you didn’t pass."); } Console.WriteLine("Your score is: " + points);

Page 12: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements switch

switch (logical condition) { case value 1: instructions... break; case value 2: instructions... break; ... default: instructions... break; }

Page 13: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements switch

int points = int.Parse(Console.ReadLine()); Console.Write("Your grade: "); switch (points) { case 10: Console.WriteLine("Excellent"); break; case 9: Console.WriteLine("Very good"); break; case 8: Console.WriteLine("Good"); break; case 7: Console.WriteLine("Satisfactory"); break; case 6: Console.WriteLine("Sufficient"); break; default: Console.WriteLine("Fail"); break; }

Page 14: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Conditional statements switch

Console.Write("Select an option: n: new game, s: settings, q: quit > "); string option = Console.ReadLine(); switch (option) { case "n": Console.WriteLine("Let’s start the game!"); break; case "s": Console.WriteLine("Settings..."); break; case "q": Console.WriteLine("Bye bye!"); break; default: Console.WriteLine("There’s no such option!"); break; }

Page 15: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – while

while (logical condition) { instructions... }

Page 16: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – while

int l = 5; while (l > 0) { Console.WriteLine(l); l--; }

Page 17: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – while

Console.WriteLine("Welcome to my division program!"); Console.Write("Numerator: "); int numerator = int.Parse(Console.ReadLine()); int denumerator = 0; while (denumerator == 0) { Console.Write("Denumerator: "); denumerator = int.Parse(Console.ReadLine()); } Console.WriteLine("Result: " + ((float)numerator / denumerator));

Page 18: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – while

Console.WriteLine("Welcome to my division program!"); Console.Write("Numerator: "); int numerator = int.Parse(Console.ReadLine()); int denumerator = 0; do { Console.Write("Denumerator: "); denumerator = int.Parse(Console.ReadLine()); } while (denumerator == 0); Console.WriteLine("Result: " + ((float)numerator / denumerator));

Page 19: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

TryParse

int variable = 0; bool success = int.TryParse("text to parse", out variable);

Page 20: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – while

Console.WriteLine("Welcome to my division program!"); Console.Write("Numerator: "); int numerator = int.Parse(Console.ReadLine()); int denumerator = 0; do { Console.Write("Denumerator: "); } while (!int.TryParse(Console.ReadLine(), out denumerator) || denumerator == 0); Console.WriteLine("Result: " + ((float)numerator / denumerator));

Page 21: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – for

for (initialization; logical condition; update) { instructions... }

Page 22: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – for

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

Page 23: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Loops – for

Console.Write("How many numbers should I write: "); int numbers = int.Parse(Console.ReadLine()); for (int i = 0; i < numbers; i++) { Console.WriteLine(i); }

Page 24: Review of Lecture 1 - kolosovpetro.github.io · Operators 0 Assignment 0 Rzutowanie int x = 23; x = x + 1; float f = 23; int i = (int)f; Assignment returns a value! int y = x = x

Summary

0 Operators

0 Conditional statements

0 Loops