logo

هياكل التحكم في بايثون

لا تعمل معظم البرامج من خلال تنفيذ تسلسل مباشر من البيانات. تتم كتابة الكود للسماح باتخاذ الاختيارات والعديد من المسارات من خلال البرنامج التي سيتم اتباعها اعتمادًا على التحولات في القيم المتغيرة.

تحتوي جميع لغات البرمجة على مجموعة مضمنة مسبقًا من هياكل التحكم التي تمكن تدفقات التحكم هذه من التنفيذ، مما يجعلها قابلة للتنفيذ.

سيتناول هذا البرنامج التعليمي كيفية إضافة حلقات وفروع، أي الشروط إلى برامج بايثون الخاصة بنا.

أنواع هياكل التحكم

يشير تدفق التحكم إلى التسلسل الذي سيتبعه البرنامج أثناء تنفيذه.

تؤثر الشروط والحلقات ووظائف الاتصال بشكل كبير على كيفية التحكم في برنامج بايثون.

هناك ثلاثة أنواع من هياكل التحكم في بايثون:

  • متسلسل - العمل الافتراضي للبرنامج
  • التحديد - يُستخدم هذا الهيكل لاتخاذ القرارات عن طريق التحقق من الشروط والتفرع
  • التكرار - يتم استخدام هذه البنية للتكرار، أي التنفيذ المتكرر لجزء معين من كتلة التعليمات البرمجية.

تسلسلي

البيانات المتسلسلة هي مجموعة من البيانات التي تتم عملية تنفيذها بالتسلسل. المشكلة في العبارات المتسلسلة هي أنه إذا تم كسر المنطق في أي سطر من السطور، فسوف ينقطع تنفيذ التعليمات البرمجية المصدر بالكامل.

شفرة

com.sdlc
 # Python program to show how a sequential control structure works # We will initialize some variables # Then operations will be done # And, at last, results will be printed # Execution flow will be the same as the code is written, and there is no hidden flow a = 20 b = 10 c = a - b d = a + b e = a * b print('The result of the subtraction is: ', c) print('The result of the addition is: ', d) print('The result of the multiplication is: ', e) 

انتاج:

 The result of the subtraction is: 10 The result of the addition is : 30 The result of the multiplication is: 200 

بيانات الاختيار/مراقبة القرار

ويشار أيضًا إلى البيانات المستخدمة في هياكل التحكم في الاختيار باسم البيانات المتفرعة، أو، حيث أن دورها الأساسي هو اتخاذ القرارات، بيانات التحكم في القرار.

يمكن للبرنامج اختبار العديد من الشروط باستخدام عبارات التحديد هذه، واعتمادًا على ما إذا كان الشرط المحدد صحيحًا أم لا، يمكنه تنفيذ كتل تعليمات برمجية مختلفة.

يمكن أن يكون هناك العديد من أشكال هياكل التحكم في القرار. فيما يلي بعض هياكل التحكم الأكثر استخدامًا:

  • فقط اذا
  • إذا كان غير ذلك
  • المتداخلة إذا
  • كامل إذا إليف آخر

بسيطة إذا

إذا كانت العبارات في Python تسمى عبارات التحكم في التدفق. تساعدنا بيانات التحديد في تشغيل جزء معين من التعليمات البرمجية، ولكن في ظروف معينة فقط. هناك شرط واحد فقط للاختبار في عبارة if الأساسية.

البنية الأساسية لعبارة if هي كما يلي:

بناء الجملة

 if : The code block to be executed if the condition is True 

سيتم تنفيذ هذه العبارات دائمًا. هم جزء من الكود الرئيسي.

سيتم تشغيل كافة العبارات المكتوبة بمسافة بادئة بعد عبارة if إذا كان مقدم الشرط بعد if الكلمة الأساسية صحيحة. فقط بيان الكود الذي سيتم تنفيذه دائمًا بغض النظر عما إذا كان الشرط هو البيان المكتوب المتوافق مع الكود الرئيسي. تستخدم Python هذه الأنواع من المسافات البادئة لتحديد كتلة التعليمات البرمجية لبيان تدفق تحكم معين. ستقوم بنية التحكم المحددة بتغيير تدفق تلك العبارات ذات المسافة البادئة فقط.

فيما يلي بعض الحالات:

