مقدمة في الحلقات التكرارية

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

ما هي الوظيفة الأساسية للحلقات التكرارية في البرمجة؟

  • تقليل استهلاك الذاكرة في البرنامج.
  • تنظيم هيكل البرنامج وتسهيل قراءته.
  • تسريع تنفيذ العمليات الحسابية المعقدة.
  • تكرار تنفيذ مجموعة من الأوامر عددًا معينًا من المرات أو طالما تحقق شرط معين. (correct)

أي نوع من الحلقات التكرارية يعتبر الأنسب لتكرار تنفيذ أوامر عدد محدد من المرات معروف مسبقًا؟

  • حلقة `while`.
  • حلقة `if`.
  • حلقة `do...while`.
  • حلقة `for`. (correct)

ما هي المكونات الرئيسية لحلقة for؟

  • العداد، تعبير الاستمرار، تعبير تغيير القيمة. (correct)
  • القيمة الابتدائية، الشرط، القيمة النهائية.
  • اسم المتغير، نوع المتغير، القيمة.
  • الشرط، الأوامر المراد تكرارها، الزيادة.

في حلقة for، متى يتم تنفيذ "تعبير تغيير القيمة"؟

<p>بعد كل دورة من دورات الحلقة. (D)</p> Signup and view all the answers

ما الفرق الأساسي بين تعريف العداد داخل حلقة for أو خارجها؟

<p>تعريف العداد داخل الحلقة يجعله متغيرًا محليًا خاصًا بالحلقة فقط، بينما تعريفه خارجها يجعله متاحًا للاستخدام خارج الحلقة أيضًا. (B)</p> Signup and view all the answers

كيف يمكن إنهاء حلقة تكرارية قبل الوصول إلى نهايتها الطبيعية؟

<p>باستخدام الأمر <code>break</code>. (A)</p> Signup and view all the answers

ماذا تعني جملة for متداخلة؟

<p>حلقة <code>for</code> توجد داخل حلقة <code>for</code> أخرى. (B)</p> Signup and view all the answers

ما هي الحلقة التكرارية التي تضمن تنفيذ الأوامر مرة واحدة على الأقل قبل التحقق من الشرط؟

<p>حلقة <code>do...while</code>. (B)</p> Signup and view all the answers

ما هو الفرق الرئيسي بين حلقتي while و do...while؟

<p><code>while</code> تتحقق من الشرط قبل التنفيذ، بينما <code>do...while</code> تتحقق منه بعد التنفيذ. (B)</p> Signup and view all the answers

ما هو نوع التطبيقات الذي لا يتطلب تصميم نافذة ويستخدم شاشة خاصة للإدخال والإخراج؟

<p>تطبيقات الطرفية (Console Applications). (B)</p> Signup and view all the answers

ما هي الوسيلتان الأساسيتان اللتان تستخدمان في تطبيقات الطرفية Console لإدخال وعرض البيانات؟

<p><code>ReadLine()</code> و <code>WriteLine()</code>. (B)</p> Signup and view all the answers

إذا أردت إظهار رسالة ترحيب في مربع رسالة 10 مرات، ثم الخروج من الحلقة، أي الأوامر التالية تستخدم ؟

<p><code>break</code> (B)</p> Signup and view all the answers

لإضافة قيمة المتغير i إلى المتغير sum وتخزين الناتج في sum، أي تعبير يستخدم؟

<p><code>sum += i</code> (A)</p> Signup and view all the answers

ماذا يحدث إذا كان شرط الاستمرار في حلقة while غير محقق في البداية؟

<p>لا يتم تنفيذ الأوامر داخل الحلقة على الإطلاق. (D)</p> Signup and view all the answers

في حلقة for، إذا كان تعبير الاستمرار دائمًا صحيحًا، فماذا سيحدث؟

<p>ستستمر الحلقة في العمل بشكل لانهائي. (B)</p> Signup and view all the answers

كيف يمكن كتابة حلقة تكرارية متناقصة؟

<p>باستخدام عداد يبدأ بقيمة كبيرة وينقص تدريجيًا. (C)</p> Signup and view all the answers

أي من الخيارات التالية هو الشكل الصحيح لتعريف حلقة for تبدأ من 1 وتنتهي عند 10 وتزيد بمقدار 1 في كل مرة?

<p><code>for (int i = 1; i &lt;= 10; i++)</code> (B)</p> Signup and view all the answers

أي مما يلي يمثل الطريقة الصحيحة لطباعة الأرقام الزوجية من 2 إلى 10 باستخدام حلقة for?

