Skip to content

Latest commit

 

History

History

chapter03

Chapter3- Conditional Instructions


In C language, we must be able to execute instructions on a condition..

Decision Making Instructions in C

1. If-else Statement

2. Switch Statement

If-else Statement

Syntax-


if(condition to be checked){
    Statements-if-condition-true;
}
else{
    Statements-if-condition-false;
}

example:-


int a=23;
if(a>18){
    printf("You can drive\n")
}

Note:- Else block is not necessary but optional.


Relational Operators

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 to
Note:- '=' is used for assignment but '==' is used for equality check

Logical Operators

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

Else if Clause

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

  1. 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

Operator Precedence

Priority                Operator
1st                     !                     
2nd                     *,/,%
3rd                     +,-
4th                     <,>,<=,>=
5th                     ==, !=
6th                     &&
7th                     ||
8th                     =

Conditional Operators

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 Control Instrcution

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:

Exercise--

  1. 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
  2. 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.
  1. Write a program to determine whether a character enterd by the user is lowercase or not
  2. write a program to find greatest of four numbers entered by the user.
  3. Write a program to determine the entered is a vowel or a constant.