Hayotiy misollar
Hayotiy misol
Bu misolda foydalanuvchi to‘g‘ri kodni kiritsa, "eshikni ochish" uchun if..else operatoridan qanday foydalanish mumkinligi ko‘rsatilgan:
Misol
int doorCode = 1337;
if (doorCode == 1337) {
cout << "Correct code.\nThe door is now open.\n";
} else {
cout << "Wrong code.\nThe door remains closed.\n";
}
O‘zingiz sinab ko‘ring »
Bu misolda sonning musbat yoki manfiy ekanligini aniqlash uchun if..else operatoridan qanday foydalanish mumkinligi ko‘rsatilgan:
Misol
int myNum = 10; // Is this a positive or negative number?
if (myNum > 0) {
cout << "The value is a positive number.\n";
} else if (myNum < 0) {
cout << "The value is a negative number.\n";
} else {
cout << "The value is 0.\n";
}
O‘zingiz sinab ko‘ring »
Insonning ovoz berish uchun yoshi yetarli ekanligini aniqlang:
Misol
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
cout << "Old enough to vote!\n";
} else {
cout << "Not old enough to vote.\n";
}
O‘zingiz sinab ko‘ring »
Insonning ovoz berish uchun yoshi yetarliligini va u fuqaro ekanligini aniqlang (ichma-ich if operatorlari yordamida):
Misol
int age = 20;
bool isCitizen = true;
if (age >= 18) {
cout << "Old enough to vote.\n";
if (isCitizen) {
cout << "And you are a citizen, so you can vote!\n";
} else {
cout << "But you must be a citizen to vote.\n";
}
} else {
cout << "Not old enough to vote.\n";
}
Sonning juft yoki toq ekanligini aniqlang:
Misol
int myNum = 5;
if (myNum % 2 == 0) {
cout << myNum << " is even.\n";
} else {
cout << myNum << " is odd.\n";
}
O‘zingiz sinab ko‘ring »
Haroratni tekshiring (Selsiy bo‘yicha):
Misol
int temperature = 30;
if (temperature < 0) {
cout << "It's freezing!\n";
} else if (temperature < 20) {
cout << "It's cool.\n";
} else {
cout << "It's warm.\n";
}
O‘zingiz sinab ko‘ring »
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
