logo

C'de iç içe if else ifadesi

Programlamanın temel yapılarından biri koşullu ifadeler . Bir programın belirli koşulların değerlerine bağlı olarak farklı yollar izlemesine izin verirler. C'de koşullu ifadeler kullanılarak uygulanır. if-else ifadeleri . Daha karmaşık senaryolarda, iç içe if-else ifadeleri Daha karmaşık kararlar almak için kullanılabilir. Bu blog yazısı, sözdizimi, örnek ve çıktı da dahil olmak üzere C'deki iç içe geçmiş if-else ifadelerinin derinlemesine bir açıklamasını sağlayacaktır.

Sözdizimi:

A iç içe if-else ifadesi bir if ifadesi diğerinin içinde if ifadesi . C dilinde iç içe if-else ifadesinin genel sözdizimi aşağıdaki gibidir:

 if (condition1) { /* code to be executed if condition1 is true */ if (condition2) { /* code to be executed if condition2 is true */ } else { /* code to be executed if condition2 is false */ } } else { /* code to be executed if condition1 is false */ } 

Gördüğünüz gibi dış if ifadesi iki olası yolu vardır: biri koşulun gerçekleşmesi için doğru , ve diğeri koşul olduğunda YANLIŞ . Koşul doğruysa program, dış if ifadesiyle ilişkili bloğun içindeki kodu çalıştırır. Ancak koşul yanlışsa program o bloğu atlayıp else bloğuna geçecektir. Dış if bloğunun içinde, koşulun doğru veya yanlış olmasına bağlı olarak iki olası yola sahip olabilen başka bir if ifadesi daha vardır.

Örnek:

Nasıl olduğunu göstermek için iç içe if-else ifadesi çalışırsa aşağıdaki örneği inceleyin. Diyelim ki bir sayıyı alan ve bu sayının doğru olup olmadığını kontrol eden bir program yazmak istiyoruz. pozitif Negatif , veya sıfır .

 #include int main() { int num; printf('Enter a number: '); scanf('%d', &num); if (num > 0) { printf('%d is positive.
&apos;, num); } else { if (num <0) { printf('%d is negative.
', num); } else zero.
', return 0; < pre> <p> <strong>Output:</strong> </p> <p>Let&apos;s run the above program with some sample inputs and see the output.</p> <pre> Enter a number: 10 10 is positive. Enter a number: -5 -5 is negative. Enter a number: 0 0 is zero. </pre> <p> <strong>Explanation:</strong> </p> <p>In this program, we first prompt the user to enter a number, which is then read in using <strong> <em>scanf()</em> </strong> . After that, we use a nested if-else statement to check whether the number is <strong> <em>positive, negative</em> </strong> , or <strong> <em>zero</em> </strong> . The outer if statement checks whether the number is greater than zero. If it is, the program prints a message saying that the number is positive. If it is not, the program moves to the else block. Within the else block, there is another if statement that checks whether the number is less than zero. If it is, the program prints a message saying that the number is negative. If it is not, the program moves to the else block, which is the final block. This block prints a message saying that the number is zero. As you can see, the program correctly identifies whether the input number is positive, negative, or zero, and prints the appropriate message.</p> <h2>Conclusion:</h2> <p>In conclusion, <strong> <em>nested if-else statements</em> </strong> are an important construct in programming that allows a program to make more sophisticated decisions based on multiple conditions. In C, nested if-else statements are implemented using an if statement inside another if statement. The syntax of nested if-else statements is straightforward, and the example we discussed in this blog post demonstrates how to use nested if-else statements to check whether a number is positive, negative, or zero. By using nested if-else statements, we can write programs that are more complex and able to make more sophisticated decisions based on multiple conditions.</p> <p>It is important to note that nested if-else statements can quickly become unwieldy if there are too many conditions to check. In such cases, it may be more appropriate to use other control flow constructs, such as <strong> <em>switch statements</em> </strong> or <strong> <em>loops</em> </strong> . Additionally, it is important to ensure that nested if-else statements are properly indented and formatted to improve code readability and maintainability.</p> <p>Additionally, it&apos;s important to ensure that the conditions used in nested if-else statements are well-defined and cover all possible cases. Failure to do so can result in unexpected behavior and errors in your program.</p> <hr></0)>

Açıklama:

Bu programda, önce kullanıcıdan bir sayı girmesini isteriz ve bu sayı daha sonra kullanılarak okunur. tarama() . Bundan sonra sayının olup olmadığını kontrol etmek için iç içe geçmiş if-else ifadesini kullanırız. pozitif Negatif , veya sıfır . Dış if ifadesi sayının sıfırdan büyük olup olmadığını kontrol eder. Eğer öyleyse, program sayının pozitif olduğunu belirten bir mesaj yazdırır. Değilse program else bloğuna geçer. else bloğunun içinde sayının sıfırdan küçük olup olmadığını kontrol eden başka bir if ifadesi vardır. Eğer öyleyse, program sayının negatif olduğunu belirten bir mesaj yazdırır. Değilse program son blok olan else bloğuna geçer. Bu blok, sayının sıfır olduğunu söyleyen bir mesaj yazdırır. Gördüğünüz gibi program, giriş numarasının pozitif mi, negatif mi yoksa sıfır mı olduğunu doğru bir şekilde tespit ediyor ve uygun mesajı yazdırıyor.

Çözüm:

Sonuç olarak, iç içe if-else ifadeleri Bir programın birden fazla koşula dayalı olarak daha karmaşık kararlar almasına olanak tanıyan, programlamada önemli bir yapıdır. C'de, iç içe geçmiş if-else ifadeleri, başka bir if ifadesinin içindeki bir if ifadesi kullanılarak uygulanır. İç içe if-else ifadelerinin sözdizimi basittir ve bu blog yazısında tartıştığımız örnek, bir sayının pozitif, negatif veya sıfır olup olmadığını kontrol etmek için iç içe if-else ifadelerinin nasıl kullanılacağını gösterir. İç içe geçmiş if-else ifadelerini kullanarak, daha karmaşık ve birden çok koşula dayalı olarak daha karmaşık kararlar alabilen programlar yazabiliriz.

Kontrol edilecek çok fazla koşul varsa iç içe if-else ifadelerinin hızla kullanışsız hale gelebileceğini unutmamak önemlidir. Bu gibi durumlarda aşağıdakiler gibi diğer kontrol akışı yapılarını kullanmak daha uygun olabilir: anahtar ifadeleri veya döngüler . Ek olarak, kodun okunabilirliğini ve sürdürülebilirliğini geliştirmek için iç içe geçmiş if-else ifadelerinin uygun şekilde girintili ve biçimli olduğundan emin olmak önemlidir.

Ayrıca, iç içe geçmiş if-else ifadelerinde kullanılan koşulların iyi tanımlandığından ve tüm olası durumları kapsadığından emin olmak önemlidir. Bunu yapmamak, programınızda beklenmeyen davranışlara ve hatalara neden olabilir.