if else- c in my word

Upload: spaxyz

Post on 04-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 if else- c in My Word

    1/15

    As I mention my approach is toward the mu

    control statement-if else /*vivek*/

    Until now we study ,we give command and it takes value from keyboard and run the program but here

    something new .now we learn how to give particular direction to our program using some condional

    statements This is a conditional statement used in C to check condition or to control the flow of

    execution of statements. This is also called as 'decision making statement or control statement.' The

    execution of a whole program is done in one direction only.

    1)If

    Syntax:-

    if (condition)

    {

    statements;

    }

    in above syntax, the condition is checked first. If it is true, then the program control flow goes inside the

    braces and executes the block of statements associated with it. If it returns false, then program skips the

  • 7/29/2019 if else- c in My Word

    2/15

    braces. If there are more than 1 (one) statements in if statement then use { } braces else it is not

    necessary to use.

    Lets understand by flow chart

    1) enter the salary, if salary is less than50, 000 then give 20% salary as bonus.

    #include

    main( )

    {

    float x,bonus_rate=0;

    printf("enter the salary:-");

    scanf("%f",&x);

    if(x

  • 7/29/2019 if else- c in My Word

    3/15

    printf("now your salary is %f",x);

    }

    input:- 3000

    Output :-3600

    Input:-5000

    Output:-5000

    Live image of output :-

    How it works - Step by step

    1)initialize the variable and bonus rate

    2)take the value from keyboard

    3)if the value is less than 5000 then program run as it is and take bonus rate as 20and perform the

    calculation and print the calculated value

  • 7/29/2019 if else- c in My Word

    4/15

    4)if the value is more than 5000 if condition jump one statement and for computer one statement is

    nothing but one semicolon ( ; ) .therefore it jump the statement bonus_rate =20; and go to next

    statement here x=x+x*bonus_rate /100; but if condition goes false therefore it takes the bonus

    rate as 0 and perform calculation and give the output.

    C in my words

    1) Dont put ; after ifstatement. If statement is conditional statement if condition is true it go as it

    is but if condition goes wrong then it jump one statement and go as it is .but jumping of one

    statement is nothing but jumping of one semicolon because in computer language line is complete

    with ; so if we put ; after it nullify the effect of using if because it jump it jump if statement itself

    considering as statement then 20% bonus_rate is given to all

    2)initialization of bonus_rate=0 is another key statement .

    What happen if someone not initializes the bonus rate ?

    Ans In some compiler the bonus_rate is directly initialized to 0 but in some compiler

    it is not . In this case bonus_rate can take any garbage value and make calculation .so I prefer better

    to initialize it as 0 to avoid error.

    Multiple Statements within if

    2)write program to print the result.

    #include

    Void main( )

    {

    Int result ;printf("enter the mark:-");

    scanf ( "%d ", & result) ;

    if (result >=40)

    {

    printf("Passed\n");

    printf("Congratulations\n")

    }

    }

  • 7/29/2019 if else- c in My Word

    5/15

    Live output:-

    As I mention above If there are more than 1 (one) statements in if statement then use { } braces else it is

    not necessary to use. this program is just ex for that .

    C in my word:-

    1) Here when we write more the one statement using {} braces then whole block of brace {}consider as one statement and print as it is.

    2)If. Else

    The same above program we can also write using if else

    #include

    main( )

    {

    float x;

    printf("enter the mark:-");

    scanf("%f",&x);

    if(x>40)

    {

    printf("Passed\n");

    printf("Congratulations\n");

    }

  • 7/29/2019 if else- c in My Word

    6/15

    else

    {

    printf("Failed\n");

    printf("Good luck in the resist\n");

    }

    }

    Live output-

    How it works- step by step

    1)First step is initialization of var.

    2)enter the marks

    3)if your marks is above 40 then if condition is true and it print block of braces {} and program stop

    there and no further execution but if someone give value less than 40 then if statement become false

    and the whole block of braces {}is jumped because it is consider as one statement

    And program is further executed and directly print whole block of brace bracket as it is.

    C in my word :-

    when you are going for bigger programming making a small mistake it irritate you therefore you to avoid

    such mistake

  • 7/29/2019 if else- c in My Word

    7/15

    You improve yourself form the beginning of learning. If you are careful in reading you can observe the

    way I put the braces {}.I suggest you to after writing if condition give tab and then put the {} it avoid the

    confusion and if we make any mistake we can correct easily.

    Nested if statements

    If you use an if statement in an if statement it is called nesting. Nestingif statements can make a

    program very complex, but sometimes there is no other way. So use it wisely. Take a look at a nested

    if statement example below:

    Enter the grade if grade is liss then 10 print fail if grade is between 10 and 60 print pass and if grade is

    more than 60 print very good

    #include

    void main()

    {

    int grade;

    scanf("%d",&grade);

    if(grade

  • 7/29/2019 if else- c in My Word

    8/15

    1)If the grade is equal or below ten program stop there and print you are fail.

    2)If the grade is above ten, the program will go into the else statement. In the else statement there

    is another if statement (this is called as nesting).

    This if statement checks the grade again. If the grade is below sixty, you must study harder. In the

    else statement from this if statement there is another if statement. It checks whether the grade

    is above or equal to sixty. If it is, you passed the test.

    3)if grade is more than 60 then another else come in operation which print output as very good

    case(1): If grade is below 10

    only first if is evaluate

    Live output-

    Case (2):If grade is between 10 to 60

    program move to else because first if condition false but inside the else another if.else looping(i.e.

    nested if else)

    Inside the else compiler move to if condition here this if is satisfy

  • 7/29/2019 if else- c in My Word

    9/15

    Live output-

    Case(3): If grade is more than 60

    program move to else because first if condition is false but inside the else another if.else

    looping(i.e. nested if else)

    Inside the else compiler move to if condition here this if condition is also fails therefore compiler

    move to else this else statement is satisfy

    Live output-

  • 7/29/2019 if else- c in My Word

    10/15

    Multiple condition operators

    C three logical operators, namely, &&, || and !. These are to be read as AND OR and NOT

    respectively.

    There are several things to note about these logical operators. Most obviously, two of them are

    composed of double symbols: || and &&. Dont use the single symbol | and &. These single symbols

    also have a meaning. They are bitwise operators

    Write the above grade code program && and || also

    #include

    void main()

    {

    int grade;

    printf("enter the grade:- ");

    scanf("%d",&grade);

  • 7/29/2019 if else- c in My Word

    11/15

    if(grade10)&&(grade75)

    {

    printf("very good");

    }

    }

    C in my words

    1)here we are not using else

    2)for operator && we have use in bracket properly

    Ex: If( (grade>10)&&(grade

  • 7/29/2019 if else- c in My Word

    12/15

    With the OR ( || ) operator you can test if one of two conditions are true. Example:|

    If ( a=10 || b

  • 7/29/2019 if else- c in My Word

    13/15

    printf("you are fail");

    }

    else if (grade

  • 7/29/2019 if else- c in My Word

    14/15

    }

    }

    }

    From the comparison you can see that

    else

    { is replace by else if

    If

    4)Nested if

    Write program to check given no is divisible by 5 or 2 or both

    #include

    #include

    void main(){

    clrscr();

    int n;

    scanf("%d",&n);

    if(n%5==0) /*vivek*/

    {

    if(n%2==0)

  • 7/29/2019 if else- c in My Word

    15/15

    {

    printf("\n divisible 2");

    }

    else

    {

    printf("\n not divisible 2");}

    printf("\n divisible 5");

    }

    else

    {

    printf("\n not divisible 5");

    }

    getch();

    } /*vivek*/