SlideShare a Scribd company logo
1 of 37
Pętle w C#
Mapa Wykładu
 Wykład : Pętle
 Powtórka z poprzedniego wykładu
 Switch/Case
 While
 For
 Do...while
 Skróty arytmetyczne
 Słowa kluczowe Continue i Break
 Debugowanie kodu
 Wyjątki
Powtórka z poprzedniego wykładu
string napis = "O_o What";
char znakZ = 'Z';
bool IsThisbłąd = false;
double ulamek = 3.14;
double 2ulamek = 0.01;
ulamek = 2ulamek;
Console.WriteLine(ulamek);
bool isReady = true;
Console.WriteLine(IsReady);
string s1 = "12";
string s2 = "8";
Console.WriteLine(int.Parse(s1)
+ int.Parse(s2));
int liczba12 = 12;
string liczbaNapis12 = liczba12.ToString();
typ_zwracany nazwa_metody (lista parametrów)
{
//blok kodu który wykona się przy
metodzie
}
WykonamOperacje();
int rezultat = ObliczeCos(1,2);
static string Zwracam()
{
return "LOL";
string s = "Do tego program nigdy nie dojdzie";
}
static void MessageNumber(string komunikat, int liczba)
{
Console.WriteLine(komunikat + " : " + liczba.ToString());
}
int a = 5;
int b = 2;
bool rownasie = a == b;
Console.WriteLine(rownasie);
Console.WriteLine(a != b);
int liczba = 20;
Console.WriteLine(liczba < 18);//false
Console.WriteLine(liczba <= 20);//true
Console.WriteLine(liczba > 30);//false
Console.WriteLine(liczba >= 16);//true
bool zrobione = false;
Console.WriteLine(!zrobione);//true
Console.WriteLine(!true);//false
//operacje AND
bool WyjscNaSpacer;
int temperatura = 20;
WyjscNaSpacer = (temperatura >= 10) && (temperatura <= 30);
//operacje LUB
bool ZostacWDomu;
int temperaturA = 5;
ZostacWDomu = (temperaturA < 10) || (temperaturA > 30);
string TwojeZdanie = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.DarkYellow;
if ((TwojeZdanie == "nie") || (TwojeZdanie == "Nie"))
{
Console.WriteLine("Rozumiem w takim razie musze zacząć od tego.");
Console.WriteLine("...jego informacje w której się zawiera");
Console.WriteLine("Bla bla");
}
else
{
Console.WriteLine("t To bardzo dobrze. ");
}
Console.WriteLine("Teraz gdy wiesz czym jest kwantowa teleportacja..");
Switch/Case
int CzasSnu;
CzasSnu = 8;
if (CzasSnu <= 3)
Console.WriteLine("To była mała drzemka.Musze więcej spać.");
else if (CzasSnu <= 5)
Console.WriteLine("Czuje się wciąż zmęczony");
else if (CzasSnu <= 7)
Console.WriteLine("To był dobry sen.Ale mam mały ból głowy.");
else if (CzasSnu <= 9) Console.WriteLine("Czuje się silniejszy");
else if (CzasSnu <= 12) Console.WriteLine("Za długo spałem");
else Console.WriteLine("Nie zrozumiała komenda");
Tutaj if/elseif ma sens
int CzasSnu;
CzasSnu = 4;
if (CzasSnu <= 3) {}
if ((CzasSnu <= 5) && (CzasSnu > 3)) { }
Dużo gorszy zapis
string polskaLiczba = "jeden";
if (polskaLiczba == "jeden")
Console.WriteLine(1);
else if (polskaLiczba == "dwa")
Console.WriteLine(2);
else if (polskaLiczba == "trzy")
Console.WriteLine(3);
else
Console.WriteLine("Nie znane");
Tutaj już nie jest to oczywiste. Do tego przykładu
lepiej zastosować instrukcje „switch”
polskaLiczba = "dwa";
switch (polskaLiczba)
{
case "jeden":
Console.WriteLine(1);
break;
case "dwa":
Console.WriteLine(2);
break;
case "trzy":
Console.WriteLine(3);
break;
default:
Console.WriteLine("Nie znane");
break;
}
Pętle
While
while (warunekbool)
{
operacje
}
int i = 0;
while (i < 5)
{
Console.WriteLine(i++);
}
For
for (inicjalizacja zmiennej; warunek logiczny; zaktualizowanie zmiennej)
{
//blok kodu
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Do While
int b = 0;
do
{
b += 2;
Console.WriteLine(b);
}
while (b < 9);
Continue i Break
int b = 0;
do
{
b += 2;
if (b == 12)
break;
else if ((b == 4) || (b == 10))
continue;
Console.WriteLine(b);
}
while (true); //2,6,8

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Pętle w c#

  • 3.  Wykład : Pętle  Powtórka z poprzedniego wykładu  Switch/Case  While  For  Do...while  Skróty arytmetyczne  Słowa kluczowe Continue i Break  Debugowanie kodu  Wyjątki
  • 5. string napis = "O_o What"; char znakZ = 'Z'; bool IsThisbłąd = false;
  • 6. double ulamek = 3.14; double 2ulamek = 0.01; ulamek = 2ulamek; Console.WriteLine(ulamek);
  • 7. bool isReady = true; Console.WriteLine(IsReady);
  • 8. string s1 = "12"; string s2 = "8"; Console.WriteLine(int.Parse(s1) + int.Parse(s2)); int liczba12 = 12; string liczbaNapis12 = liczba12.ToString();
  • 9. typ_zwracany nazwa_metody (lista parametrów) { //blok kodu który wykona się przy metodzie }
  • 11. static string Zwracam() { return "LOL"; string s = "Do tego program nigdy nie dojdzie"; }
  • 12. static void MessageNumber(string komunikat, int liczba) { Console.WriteLine(komunikat + " : " + liczba.ToString()); }
  • 13.
  • 14. int a = 5; int b = 2; bool rownasie = a == b; Console.WriteLine(rownasie); Console.WriteLine(a != b);
  • 15. int liczba = 20; Console.WriteLine(liczba < 18);//false Console.WriteLine(liczba <= 20);//true Console.WriteLine(liczba > 30);//false Console.WriteLine(liczba >= 16);//true
  • 16. bool zrobione = false; Console.WriteLine(!zrobione);//true Console.WriteLine(!true);//false
  • 17. //operacje AND bool WyjscNaSpacer; int temperatura = 20; WyjscNaSpacer = (temperatura >= 10) && (temperatura <= 30); //operacje LUB bool ZostacWDomu; int temperaturA = 5; ZostacWDomu = (temperaturA < 10) || (temperaturA > 30);
  • 18. string TwojeZdanie = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.DarkYellow; if ((TwojeZdanie == "nie") || (TwojeZdanie == "Nie")) { Console.WriteLine("Rozumiem w takim razie musze zacząć od tego."); Console.WriteLine("...jego informacje w której się zawiera"); Console.WriteLine("Bla bla"); } else { Console.WriteLine("t To bardzo dobrze. "); } Console.WriteLine("Teraz gdy wiesz czym jest kwantowa teleportacja..");
  • 19.
  • 21. int CzasSnu; CzasSnu = 8; if (CzasSnu <= 3) Console.WriteLine("To była mała drzemka.Musze więcej spać."); else if (CzasSnu <= 5) Console.WriteLine("Czuje się wciąż zmęczony"); else if (CzasSnu <= 7) Console.WriteLine("To był dobry sen.Ale mam mały ból głowy."); else if (CzasSnu <= 9) Console.WriteLine("Czuje się silniejszy"); else if (CzasSnu <= 12) Console.WriteLine("Za długo spałem"); else Console.WriteLine("Nie zrozumiała komenda"); Tutaj if/elseif ma sens
  • 22. int CzasSnu; CzasSnu = 4; if (CzasSnu <= 3) {} if ((CzasSnu <= 5) && (CzasSnu > 3)) { } Dużo gorszy zapis
  • 23. string polskaLiczba = "jeden"; if (polskaLiczba == "jeden") Console.WriteLine(1); else if (polskaLiczba == "dwa") Console.WriteLine(2); else if (polskaLiczba == "trzy") Console.WriteLine(3); else Console.WriteLine("Nie znane"); Tutaj już nie jest to oczywiste. Do tego przykładu lepiej zastosować instrukcje „switch”
  • 24. polskaLiczba = "dwa"; switch (polskaLiczba) { case "jeden": Console.WriteLine(1); break; case "dwa": Console.WriteLine(2); break; case "trzy": Console.WriteLine(3); break; default: Console.WriteLine("Nie znane"); break; }
  • 26.
  • 27. While
  • 29. int i = 0; while (i < 5) { Console.WriteLine(i++); }
  • 30. For
  • 31. for (inicjalizacja zmiennej; warunek logiczny; zaktualizowanie zmiennej) { //blok kodu }
  • 32. for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
  • 34. int b = 0; do { b += 2; Console.WriteLine(b); } while (b < 9);
  • 35.
  • 37. int b = 0; do { b += 2; if (b == 12) break; else if ((b == 4) || (b == 10)) continue; Console.WriteLine(b); } while (true); //2,6,8