400+ Editable Festival Posters + Free Bonuses

PART 1 : Lesson 4 : Decisions Making Statements


Expression Statements
Compound Statements
Control Statements
if Statement
if-else Statement
Nested if-else Statement
else-if Ladder Statement
Forms of if
&& (Logical AND) Operator
|| (Logical OR) Operator
! (Logical Not) Operator


There are 3 types of statements in C and they are expression statements, compound statements and control statements.

Ø    Expression Statements:-

1.  Assignment Statement: - These are used to store values; usually the results of calculations are stored in variables.
2.     Input / Output Statement: - These are used to read in data and to print it out.
3.   Control Statement: - These statements are used to make a decision in the program about what to do next.

Ø    Compound Statements:-

These statement consists of a sequence of two or more consecutive statements enclosed in braces ({ and }). The enclosed statements can be expression statements, other compound statements or control statements.
                        Example:-
void main( )
{
            int a, b;
            scanf ("%d %d", &a, &b);
            {
                        int x, y;
                        scanf ("%d %d", &x, &y);
            }
}
                        In this example two compound statements are used.
           
Ø    Control Statements:-

The flow of statement execution can be controlled by means of various control statements. Every language has statements that can alter the flow of a sequence of execution of instructions. These statements are known as control statements.
                        Control statements are broadly classified into two types: -
1.                  Iteration statements (Conditional statement):-

Iterative statements check the condition only once. An iterative statement allows a program to test a condition and then choose which code to execute next. The iterative statements are: -

                                                                                                              i.      If statement
                                                                                                            ii.      Switch statement

2.                  Loop statements:-
                                                Here the condition is checked several times.



                        These statements are used to make decisions in C.


1.                  if Statement
2.                  if-else Statement
3.                  Nested if-else Statement
4.                  else-if Ladder Statement
5.                  Conditional Statement & operators (Logical operators)

1.                  if Statement:-
If the expression is true, the statement is executed. Then the next statement in the program sequence is executed.
If the expression is false, the statement is not executed. The next statement in the program sequence is executed.

                                    if (this condition is true)
                                                Execute this statement;           

 Conditions of if Statement:- 

if (x==y)
Equal
if(x!=y)
Not equal
if(x<y)
Less than
if(x>y)
Greater than
if(x<=y)
Less than or equal
if(x>=y)
Greater than or equal

                        Example:-
                        void main( )
                        {
                                    int n;
                                    printf("Enter a number less than 10");
                                    scanf("%d", &n);
                                    if (n<10)
                                    {
                                                printf("Valid value, Thank you");
                                    }
                        }


2.                  if-else Statement:-

if (this condition is true then go to true part) & otherwise go to else (this condition is false).
                                                                                    

                        Example:-
                        void main( )
                        {
                                    int n;
                                    printf("Enter any number=");
                                    scanf("%d", &n);
                       
                                    if (n<10)
                                    {
                                                printf("You enter single digit number. ");
                                    }

                                    else
                                    {
                                                printf ("You enter more than single digit number. ");
                                    }
                        }


3.                  Nested if-else Statement:-

            To use more than one if…..else statement in Nested form is called Nested if-else Statement.

            Example:-

/* W.A.P to calculate total salary. If salary less than 2000 than provided 20% bonus and if salary less than 5000 and greater than 2000 than provided 10% bonus otherwise no bonus. */

                        #include <stdio.h>
                        #include <conio.h>
                        void main( )
                        {
                                    int sal, bonus, total;

                                    clrscr( );

                                    printf("Enter salary = ");
                                    scanf("%d", &sal);

                                    if (sal<5000)
                                    {



                                                if (sal < 2000)
                                                {
                                                            bonus = 20;
                                                }
                                                else
                                                {
                                                            bonus = 10;
                                                }
                                    }
                                    else
                                    {
                                                bonus = 0;
                                    }

                                    total = sal + (sal * bonus/100);
                                    printf ("Total Salary %d", total);

                                    getch( );
                        }

4.                  else-if Ladder Statement:-
            When multipath decisions are involved. A multipath decision is a chain of if’s in which the statement associated with each else is an if.


Example:-

                                    // W.A.P to select a code and display color name of following code.

                        #include <stdio.h>
                        #include <conio.h>
                        void main( )
                        {
                                    int code;
                                    printf("Enter color code = ");
                                    scanf("%d", &code);

                                    if (code == 1)
                                    {
                                                printf ("Red Color");
                                    }
                                    else if (code == 2)
                                    {
                                                printf ("Green Color");
                                    }
                                    else if (code == 3)
                                    {
                                                printf ("Pink Color");
                                    }
                                    else
                                    {
                                                printf ("Black Color");
                                    }

                                    getch( );
                        }

            Forms of if:-




                        C allows usage of three types of logical operators.

1.                  && (Logical AND) All Conditions must be true then Executed, otherwise go to else.

2.                  ||      (Logical OR)    At least one Condition must be True then Executed, if both are                                              false then go to else.

3.                  !      (Logical Not)      If the Condition is True then Executed.


                  Syntax:-

1.                  && (Logical AND) Operator:

                        if (condition1 && condition2)
                        {
                                    --------
                                    --------
                        }
                        else
                        {
                                    -----------
                                    ------------
                        }


2.                  || (Logical OR) Operator:
                       
                        if (condition1 || condition2)
                        {
                                    -------------
                                    -------------
                        }
                        else
                        {
                                    ---------------
                                    ---------------
                        }


3.                  ! (Logical Not) Operator:
                                                           
                        if (! (condition))
                        {
                                    ------------
                                    ------------
                        }

0 comments:

Post a Comment

Awesome Gadgets for Students and IT People