Clear and organized handwritten-style notes covering If…Else Statements in C programming. These notes are designed for students studying Computer Programming Logic and Design and are useful for reviewing conditional statements and basic decision-making in C.
Download Computer Programming in C and more Summaries Computer science in PDF only on Docsity!
SOSH HHKHKHKHKHHKHHKHHHHHHHHHOS Date: 10/03 / 26 Page 1 IF... ELSE STATEMENT * allows selective execution of code. * if the condition is true -> one set of statements is executed. + if the condition is false — alternative set of statements may be executed. Basic Syntax (4) Simple (if (condition) { // statements (executed if true) CSsaik: @) If... else statement if (condition) { } // statements (executed if true) } else { // statements (executed if false) #include int main() { int number; printf (“Enter an Integer: "); scantf ("%d", &number); if (number % 2 == 0) { printf ("“%d is even.", number); } else { printf (“%d is odd.", number); return 0; Use when you need to execute a block of code only if a condition is true; no alternative action is required when the condition is false. Use when exactly two mutually exclusive paths must perform meaningful work. «In this example, the program checks if the number is even or odd and prints the result accordingly. SOOKE LEEESD Date: 10/03 / 26 Page 2 3. The else if Ladder if (condition1) { // statements } else if (condition2) { // statements } else if (condition3) { // statements } else { // statements } 4. Nested if statements Use when a series of mutually exclusive conditions and only one of them should be true at a time. (The first true condition is executed.) * Careful indentation and bracing are essential to maintain readability. if (condition1) { if (condition2) { // statements } else { // statements } } else { // statements } « This code checks conditioni first. If true, it then checks condition2. If condition2 is true, it executes the first block; otherwise, it executes the second block. If condition1 is false, it executes the outer else block.