DEV Community

BUTTSAN
BUTTSAN

Posted on

assalomu aleykum 28.08.24 biz c++ da nabatagi darsimizni otdik

  1. bu switch switchBajariladigan ko'plab kod bloklaridan birini tanlash uchun bayonotdan foydalaning . ex.
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
Enter fullscreen mode Exit fullscreen mode

Bu shunday ishlaydi:

Ifoda switchbir marta baholanadi
Ifodaning qiymati har birining qiymatlari bilan taqqoslanadicase
Agar mos keladigan bo'lsa, tegishli kod bloki bajariladi
breakva kalit so'zlari defaultixtiyoriy bo'lib, ular keyinchalik ushbu bobda tasvirlanadi
Quyidagi misolda ish kuni nomini hisoblash uchun ish kuni raqamidan foydalaniladi:
ex.

int day = 4;
switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
}
// Outputs "Thursday" (day 4)

Enter fullscreen mode Exit fullscreen mode

Tanaffus kalit so'zi
C++ kalit so'zga yetganda break , u switch blokidan chiqib ketadi.

Bu blok ichida ko'proq kod va holatlar testini bajarishni to'xtatadi.

Gugurt topilib, ish tugagach, tanaffus vaqti keldi. Ko'proq test o'tkazishga hojat yo'q.
2.break
C++ Break
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when i is equal to 4:

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  cout << i << "\n";
}
Enter fullscreen mode Exit fullscreen mode

Agar belgilangan shart yuzaga kelsa, bayonot continuebitta iteratsiyani (siklda) buzadi va tsikldagi keyingi iteratsiya bilan davom etadi.

Ushbu misol 4 qiymatini o'tkazib yuboradi:
\

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  cout << i << "\n";
}
Enter fullscreen mode Exit fullscreen mode

breakShuningdek , va continuewhile sikllaridan ham foydalanishingiz mumkin :

int i = 0;
while (i < 10) {
  if (i == 4) {
    i++;
    continue;
  }
  cout << i << "\n";
  i++;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)