C++ Switch. Algo ubtuit uz dan namuna uchun misollar


Download 5.12 Kb.
Sana24.12.2022
Hajmi5.12 Kb.
#1057581
Bog'liq
C Switch. Algo ubtuit uz dan namuna uchun misollar-fayllar.org


C++ Switch. Algo ubtuit uz dan namuna uchun misollar

C++ Switch. Algo.ubtuit.uz dan namuna uchun misollar.

Amalga oshiriladigan ko'plab kod bloklaridan birini tanlash uchun switch operatoridan foydalaning.

Amalga oshiriladigan ko'plab kod bloklaridan birini tanlash uchun switch operatoridan foydalaning.

switch(expression) {

case x:

// code block

break;

case y:

// code block

break;

default:

// code block

}

Bu quyidagicha ishlaydi:

Bu quyidagicha ishlaydi:

switch ifodasi bir marta baholanadi.

Ifodaning qiymati har bir holatning qiymatlari bilan taqqoslanadi, agar mos keladigan bo'lsa, tegishli kod bloki bajariladi.

Quyidagi misol hafta kuni nomini hisoblash uchun ish kunining raqamidan foydalaniladi:

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)

Break kalit so'z.

Break kalit so'z.

C ++ so'zi break so'ziga etib kelganida, u switch blokidan chiqib ketadi.

Bu blok ichidagi ko'proq kod va ishni sinab ko'rishni to'xtatadi.

Break ijro vaqtini ancha tejashga imkon beradi, chunki u kommutatorlar blokidagi barcha qolgan kodlarning bajarilishini "e'tiborsiz qoldiradi".

Default kalit so'z.

Default kalit so'z, agar biron-bir holatga mos kelmasa, ishlash uchun ba'zi kodlarni belgilaydi:

int day = 4; switch (day) { case 6: cout << “Bugun Shanba"; break; case 7: cout << “Bugun Yakshanba"; break; default: cout << “Kiritilgan son ish kuni"; } // Outputs " Kiritilgan son ish kuni "

int day = 4; switch (day) { case 6: cout << “Bugun Shanba"; break; case 7: cout << “Bugun Yakshanba"; break; default: cout << “Kiritilgan son ish kuni"; } // Outputs " Kiritilgan son ish kuni "

#include


using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
cout << "Error! The operator is not correct";
break;
}
return 0;
}

#include

#include

using namespace std;

int main () {

char grade = 'D';

switch(grade) {

case 'A' :

cout << "Excellent!" << endl;

break;

case 'B' :

case 'C' :

cout << "Well done" << endl;

break;

case 'D' :

cout << "You passed" << endl;

break;

case 'F' :

cout << "Better try again" << endl;

break;

default :

cout << "Invalid grade" << endl;

}

cout << "Your grade is " << grade << endl;

return 0; }

You passed


Your grade is D
#include
#include
#include
using namespace std;
int main(){
int h, r;
cin >> h >> r;
double v = (M_PI * r *r * h)/3;
printf("%.2f", v);
return 1;
}

#include

#include

#include

#include

using namespace std;

int main(){

double x, y;

double a, b, c, d, f, g, h, res;

cin >> x >> y;

a = pow((x+y),2);

b = sqrt(fabs(y)+2);

c = x - (x*y)/(x*x/2-5);

d = log(fabs(a+b-c));

f = cos(x+y)*cos(x+y);

g = pow((x+y),1.0/3);

h = f/g;

res = d+h;

printf("%.2f", res);

return 0;

}

#include

#include

#include

using namespace std;

int main(){

int a,b,c;

cin >> a >> b >> c;

if(a>=b && b>=c){

a = 2*a;

b = 2*b;

c = 2*c;

}

else{

a = abs(a);

b = abs(b);

c = abs(c);

}

cout << a << " " << b << " " <

return 0;

}


http://fayllar.org


Download 5.12 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling