C++ Aksi takdirde
Diğer if Bildirimi
else if
İlk koşul ise yeni bir koşul belirtmek için ifadeyi kullanın false
.
Sözdizimi
if (condition1) {
// block of code to be executed if
condition1 is true
} else if (condition2) {
// block of
code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false
and condition2 is false
}
Örnek
int time = 22;
if (time < 10) {
cout << "Good
morning.";
} else if (time < 20) {
cout << "Good
day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."
Örnek açıklama
Yukarıdaki örnekte, zaman (22) 10'dan büyüktür, bu nedenle ilk koşulfalse
. Bir sonraki koşul,
else if
ifadede de 'dir false
, bu yüzden koşul1 ve koşul2'nin her ikisi de else
olduğundan koşula geçiyoruz ve ekrana "İyi akşamlar" yazdırıyoruz.false
Ancak, saat 14 olsaydı, programımız "İyi günler" yazdırırdı.