5_if_else.pdf
Document Details
Uploaded by DoctorVisionaryAngel
Seneca Polytechnic
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- University of Santo Tomas Pre-Laboratory Discussion of LA No. 1 PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
Full Transcript
PRG 155 – Programming Fundamentals Using C 5. Decision Control Statements in C if statement if-else and else-if statements switch-case statements if Statement if statement is used to make a decision based on a condition. Condition can be an expression (examples: if(x + y...
PRG 155 – Programming Fundamentals Using C 5. Decision Control Statements in C if statement if-else and else-if statements switch-case statements if Statement if statement is used to make a decision based on a condition. Condition can be an expression (examples: if(x + y) or if(num1>5)) of a direct value (example: if(5)) If the expression produces zero, or direct value is zero, the condition becomes FALSE. For all other values, the condition is TRUE. Examples: if(10) is TRUE if(0) is FALSE if(x + y) is TRUE if (x+y) is not equal to zero if(num1 > 5) is TRUE if the value of variable num is greater than 5 1 PRG 155 – Programming Fundamentals Using C Simple if statement The if statement evaluates the condition. If it is TRUE, it executes the block of code that follows. If the condition is FALSE, it will skip this code. Used when there is only one option that is executed or not based on a condition. Syntax if Statement Flowchart Example: if (number < 0) //checks if number is a negative number { printf(“You entered negative number: %d”, number); } 2 PRG 155 – Programming Fundamentals Using C if - else statement The if-else evaluates the condition. If it is TRUE, it executes the block of code that follows. If the condition is FALSE, it executes the second block of code. Syntax if-else Statement Flowchart Example: if (number < 0) //checks if a number is a negative number { printf(“You entered negative number: %d”, number); } else { printf(“You entered zero or positive number: %d”, number); } 3 PRG 155 – Programming Fundamentals Using C Nested if statement The if-else statement is inside another if-else statement Syntax if (condition1) { if (condition2) { //statements to be executed if condition1 & condition2 are TRUE } else { } } else { //statements to be executed if condition1 is false } Example: if (number < 0) //checks if number is a negative number { printf(“You entered negative number: %d”, number); } else { if (number == 0) //checks if number is equal to zero { printf(“You entered zero”); } else { printf(“You entered positive number: %d”, number); } } 4 PRG 155 – Programming Fundamentals Using C if – else – if statement (if-else ladder) The if-else ladder allows for multiple test expressions to be checked and to execute different codes for more than two conditions. Syntax if (condition1) { //statements to be executed if condition1 is TRUE } else if (condition2) { //statements to be executed if condition1 is FALSE and condition2 is TRUE } else if (condition3) { //statements to be executed if condition1&condition2 are FALSE, // and condition3 is TRUE }... else { //statement to be executed if all conditions are FALSE } Example: if(number < 0) //checks if number is a negative number { printf(“You entered negative number: %d”, number); } else if (number > 0) //checks if number is a positive number { printf(“You entered positive number: %d”, number); } else { printf(“You entered zero”); } 5 PRG 155 – Programming Fundamentals Using C References Tan, H.H., and T.B. D’Orazio. C Programming for Engineering & Computer Science. USA: WCB McGRaw-Hill. 1999. Print. C if, if...else and Nested if...else Statement. N.p., n.d. Web. 09 Feb. 2017.. 6