شفرة

 # Python program to show how a simple if keyword works # Initializing some variables v = 5 t = 4 print(&apos;The initial value of v is&apos;, v, &apos;and that of t is &apos;,t) # Creating a selection control structure if v &gt; t : print(v, &apos;is bigger than &apos;, t) v -= 2 print(&apos;The new value of v is&apos;, v, &apos;and the t is &apos;,t) # Creating the second control structure if v <t : print(v , 'is smaller than ', t) v +="1" print('the new value of is v) # creating the third control structure if t: v, ' and t,', t, are equal') < pre> <p> <strong>Output:</strong> </p> <pre> The initial value of v is 5 and that of t is 4 5 is bigger than 4 The new value of v is 3 and the t is 4 3 is smaller than 4 the new value of v is 4 The value of v, 4 and t, 4, are equal </pre> <h3>if-else</h3> <p>If the condition given in if is False, the if-else block will perform the code t=given in the else block.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to use the if-else control structure # Initializing two variables v = 4 t = 5 print(&apos;The value of v is &apos;, v, &apos;and that of t is &apos;, t) # Checking the condition if v &gt; t : print(&apos;v is greater than t&apos;) # Giving the instructions to perform if the if condition is not true else : print(&apos;v is less than t&apos;) </pre> <p> <strong>Output:</strong> </p> <pre> The value of v is 4 and that of t is 5 v is less than t </pre> <h2>Repetition</h2> <p>To repeat a certain set of statements, we use the repetition structure.</p> <p>There are generally two loop statements to implement the repetition structure:</p> <ul> <li>The for loop</li> <li>The while loop</li> </ul> <h3>For Loop</h3> <p>We use a for loop to iterate over an iterable Python sequence. Examples of these data structures are lists, strings, tuples, dictionaries, etc. Under the for loop code block, we write the commands we want to execute repeatedly for each sequence item.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to execute a for loop # Creating a sequence. In this case, a list l = [2, 4, 7, 1, 6, 4] # Executing the for loops for i in range(len(l)): print(l[i], end = &apos;, &apos;) print(&apos;
&apos;) for j in range(0,10): print(j, end = &apos;, &apos;) </pre> <p> <strong>Output:</strong> </p> <pre> 2, 4, 7, 1, 6, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, </pre> <h3>While Loop</h3> <p>While loops are also used to execute a certain code block repeatedly, the difference is that loops continue to work until a given precondition is satisfied. The expression is checked before each execution. Once the condition results in Boolean False, the loop stops the iteration.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to execute a while loop b = 9 a = 2 # Starting the while loop # The condition a <b 1 will be checked before each iteration while a < b: print(a, end=" " ) + print('while loop is completed') pre> <p> <strong>Output:</strong> </p> <pre> 2 3 4 5 6 7 8 While loop is completed </pre> <hr></b></pre></t>

إذا كان غير ذلك

إذا كان الشرط الوارد في if هو False، فستقوم كتلة if-else بتنفيذ الكود t=المعطى في الكتلة else.

عفوا المفاهيم

شفرة

 # Python program to show how to use the if-else control structure # Initializing two variables v = 4 t = 5 print(&apos;The value of v is &apos;, v, &apos;and that of t is &apos;, t) # Checking the condition if v &gt; t : print(&apos;v is greater than t&apos;) # Giving the instructions to perform if the if condition is not true else : print(&apos;v is less than t&apos;) 

انتاج:

 The value of v is 4 and that of t is 5 v is less than t 

تكرار

لتكرار مجموعة معينة من العبارات، نستخدم بنية التكرار.

يوجد عمومًا بيانان للحلقة لتنفيذ بنية التكرار:

  • الحلقة for
  • حلقة بينما

لحلقة

نحن نستخدم حلقة for للتكرار عبر تسلسل بايثون القابل للتكرار. ومن أمثلة هياكل البيانات هذه القوائم والسلاسل والصفوف والقواميس وما إلى ذلك. ضمن كتلة التعليمات البرمجية للحلقة، نكتب الأوامر التي نريد تنفيذها بشكل متكرر لكل عنصر تسلسل.

شفرة

 # Python program to show how to execute a for loop # Creating a sequence. In this case, a list l = [2, 4, 7, 1, 6, 4] # Executing the for loops for i in range(len(l)): print(l[i], end = &apos;, &apos;) print(&apos;
&apos;) for j in range(0,10): print(j, end = &apos;, &apos;) 

انتاج:

 2, 4, 7, 1, 6, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 

حائط اللوب

بينما تُستخدم الحلقات أيضًا لتنفيذ كتلة تعليمات برمجية معينة بشكل متكرر، فإن الفرق هو أن الحلقات تستمر في العمل حتى يتم استيفاء شرط مسبق معين. يتم فحص التعبير قبل كل تنفيذ. بمجرد أن يؤدي الشرط إلى خطأ منطقي، توقف الحلقة التكرار.

شفرة

 # Python program to show how to execute a while loop b = 9 a = 2 # Starting the while loop # The condition a <b 1 will be checked before each iteration while a < b: print(a, end=" " ) + print(\'while loop is completed\') pre> <p> <strong>Output:</strong> </p> <pre> 2 3 4 5 6 7 8 While loop is completed </pre> <hr></b>