<p><code>for (int i = 1; i &lt;= 10; i++) { if (i % 2 == 0) { Console.WriteLine(i); } }</code> (B)</p> Signup and view all the answers

ما هو التعبير الذي يجب استخدامه في حلقة for لتحقيق تناقص العداد بمقدار 2 في كل تكرار?

<p><code>i -= 2</code> (D)</p> Signup and view all the answers

متى يتم تقييم شرط الاستمرار في حلقة do...while?

<p>بعد تنفيذ كتلة التعليمات. (B)</p> Signup and view all the answers

أي جزء من حلقة التكرار for مسؤول عن تحديد متى يجب أن تتوقف الحلقة?

<p>الشرط (Condition). (C)</p> Signup and view all the answers

ماذا يحدث إذا لم يتم تحديث قيمة العداد داخل حلقة while?

<p>ستستمر الحلقة في التنفيذ إلى أجل غير مسمى. (C)</p> Signup and view all the answers

أي نوع من الحلقات مناسب لتكرار جزء من التعليمات البرمجية بناءً على شرط معروف مسبقًا, ولكن مع ضمان تنفيذه مرة واحدة على الأقل?

<p><code>do...while</code> (A)</p> Signup and view all the answers

إذا كانت لديك حلقة for متداخلة داخل حلقة for أخرى، فكم مرة سيتم تنفيذ الكود الموجود داخل الحلقة الداخلية?

<p>بعدد مرات تنفيذ الحلقة الخارجية مضروبًا في عدد مرات تنفيذ الحلقة الداخلية. (A)</p> Signup and view all the answers

ما الغرض من استخدام جملة break داخل حلقة تكرارية?

<p>للخروج من الحلقة التكرارية بشكل فوري. (D)</p> Signup and view all the answers

في سياق تطبيقات консоль، ما هي وظيفة الأوامر Console.WriteLine() و Console.ReadLine()?

<p>لعرض الإخراج وقراءة الإدخال من المستخدم. (C)</p> Signup and view all the answers

في حلقة for، أي جزء يتم تنفيذه مرة واحدة فقط عند بداية الحلقة?

<p>التهيئة (A)</p> Signup and view all the answers

ما هو الناتج المتوقع للكود التالي؟ int x = 0; while (x < 5) { Console.WriteLine(x); x++; }

<p>0 1 2 3 4 (B)</p> Signup and view all the answers

ما هو الغرض من العداد (Counter) في الحلقة التكرارية?

<p>تتبع عدد مرات تنفيذ الحلقة (A)</p> Signup and view all the answers

إلى ماذا يشير مصطلح حلقة لانهائية (Infinite Loop)?

<p>حلقة لا تنتهي أبدًا (A)</p> Signup and view all the answers

أي من التعليمات التالية تسمح لك بتخطي بقية التعليمات في التكرار الحالي للانتقال مباشرة إلى بداية التكرار التالي؟

<p>continue (D)</p> Signup and view all the answers

ما هو الأمر الذي يكتب عادة في الجزء ()while ليحدد متى تنتهي الحلقة؟

<p>تعبير (D)</p> Signup and view all the answers

ما هو المصطلح التقني لجعل احدى حلقات التكرار داخلية بالنسبة لحلقة تكرار اخرى؟

<p>التعشيش (A)</p> Signup and view all the answers

ماذا يحدث عند استدعاء الأمر ()break داخل كتلة الأوامر الخاصة بحلقة التكرار؟

<p>الخروج من حلقة التكرار الداخلية فقط (B)</p> Signup and view all the answers

أيهما الأصح لوضع قيمة أولية للعداد الخاص بحلقة التكرار؟

<p>قبل الحلقة (A)</p> Signup and view all the answers

لو كان المطلوب هو كتابة برنامج لحساب مجموع الأعداد الزوجية بين 1 و 10، ما هي نوعية الحلقة الأنسب لتحقيق ذلك؟

<p>for (C)</p> Signup and view all the answers

Flashcards

الحلقات التكرارية

تكرار تنفيذ بعض المهام عدد معين من المرات أو طالما يتحقق شرط معين.

حلقة for

لتكرار تنفيذ أمر لعدد معين من المرات.

تعبير الاستمرار

تعبير يستخدم لمقارنة قيمة العداد بقيمة معينة لتحديد استمرار الحلقة.

تعبير تغيير القيمة

تعبير يغير قيمة العداد مع كل دورة من دورات الحلقة.

Signup and view all the flashcards

حلقة تكرارية شرطية

لتنفيذ الأوامر طالما تحقق شرط معين

Signup and view all the flashcards

حلقة For ذات العداد

لتنفيذ أمر أو أكثر عدد معين من المرات.

