
Understanding Conditional Statements in ANSI C Programming
Explore the concept of conditional statements in ANSI C programming, including if statements, if-else statements, conditional expression operators, examples, nested conditional statements, and the sequence of nested if statements. Learn how these statements allow selective processing of code based on certain conditions, enhancing the flexibility and control flow of your programs.
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
COM101B LECTURE 7: CONDITIONAL STATEMENTS ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
A conditional statement allows selective processing of a statement or a group of statements. There are two forms of conditional statements: if, if-else stmts. There is also a conditional expression operator REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
CONDITIONAL EXPR. OPERATOR Has Ternary form: expr1 ? expr2 : expr2 expr1 is evaluated, if it is non-zero (true), the value of expr2 is the value of this expression; if it is zero (false), the value of expr2 is the value of this expr. Example: 3>5 ? 7 : 2 // 2 12>7 ? 1 : 3 // 1 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
IF STATEMENT Has this form: if (expr) statement expr is evaluated; if it is non-zero (true) statement is executed; if it is zero (false) statement is NOT executed. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
EXAMPLES if (my_age > 30) my_salary *= 1.1; if(age>18 && salary<100) { printf( Unemployed\n ); } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
IF-ELSE STATEMENT Has this form: if (expr) statement1 else statement 2 the value of expr is evaluated; if it s non-zero (true), statement1 is evaluated; if it s zero statement2 is evaluated. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
NESTED CONDITIONAL STATEMENTS Has this form: if (expr1) if(expr2) statement1 else statement2 else if(expr 3) statement3 else statement 4 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
SEQUENCE OF NESTED IFS if (expr1) if(expr2) if(expr3) ... if(exprN) statement can be written as: if(expr1 && expr2 && ... && exprN) statement REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
MULTIWAY CONDITIONAL STATEMENT if (expr1) statament1 else if(expr2) statement2 else if(expr3) statement3 .. else statementN REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
SWITCH STATEMENT Tests different values of the same expression is also a multieay conditional statement has this form: switch (expr) { case value1: statement1; break; case value2: statement2; break; ... default: statementN; break; } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992 // expr must be an integral expr. // cases must be constant integral expr.