محاضرة 5
Loop Control (Break/Continue) and 1D Arrays
أوامر Break و Continue، والمصفوفات أحادية البعد
ملخص المحاضرة
📜 Lecture 5: Loops (Postfix/Prefix), Arrays (1D)
This lecture clarifies Postfix/Prefix inside cout, introduces break and continue, and covers 1D Arrays.
Key Concepts
- Postfix/Prefix in
cout:cout << x++;(Postfix): Prints the current value ofx, then incrementsx. Output of example is 3, then x becomes 4.cout << ++x;(Prefix): Incrementsxfirst, then prints the new value. Output of example is 4.
- Loop Control Statements: Used with
for,while, anddo-whileloops.break: Exits the loop immediately. In the example, the loop stops wheniequals 5, so it only prints 0, 1, 2, 3, 4.continue: Skips the rest of the current iteration and jumps to the next iteration. In the example, it skipsi==3, so it prints 1, 2, 4.
- 1D Arrays:
- Benefit: Stores multiple values of the same data type in a single variable.
- Syntax:
data_type array_name[size];. - Declaration Methods:
int x[5] = {1, 2, 3, 4, 5};(Initialize with size).int x[5];(Declare size, no values).int x[] = {1, 2, 3, 4, 5};(Initialize without size; compiler figures it out).
- Accessing Elements:
- Indexing starts at 0.
- The first element is
x[0], the second isx[1], etc.. - The last element of
x[5]isx[4].
- Accessing with Loops: A
forloop is the standard way to iterate through an array.for (int i=0; i<3; i++) { cout << x[i] << endl; }. - Modifying Elements: You can change elements directly (e.g.,
x[i]++;or++x[i];) inside a loop.
محتويات المحاضرة
اختبر فهمك للمحاضرة
تصحيح فوري بالذكاء الاصطناعي