Signup and view all the flashcards

عداد Counter

متغير صحيح يمثل عداد الحلقة.

Signup and view all the flashcards

قيمة البدء StartVal

القيمة التي يبدأ بها العداد.

Signup and view all the flashcards

Console Application

إظهار نتائج البرنامج مباشرة على الشاشة بدون تصميم.

Signup and view all the flashcards

ReadLine

أداة لإدخال البيانات في Console Application

Signup and view all the flashcards

WriteLine

أداة لعرض البيانات في Console Application

Signup and view all the flashcards

حلقة تكرارية متداخلة

إنشاء حلقة for داخل حلقة for أخرى.

Signup and view all the flashcards

break

تنهي الحلقة التكرارية قبل الوصول لنهايتها.

Signup and view all the flashcards

حلقة Do While

حلقة تعتمد على وجود شرط معين لاستمرار التنفيذ.

Signup and view all the flashcards

حلقة While.

لتكرار تنفيذ الأوامر

Signup and view all the flashcards

Study Notes

  • This lecture covers how to repeat tasks a specific number of times or as long as a condition is met using loops.

Learning Objectives

  • Differentiate between loops that iterate a specific number of times and those that depend on a condition.
  • Implement a for loop to repeat an instruction or set of instructions a fixed number of times.
  • Explain the components of a for loop: counter, continuation expression, and value change expression.
  • Use the for loop with an incrementing or decrementing counter.
  • Differentiate between defining a counter inside or outside a for loop.
  • Terminate a loop before reaching its natural end.
  • Explain how nested for loops work.
  • Implement Do While loops.
  • Implement While loops.
  • Explain the difference between Do While and While loops.
  • Design an application using the Console Application type.
  • Use input and output statements in the Console Application environment.

Introduction to Loops

  • One of the main advantages of computers is their ability to perform tasks quickly and accurately, no matter how many tasks there are.
  • It can also repeat execution of a specific task any number of times with the same accuracy.
  • For example, to design an application to input the grades of a student in 10 subjects, and the program calculates the sum and grade for the student, there is no problem so far, but suppose you have 1000 students, will you repeat writing the previous program several times according to the number of students? Of course not.
  • Programming languages, including C#.NET, provide what are called loops, where a specific task is executed repeatedly instead of writing the steps of this task several times.
  • C#.NET uses two types of loops

Types of Loops

  • Counter loop for executing the required commands a specific number of times.
  • Conditional loop to execute commands based on the continued existence of a specific condition.

For Loop

  • Used to execute a command or more a specific number of times. The idea of ​​the loop depends on the presence of a variable that acts as a counter, so it is called a counter. We specify a certain value ​​for this variable to start with.

