In C language, we must be able to execute instructions on a condition..
if(condition to be checked){
Statements-if-condition-true;
}
else{
Statements-if-condition-false;
}
int a=23;
if(a>18){
printf("You can drive\n")
}
Note:- Else block is not necessary but optional.
used to evaluate conditions(True or false) inside the if statement.
- These are binary operators
- Relational expression always evaluated to either true or false
- Used to form conditions in programming
- Lower priority than arithmetic operators
== equals >= greater than or equal to > greater than < smaller than <= smaller than or equal to != not equal toNote:- '=' is used for assignment but '==' is used for equality check
Logical operators are used to combine two or more conditions logically.
Used to provide logic to our C program.
Logical 'AND' Operator -- '&&' is true when both conditions are true Logical 'OR' operator-- '||' is true when at least one of the conditions is true Logical 'NOT' Operator-- '!' returns true if given false and false if given true
instead of using multiple if statements, we can also use else if along with if thus formatting an if-else-if-else ladder.
if (/* condition */)
{
/* code */
}
else if (/* condition */)
{
/* code */
}
else
{
/* code */
}
5. A basic Program on else-if clause taking numbers and telling what the numbers are
7. The marks obtained by a student in 5 different subjects are input. Tell a devision as per the following rules--
- if marks are equal or above than 60- 1st devision
2. if marks are between 45 and 60- 2nd devision
3. if marks are between 33 and 45- 3rd devision
4. If marks are below 33-- fail
Priority Operator 1st ! 2nd *,/,% 3rd +,- 4th <,>,<=,>= 5th ==, != 6th && 7th || 8th =
A shorthand "if-else" can be written using the conditional or ternary operators
condition?expression1-if-true:expression2-if-false E.G- x=(y>0)?1:0;
The conditional operator is used for making two way decision.This operator is a combination of ? and : .
- Denoted by ?
- It is the only ternary operator in C
- If condition is true,exp1 is evaluated
- If condition is false, exp2 is evaluated
- conditional operator can be used to replace simple "if-else" statement in symbolic form
Switch-case is used when we have to make a choice between number of alternatives for a given variable.
The control statement that allow to make a decision from list the no. of choices is called a switch statement.
switch (expression)
{
case /* constant-expression 1 */:
/* code */
case /* constant-expression 2 */:
/* code */
case /* constant-expression 3 */:
/* code */
default:
// code,
}
______________________________________
case:
break;
default:
- write a program to find out whether a student is pass or fail, ifit requires total 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user
- Calculate income tax paid by an employee to the govn as per the stats below-
Income Slab Tax 2.5L-5.0L 5% 5.0L-10.0L 20% above 10.0L 30% there is no tax below 2.5L.