120 likes | 226 Vues
Dive into the world of conditionals in C programming with this comprehensive guide. Learn about relational operators for comparisons, logical operators for combining conditions, and the syntax for conditional branching using `if`, `if-else`, and nested `if` statements. Explore practical examples, such as salary increments based on age and class distinctions based on marks. By the end of this tutorial, you'll have a solid understanding of how to control the flow of your programs with conditionals. Perfect for beginners and anyone looking to enhance their C programming skills!
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…!”); }