1 / 34

ניתוח מערכות מידע תכנות ב C#

ניתוח מערכות מידע תכנות ב C#. נעים להכיר. תוכנית ראשונה ב C #. using System; class FirstProgram { static void Main(string[] args) { Console.WriteLine("Let's Learn C#"); } }. ;. בסוף כל פקודה יש ;. (data type) טיפוס . (variable name) שם. int total;.

rex
Download Presentation

ניתוח מערכות מידע תכנות ב C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ניתוח מערכות מידעתכנות ב C# נעים להכיר...

  2. תוכנית ראשונה ב C# using System; class FirstProgram { static void Main(string[] args) { Console.WriteLine("Let's Learn C#"); } }

  3. ; • בסוף כל פקודה יש ;

  4. (data type)טיפוס (variable name) שם int total; int count, temp, result; משתנים (variables) • משתנה הוא שם של מקום בזכרון. • משתנה מוגדר ע"י הגדרת השם שלו והטיפוס שלו. • טיפוס – מתאר את סוג המידע שהמשתנה יחזיק. • ניתן לאתחל משתנים בהצהרה • ניתן אף לאתחל מספר משתנים יחד int sum = 0; int base = 32, max = 149;

  5. השמה (assignment) • השמה משנה ערך של משתנה • אופרטור ההשמה הוא הסימן = • total = 25; • total מקבל את הערך 25. • הערך שהיה בתוךtotal נמחק • אפשר לשים רק ערך שמתאים לטיפוס של המשתנה.

  6. דוגמא משתנים והשמה using System; class Program { static void Main(string[] args) { int x = 5; int y; y = 2 * x; Console.WriteLine(y); } } • להוסיף...

  7. פקודת הקלט והערות • ReadLine – קורא שורה • השורה נקראת כמחרוזת (String) • אם רוצים לקרוא מספר יש להמיר (נראה בהמשך) • ניתן להשתמש בסוף תוכנית כדי לגרום למסך הריצה להשאר • יצא כשנלחץ return • רישום הערות // this is a comment /* this is a comment On more than one line*/

  8. דוגמא - טיפול בקלט ופלט using System; class IOProgram { static void Main(string[] args) { Console.WriteLine("Please Type Something:"); String x; x = Console.ReadLine(); Console.WriteLine("You typed {0}" , x ); Console. ReadKey(); } }

  9. WriteLine • מדפיס למסך את מה שנמצא בין גרשיים "XXX" • ניתן לשלב טיפול במשתנים • הם ממוספרים לפי סדר הופעתם • Console.WriteLine("You typed {0}" , x ); • ידפיס את הערך של x • Console.WriteLine("You typed {0,1}" , x , y ); • ידפיס את הערך של x ואז את הערך של y • ניתן לשרשר מחרוזות ע"ישימוש ב + • Console.WriteLine("You typed “ + x + y); • ידפיס גם כן את הערך של x ואז את הערך של y

  10. קלט של מספרים using System; class numberProgram { static void Main(string[] args) { String x; int y; Console.WriteLine("Enter a number:"); x = Console.ReadLine(); y = int.Parse(x); Console.WriteLine("You typed {0}" ,x); Console.WriteLine("Adding 2 makes: {0} or {1}", x+2, y+2 ); Console. ReadKey(); } }

  11. טיפוסים • integers - מספרים שלמים • byte, short, int, long… • floating point - מספרים בעלי נקודה עשרונית • float, double… • strings - מחרוזות • String • כל דבר בין " " אוטומטית יוגדר מחרוזת • characters - תוים בודדים • char, ‘a’ ‘\0’ ‘\n’ … • boolean - בוליאני • Boolean(מקבל ערך true אוfalse )

  12. טבלת תוים מיוחדים לשימוש במחרוזות

  13. ביטויים מתמטים • פעולות מתמטיות • מוגדרות על טיפוסים מסוג integer ו- floating point • + (חיבור) • - (חיסור) • * (כפל) • / (חילוק) • (כמו \ ב VB) • % (שארית, נקרא גם מודולוס) • (כמו Mod ב VB)

  14. הביטוי (condition)חייב להיות ביטוי בוליאני – כלומר ערכו אמת - true או שקר - false if מילה שמורה אם הביטוי (condition)אמת (true)מבוצעת ההצהרה (statement) אם הביטוי (condition)שקר (false) מדלגים על ההצהרה (statement) פקודת if פקודת if נכתבת באופן הבא: if ( condition ) statement;

  15. פקודת if-else • ניתן להוסיף לפקודה ifפקודת else ולייצר פקודת if-else. if ( condition ) statement1; else statement2; • אם ערך הביטוי conditionהואtrue בצע את statement1 • אם ערך הביטוי conditionהואfalse בצע את statement2 • יבוצע או statement1 או statement2אבל לא שניהם

  16. דוגמא - if using System; class ifProgram { static void Main(string[] args) { string x; x = Console.ReadLine(); if (x == "a" || x == "A") Console.WriteLine("A is for apple"); else if (x == "b" || x == "B") Console.WriteLine("B is for Boy"); else Console.WriteLine("Not defined"); Console.ReadKey(); } }

  17. ביטויים בוליאנים • אופרטורים של השוואה המחזירים ערך בוליאני == equal to (שווה) != not equal to (לא שווה) < less than (קטו מ) > greater than (גדול מ) <= less than or equal to (קטן או שווה ל) >= greater than or equal to (גדול או שווה ל) • שימו לב להבדל בין האופרטור = לאופרטור ==

  18. אופרטורים לוגיים • בביטויים בוליאנים אפשר להשתמש גם באופרטורים לוגיים. !Logical NOT &&Logical AND ||Logical OR • האופרטורים הלוגיים מקבלים אופרנדים בוליאנים ומחזירים תוצאה בוליאנית • האופרטור NOT הוא אופרטור אונרי -פועל רק על אופרנד אחד • האופרטורים AND, OR הם אופרטורים בינרים כלומר פועלים על שני אופרנדים

  19. הבדלים בין אופרטורים של VB ל C#

  20. הבדלים בין אופרטורים של VB ל C#המשך...

  21. קבוצת הצהרות • ניתן לקבץ יחד מספר הצהרות שיבוצעו אם הביטוי בפקודת ה if הוא אמת. נשתמש בסוגריים { } על מנת לתחום את ההצהרות. • ניתן גם בפקודות אחרות ב C# לקבץ מספר הצהרות באותו אופן. if (total > MAX) { Console.WriteLine ("Error!!"); errorCount++; }

  22. לולאת for • סינטקס של פקודת for: initialization אתחול מבוצע פעם אחת לפני תחילת ביצוע הלולאה statement הצהרה מבוצעת עד שהתנאי condition נהיה שקר for ( initialization ; condition ; increment ) statement; increment הוספה מבוצעת בסוף כל איטרציה (סיבוב של הלולאה)

  23. הגדרת משתנה באתחול for (int count=1; count <= 5; count++) Console.WriteLine(count); • ניתן להגדיר משתנה בתוך חלק האתחול.

  24. דוגמא - for using System; class ifProgram { static void Main(string[] args) { for (int i = 0; i < 100; i++) Console.WriteLine("The square root of {0} is {1} ", i, Math.Sqrt(i) ); Console.ReadKey(); } }

  25. הוספה (increment) בלולאת for for (int num=0; num < 100; num ++) Console.WriteLine (num); • בחלק הוספה ניתן לבצע כל פעולה for (int num=0; num <100; num += 5) Console.WriteLine (num); for (int num=100; num > 0; num -= 5) Console.WriteLine (num);

  26. לולאת while • סינטקס של פקודת while: while ( condition ) statement; • אם התנאי conditionהוא 'אמת' אז ההצהרה statement מתבצעת • אח"כ התנאי נבדק שוב אם ערכו עדיין 'אמת' ההצהרה מבוצעת שוב • ההצהרה מבוצעת שוב ושוב עד שהתנאי נהיה 'שקר' int count = 1; while (count <= 5) { Console.WriteLine(count); count++; }

  27. דוגמא לשימוש בלולאה – בדיקת קלט • הבעיה: • רוצים לקלוט מספרים מהמשתמש • דרוש לקלוט מספרים בטווח מסויים • פתרון: • נשתמש בלולאה • כל עוד לא קיבלנו קלט מתאים • נבקש ונקלוט קלט נוסף

  28. דוגמא - while using System; class FirstProgram { static void Main(string[] args) { int NUM_GAMES = 12; int won; double ratio; Console.WriteLine("Enter num of games won (0 to {0})", NUM_GAMES); string tmp = Console.ReadLine(); won = int.Parse(tmp); while (won < 0 || won > NUM_GAMES) { Console.WriteLine ("Invalid input. Please reenter: "); tmp = Console.ReadLine(); won = int.Parse(tmp); } ratio = (double)won / NUM_GAMES; Console.WriteLine ("Winning percentage is: {0}", ratio); } }

  29. דוגמא ללולאת do int count = 0; do { count++; Console.WriteLine (count); } while (count < 5); • יודפס 1 2 3 4 5 • הגוף של לולאת do מבוצע לפחות פעם אחת!

  30. switch וגם case מילים שמורות אם expression מתאים לvalue2- הבקרה עוברת לכאן פקודת switch switch ( expression ) { case value1: statement-list1 jump-statement case value2: statement-list2 jump-statement case value3 : statement-list3 jump-statement ... }

  31. שימוש ב break, default בתוך switch • ב C# חייבים להשתמש בפקודת קפיצה כפקודה האחרונה ברשימת הפקודות • למשל break, continue • ניתן ומקובל לייצר מקרה של ברירת מחדל בשם default • גם כאן יש להשתמש בפקודת קפיצה switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: break; }

  32. int grade, category; Console.WriteLine("Enter a numeric grade (0 to 100): "); string tmp = Console.ReadLine(); grade = int.Parse(tmp); category = grade / 10; switch (category) { case 10: Console.WriteLine("Perfect score. Well done."); break; case 9: Console.WriteLine("Well above average. Excellent."); break; case 8: Console.WriteLine("Above average. Nice job."); break; case 7: Console.WriteLine("Average."); break; default: Console.WriteLine("Study some more."); break; }

  33. פתיחת פרויקט C#

More Related