120 likes | 219 Views
C Programming. (Conditionals) Date : 30 th Jan 2012. Relational Operators . Logical Operators. Conditional Branching : if ( Syntax ). if ( expression ) /* if expression is true */ { statement(s); }. Conditional Branching : if (example).
E N D
C Programming (Conditionals) Date : 30th Jan 2012
Conditional Branching : if (Syntax) if (expression) /* if expression is true */ { statement(s); }
Conditional Branching : if (example) if (age < 25) /* if expression is true */ { salary = salary * 1.1; printf(“\n\nThe salary is incremented by 10%.”); }
“if-else” Syntax if (expression) /* if expression is true */ { statement(s); } else /* if expression is false */ { statement(s); }
If-else (example) if (age < 25) /* if expression is true */ { salary = salary * 1.1; printf(“\n\nThe salary is incremented by 10%.”); } else /* if expression is false */ { salary = salary * 1.2; printf(“\n\nThe salary is incremented by 20%.”); }
“if-else if” Syntax if (expression 1) /* if expression 1 is true */ { statement(s); } else if (expression 2) /* if expression 2 is true */ { statement(s); } else /* if both expressions are false */ { statement(s); }
“if-else if” (example) if (marks > 60) /* if expression is true */ { printf(“First Class…!”); } else if (marks > 50)/* if expression is true */ { printf(“Second Class…!”); } else if (marks > 40) /* if expression is true */ { printf(“Pass Class…!”); } else /* otherwise */ { printf(“Failed…!”) }
Nested “if” (Syntax) if(expression 1) { if(expression 2) { statement(s); // if both expressions are true } }
Nested “if” (Example) if(age > 25) { if(age < 30) { printf(“Age is between 25 and 30…!”); } else { printf(“Age is more than or equal to 30…!”); } } else { printf(Age is less than or equal to 25…!); }
Logical Operators (example) if(age > 25 && experience > 5) { printf(“Qualified for promotion…!”); } ------------------------------------------------------------- if(age > 40 || experience > 10) { printf(“Qualified for promotion…!”); } ------------------------------------------------------------- if(! (age >= 18)) { printf(“You can’t get a driving license…!”); }