For Loop Syntax

  • The general form of the For statement is as follows: for (int Counter = StartVal; Continue expression; Update_Expression) The command or set of commands to be repeated
  • Counter: The integer variable that represents the loop counter, and can be named anything else.
  • StartVal: The value at which the counter starts, and can be the value itself or a variable containing this value.
  • Here's how to make the counter start at value 1: for (counter=1; ....
  • Continue Expression: A comparison expression that compares the value of the counter to a specific value, and is tested with each iteration of the loop, and the loop continues to iterate as long as the result of this expression is True.
  • Examples: for (counter=1; counter <=10;.... for (counter=1; counter != 10;....
  • Update Expression: An expression that changes the value of the counter with each iteration of the loop, such as incrementing or decrementing it by a certain value.
  • Examples: for (counter=1; counter <=10; ++counter) for (counter=1; counter != 20; counter +=5)
  • What happens when execution reaches the loop?
    • The counter starts at the specified value, and the continuation expression is tested, If the result is True, it enters the loop and executes the commands enclosed between { } , and if it is False, it does not enter the loop and continues executing the program after it.
    • If the loop is entered for the first time, and after completing the execution of the required commands, the value change expression is executed.
    • Then the continuation expression is tested, If the result is True, the commands are executed a second time, and the value change expression is executed, and so the loop continues to execute as long as the comparison expression is True until the expression produces False, the loop ends and exits from it, and complete executing the program after { which is the ending brace.
  • Exercise: Design a program that displays the message box ten times, and each time the number for the numbers 1 to 10 appears in the box.
  • Start a new project, then put a command button (Button) on the form window, then open the programming window, and in the event of the command button, write the following code: for (int x = 1; x<=10;++x) { MessageBox.Show(x.ToString()); }
  • Executing the previous loop as follows:
    • When the loop is reached, the initial value (1) is stored in the variable (x), which works here as a counter.
    • The value of the variable (x) is compared with the value (10) where if the value of the counter (x) is greater than the value (10), the loop is not entered and the program execution moves to the first sentence after it.
    • If the value of the counter (x) is less than or equal to the value (10), the commands required to be repeated between { } are executed, and since the value of the variable (x) in the first cycle is (1), the command to display the message is executed and the value (1) appears in it.
  • Then the continuation expression is tested, so if the result is True, the commands are executed for the third time, and the value change expression is executed, and so the loop continues to execute as long as the result of the comparison is True until the expression produces False, then the loop is terminated and exited from it, and the program execution is completed after the { which is the closing brace.
  • The value of the variable (x) is increased by (۱).
  • The program returns to the beginning of the loop, and the value of the variable (x) is compared with the value (۱۰) again, so it will find that it is (۲), so the loop is executed again.
  • Thus, the repetition continues and the counter has the following values ​​in the following times (٣, ٤, ٥, ٦ .........) as long as the variable has not reached the value (۱۰) and the loop commands are executed, and when the value of the variable reaches the value (10), the result of the continue expression will be Flase and the repetition will stop.
  • Run the program by pressing the F5 key and then click on the button on the window to see the output.

Declaring a Counter Inside or Outside the Loop

  • Look at the following two examples of a loop, the example on the left has the counter defined inside the loop, while the example on the right has the variable defined outside the loop, but it was used as a counter normally. What's the difference? for (int x = 1; x <= 10; ++x) int x; for (x = 1; x <= 10; ++x)
  • When the variable is defined within the loop, it becomes like a local variable that is only used for the loop.
  • You cannot use it outside the curly braces { }.
  • If the variable is defined before the loop as in the example on the right, then this variable can be used as a counter for the loop, and can be used after exiting the loop as a normal variable.
  • When using it after exiting the loop, it retains its last value ​​in the loop until it is changed by a statement as usual.
  • Exercise: Suppose that in the previous exercise we want to display even numbers from 1 to 10 in the message squares.
  • Here, the counter will start with the first even number, which is (۲), and its value will increase by (۲) in each loop cycle, and we make the value change expression increase the counter by (۲) as follows: for (int x = 2; x<= 10; x+=2) { MessageBox.Show(x.ToString()); }
  • In this example, the command will only be repeated five times, because the counter starts at the value 2, then becomes 4, then 6, then 8, then 10.

Decrementing For Loop

  • In the previous examples, the loop counter started at a small value and increased, but the nature of the application may require starting with a large value, gradually decreasing to a smaller value, and here we will make the expression to change the value of the counter decrease the value of the counter.
  • Exercise: Required to display messages with the values ​​of the odd numbers from ten to one.
  • Here the counter will start with the first odd value ​​in the required range, which is (۹), and the value change expression is used to decrease the counter by (۲) with each cycle of the loop. for (int x = 9; x>= 1; x-=2) { MessageBox.Show(x.ToString()); }
  • Exercise: Required to write a program to calculate the sum of even numbers from (1) to (10).
  • Create a new project, and put a Label, (Button).
  • Modify the properties of the tools to make the shape of the window and the tools as shown in the following figure.
  • Double-click the Calculate button to open the Click event procedure, and write the following code: int sum =0; for (int i=2; i<= 10; i+=2) { sum += i; } label1.Text = sum.ToString();
  • In the previous code, note the following:
    • An integer variable named sum was declared, which will be used to accumulate each number.
    • The variable sum, we put an initial value (0) in it to ensure that it is completely empty, and always remember that any variable that will work as a storage for collection must have the value (0) in it initially.
    • The loop starts with the first even value ​​for the counter, which is (۲), and the loop increases by (۲), and the loop stops when the value of the counter reaches (۱٠).
    • The sum = i expression is used to add the value of the variable (i) to the variable (sum), then store the result in the variable (sum), because as we mentioned it is a collection field and each new value ​​is added to its contents, this is the opposite of the expression sum = i. Here the variable (Sum) will not retain the value in it, but it will be replaced with the value of the variable (i).
  • Show in the following figure the change in the value of the variable sum with each cycle of the loop
    • variable i = 2, sum + i = 0 + 2 then sum = 2
    • variable i = 4, sum + i = 2 + 4 then sum = 6
    • variable i = 6, sum + i = 6 + 6 then sum = 12
    • variable i = 8, sum + i = 12 + 8 then sum = 20
    • variable i = 10, sum + i = 20 + 10 then sum = 30

Observations

  • The value of the variable i at the beginning of the loop is 2, and the value of the variable sum at the beginning is (۰), so with the first cycle the value of the expression sum + i is (۲).
  • In the second cycle i becomes 4, and the arithmetic expression is executed by adding the previous value (sum) which is (۲) to the value of the counter (i) which is (٤) to become the current value ​​of sum is (٦). Thus, the execution of the loop is repeated until it reaches the value (sum) to (۳۰) and the resulting of summing the value of i up to ((۱۰) and the last value ​​of the variable sum is (۲۰).
  • The ToString function was used to convert the value that located in sum from numeric value to textual value and can be displayed through the Labell control.
  • Implement the program by pressing the F5 to see the final result.
  • The loop remains in rotation and executes what is inside it from the beginning until the result of a comparison expression is false and it is exited from it. Sometimes the application requires ending the loop before the result of the comparison expression is false as a result for example of something happened or a certain condition becomes valid.
  • You can implement that by using the statement (break), often used with the conditional statement if to examine if a certain event occurred or a condition is validated and the break statement is processed at that time.
  • Code example for(int x = 1; x<= 10; ++x) { MessageBox.Show(x.ToString()); if (x == 7) break; } MessageBox.Show ("welcome");
  • In this example, the message square will display several times with the value of x, and it should display 10 times, but when the counter "x" reach the value (7) it will exit from the loop as a result of the existence of if statement that validate the condition and then break statement is processed, after exiting from the loop,the first statement will be performed right behind the "{", so the "welcome" message will show.
  • We still can use variables with float data to construct for loops.
  • You can use a counter of type (float) and use an expression to change the value of a decimal value, as shown in the following example: for (float x = 1f; x <= 2f; x+=.1f)
  • In this loop, the counter starts at the value (1), and with each cycle the counter increases by (۰.۱), note the placement of the letter f after the number because, as we mentioned, decimal numbers must have this letter placed after them when using them.

Nested For Loops

  • By that is meant creating a for loop inside another for loop for (int i = 1; i <= 10; .........) { for (int j = 1; j<= 5; .........) { //Set of internal loop commands } //Set of external loop commands }
  • Following points can be made from the above code structure:
    • External loop with counter (i) will cycle its set of commands (10) times, with respect to the internal loop with counter (j) will cycle its set of commands (5) times.
    • Internal loop is exist inside external loop commands, so internal loop will be cycle (10) times.
    • So all commands found in Group (1) will be performed 50 times (i.e inner loop commands(5) * outer loop(10) = 50).
    • All commands found in Group (2) will be performed 10 times only because found in outer or external scope.

Example

  • Put Button on window
  • Write codes with same event of Button "click"

Illustration

for (int i=1; i <= 3; i++) { for (int j = 1; j<= 5; j++) { MessageBox.Show ("welcome"); } }

  • Following the implementation of mentioned example, 15 messages of "welcome" shows because inner loop will expose 5 messages and outer loop will tell inner loop to repeat itself 3 times, thus, it result : (3*5 = 15).
  • Exercise: implement "for" loop inside "for" loop that outer loop "i" starts 1 to 3 and inner loop starts 1 to 5, with message that output values of counters with it so the user can see.
  • New project and Button, set properties and name variables
  • Double click on button with code and click event
  • Write the following codes in click scope for (int i = 1; i<= 3 ; i++) { for (int k = 1; k<= 5 ;k++) { MessageBox.Show("I = " + i + "K= " + k); } }

Do While

  • Relays on the existence of a condition to stop or start the loop. stops at wrong input condition
  • General form of Loop do { Set of command to repeat } While (Condition)
  • If condition equals TRUE it will relay command loop . so, once it reaches FALSE it will terminates operation.
  • Condition is compare form/term so if results are true continue cycle command otherwise stop with operations/process .

Example

  • Showing Message values with values from 1 though 10. int i=1; do MessageBox.Show(i.ToString()); i=i+1; }while (i <= 10);
  • Variable starts " i " with one and it's displayed with values.
  • i is incremental command.
  • Value examines either it is equesl or not equal 10, or its keep cycle unit reach or stop condition.
  • Notice, command is always display first cycle but does not validate input until enter into commands.
  • The loop will give you an output even the first entry is not validated, because it is entered before examined

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Loops in C# Programming

More Like This

4. JavaScript FOR Loops
8 questions

4. JavaScript FOR Loops

MagnanimousCloisonnism avatar
MagnanimousCloisonnism
C++ Loops: While, Do-While, For
5 questions
C# Bucles: FOR, WHILE y DO-WHILE
6 questions

C# Bucles: FOR, WHILE y DO-WHILE

CostEffectiveRationality3754 avatar
CostEffectiveRationality3754
Use Quizgecko on...
Browser
Browser