1 / 30

استخدام عبارات التحكم

استخدام عبارات التحكم. عبارات التحكم. عبارات التحكم. هي تلك التي تستخدم للتحكم في سير البرنامج بناء على نتيجة مقارنات وقرارات منطقية. أنواع عبارات الشرط. عبارات شرط If عبارات المقارنة Select Case. تركيبات عبارات الشرط IF. التركيب IF … Then التركيب IF … Then … Else Nested If

azucenak
Download Presentation

استخدام عبارات التحكم

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. استخدام عبارات التحكم

  2. عبارات التحكم • عبارات التحكم • هي تلك التي تستخدم للتحكم في سير البرنامج بناء على نتيجة مقارنات وقرارات منطقية..

  3. أنواع عبارات الشرط • عبارات شرط If • عبارات المقارنة Select Case

  4. تركيبات عبارات الشرط IF • التركيب IF … Then • التركيب IF … Then … Else • Nested If • التركيب If …Then .. Elseif

  5. التركيب IF … Then • يستخدم لتنفيذ أمر واحد أو مجموعة أوامر في حالة تحقق شرط معين. • الصيغة العامة : If <condition> then <command> If <condition> then ……. <command> ….. End If • اذا ﺗﺤﻘﻖ اﻟﺸﺮط او اﺻﺒﺤﺖ ﻧﺘﻴﺤﺔ اﻟﺘﻌﺒﻴﺮ اﻟﺸﺮﻃﻲ اﻟـﺬي ﻳﻠـﻲ ﺟﻤﻠـﺔ If هـﻮ True، ﻓﺎن الأكواد اﻟﺘﻲ ﺗﻠﻲ ﻋﺒﺎرة If … Then ﺳﻴﺘﻢ ﺗﻨﻔﻴﺬهﺎ:

  6. example ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim mark AsDouble mark = Convert.ToDouble(TextBox1.Text) If mark >= 60 Then Label2.Text = مبروك ناجح“” EndSub

  7. ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim mark AsDouble mark = Convert.ToDouble(TextBox1.Text) If mark >= 60 Then Label2.Text = مبروك ناجح“” Label2.BackColor = Drawing.Color.Red EndIf EndSub

  8. التركيب If …Then .. Else • يستخدم لتنفيذ أمر واحد أو مجموعة أوامر في حالة تحقق شرط معين , وتنفيذ أمر واحد او مجموعة أوامر في حالة عدم تحقق الشرط السابق • الصيغة العامة : If <condition> Then <Commands> Else <Commands> End if

  9. ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim mark AsDouble mark = Convert.ToDouble(TextBox1.Text) If mark >= 60 Then Label2.Text = مبروك ناجح“” Else Label2.Text = راسب“” EndIf EndSub

  10. Nested If • سلسلة شروط متداخله

  11. Nested If ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim age AsInteger age = Convert.ToInt32(TextBox1.Text) If age >= 25 Then If age >= 35 Then If age >= 65 Then Label2.Text = " انك كبير السن Else Label2.Text = " إن عمرك مناسب" EndIf Else Label2.Text = " انك لا تزال شابا" EndIf Else Label2.Text = " لازلت صغيرا جدا" EndIf EndSub

  12. التركيبه If …Then .. Elseif • يستخدم عند وجود عدة احتمالات لأمر معين • الصيغة العامة : If <condition> Then <Commands> ElseIf <condition> Then <Commands> ElseIf <condition> Then <Commands> .. .. Else <Commands> End if

  13. التركيب If …Then .. IfElse • مثال : • قومي بكتابة برنامج يقوم بطباعة تقدير الطالب بناء على الدرجة

  14. التركيب If …Then .. IfElse ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim mark AsInteger mark = Convert.ToDouble(TextBox1.Text) If mark >= 99 And mark <= 100 Then Label2.Text = "ممتاز" ElseIf mark >= 80 And mark <= 89 Then Label2.Text = "جيدجدا" ElseIf mark >= 70 And mark <= 79 Then Label2.Text = "جيد" ElseIf mark >= 60 And mark <= 69 Then Label2.Text = "مقبول" ElseIf mark <= 59 Then Label2.Text = "ضعيف" Else Label2.Text = "القيمة المدخله غير صحيحة" EndIf EndSub

  15. Select Case • تستخدم هذه العبارة عندما نتوقع من وقوع احتمالات كثيرة لشرط معين. • الصيغة العامة: Select Case <condVar> Case <Value1> Statement Group 1 Case <Value2> Statement Group 2 ……. ……….. Case Else Statement Group n End Select

  16. أنماط المتغيرات المستخدمة مع Select

  17. example • قومي بكتابة برنامج يقوم بحساب نسبة التخفيض حسب عدد السلع

  18. Example for discount (Sale ) ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim q AsDouble, discount AsDouble q = Convert.Toint32(TextBox1.Text) SelectCase q CaseIs < 0 Label2.Text = "الكمية يجب ان لا تكون سالبه" Case 1, 2, 3 discount = 0 Case 4 To 9 discount = 0.1 Case 10 To 49 discount = 0.2

  19. Case 50 To 100 discount = 0.3 CaseIs > 100 discount = 0.5 Case Else Label2.Text = "المدخلات غير معرفة من قبل النظام" EndSelect Label2.Text = discount * 100 & "%" EndSub

  20. الدوارات Loops • نحتاج للتكرار في أغلب العمليات الهندسية والعلمية .... • يستخدم Visual Basic نوعين من الدوارات : • العداد Counter Loops  For Loop • الدوارات المشروطة Conditional Loops  while\ Do loop

  21. الدوارة For .. Next تستخدم لتنفيذ خطوة أو مجموعة من الخطوات عدد محدد من المرات.. For counter = Satrtv To Endv [stepVal ] Command11 Command2 …… Next [counter] نهاية العداد معدل الزيادة بداية العداد عداد التحكم في عدد مرات التكرار

  22. Example: ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load Dim i AsInteger For i = 1 To 5 TextBox1.Text = TextBox1.Text & vbNewLine & I Next i EndSub

  23. Example: ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load Dim i AsInteger For i = 2 To 10 Step 2 TextBox1.Text = TextBox1.Text & vbNewLine & I Next i EndSub

  24. جملة التكرارية while ….end while • في أحيان كثيرة نحتاج إلى تكرار عملية أو مجموعة عمليات لعدد غير معلوم من المرات طالما ظل شرط معين محققا.. • الصيغة العامة: • While condition • … • Commands …. • End while

  25. Example: ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load Dim i AsInteger=2 while i<=10 TextBox1.Text = TextBox1.Text & vbNewLine & I i=i+2 end while EndSub

  26. جملة التكرارية do ….loop • هذه الجملة تماثل جملة while .. • الاختلاف

  27. الدوارة Do .. Loop • نوعين من الدورات المشروطة: • Do … loop While • Do … loop Until

  28. جملة التكرارية do ….loop

  29. تطبيق • قومي بكتابة برنامج يطلب من المستخدم إدخال سنتين ثم يقوم البرنامج بطباعة السنوات الكبيسة بين هاتين السنتين..

  30. ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim x AsInteger = TextBox1.Text Dim y AsInteger = TextBox2.Text Dim i AsInteger For i = x To y If ((i Mod 4) = 0) Then TextBox3.Text = TextBox3.Text & vbNewLine & i EndIf Next EndSub

More Related