- 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
}
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)
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";
}
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";
}
breakShuningdek , va continuewhile sikllaridan ham foydalanishingiz mumkin :
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
cout << i << "\n";
i++;
}
Top comments (0)