if-else
- bir nechta shartlar berilganda ishlatiladi.
#include <iostream>
using namespace std;
int main()
{
int baho;
cin >> baho;
if (baho >= 90)
{
cout << "A" << endl;
}
else
{
if (baho > 80)
{
cout << "B" << endl;
}
else
{
if (baho > 70)
{
cout << "C" << endl;
}
else
{
if (baho > 60)
{
cout << "D" << endl;
}
else
{
cout << "F" << endl;
}
}
}
}
return 0 ;
}
- agar if yoki else dan so'ng amalga oshirilishi kerak bo'lgan buyruq bitta bo'lsa {} qo'yish shart emas.
#include <iostream>
using namespace std;
int main()
{
int baho;
cin >> baho;
if (baho >= 90)
cout << "A" << endl;
else
if (baho > 80)
cout << "B" << endl;
else
if (baho > 70)
cout << "C" << endl;
else
if (baho > 60)
cout << "D" << endl;
else
cout << "F" << endl;
return 0 ;
}
else-if
- readability ni oshirish uchun else dan so'ng keladigan if ni bir qatorda yozsa else-if hosil bo'ladi.
#include <iostream>
using namespace std;
int main()
{
int baho;
cin >> baho;
if (baho >= 90)
cout << "A" << endl;
else if (baho > 80)
cout << "B" << endl;
else if (baho > 70)
cout << "C" << endl;
else if (baho > 60)
cout << "D" << endl;
else
cout << "F" << endl;
return 0 ;
}
Top comments (0)