Monday 8 August 2011

Selection: the if-else statement

Selection: the if statement

An if statement lets you conditionally process a statement.for i.e if a condition is true(non-zero).then it will executed and if a condition is false(zero) then it will not executed.for example

Let say
int i=16

if(i==16)
{
printf(“hie”);
}

here the condition is true because i is equals to 16.then the body following the if statement will be executed.

NOTE:- if can be work without of relational operators. we can put  also put an variable to fulfil if condition.


Let say
int i=16

if(i)
{
printf(“hie”);
}


This code also works, Now What are you thinking,? yes man...here if condition is works because the variable i have the value that is 16 and 16 is treated as a non-zero value. and every non-zero value treated as true.so this code will work.


IF has three basic forms:


/*simple if*/

 if(expression)
{
               Statement
}



 /*if with else*/

if (expression)
{
statement1
}
else
{
statement2
.}



/*if-else ladder*/

 if(expression)
{   
statement1
}

else if(expression)
{
statement2
}
else
{
statement3
}

the if-then-else.is a generalize form of if.for i.e
in certain cases we have two plans let say
if bike is not ready then I will prefer to go with in a bus.
in this example one thing is cleared that what about if a..if statement
condition fails..don’t get worried there is a else condition.if
a if condition fails then the program. continues with else condition.

The braces are not required if the body contains
only a single statement. However, they are a
good idea and are required by the 104 C Coding
Standards.

Example of if-else

if (number >= 0)
   printf("Number is positive\n");
else
   printf("Number is negative\n");

The following example displays Number is positive if the value of number is greater than or equal to 0. If the value of number is less than 0, it displays Number is negative.


 The Conditional Operator

  
The conditional operator is short-hand form of if-else
The conditional operator have the following form:

expr1? expr2: expr3
If expr1 is true then expr2 is executed, else expr3 is evaluated,
i.e.:
x = ((y < z)? y=5: z=10);

in the above example if y is less then z. then an assignment of 5 is take place in y.
otherwise an assignment of 10 is take place in z.
we can also write this as in an improved form.

 (y < z) ? printf (“%d is smaller”,y): printf(“%d is smaller”,y);



=  (assignment operator) versus ==  (equality test operator)

int a = 2 ;

if ( a = 1 ) /* semantic (logic) error! */
{
printf (“a is one\n”) ;
}
else if ( a = = 2 )
{
printf (“a is two\n”) ;
}
else
{
printf (“a is %d\n”, a) ;
}


The statement if (a = 1) is syntactically correct,
so no error message will be produced. (Some
Compilers will produce a warning.) However, a
Semantic (logic) error will occur.


If else with logical operators

we have already learned about how to use relational operators and what are their working in the chapter second of this tutorial. Now here we can see how we can implement the logical operators with if-else statement.
consider the following example for && logical operator.

if ( age < 1 && gender == ‘m’)
{
printf (“Infant boy\n”) ;
}


consider the following example for && logical operator.

if (grade == ‘D’ || grade == ‘F’)
{
printf (“See with your Juniors !\n”) ;
}

consider the following example for && logical operator.

if ( ! (x == 2) ) /* same as (x != 2) */
{
printf(“x is not equal to 2.\n”) ;
}


Some Practice Logical Operators Expressions

Int  a = 1, b = 0, c = 7;

Expression            Numeric Value            True/False
A                                  ??                                             ??
B                                  ??                                             ??
C                                  ??                                             ??
a + b                           ??                                             ??
a && b                        ??                                             ??
a || b                           ??                                             ??
!c                                 ??                                             ??
!!c                                ??                                            ??       
a && !b                       ??                                            ??       
a < b && b < c          ??                                            ??       
a > b && b < c          ??                                            ??
a >= b || b > c          ??                                            ??

4 comments:

  1. Thiѕ ԁesign iѕ incredible! You certainlу know hοw to keep a rеadeг amusеd.
    Betωeen your wit and your videοs, I ωas almost movеd to
    ѕtаrt mу own blog (well, almost...HaНa!
    ) Excellent job. I гeallу lоveԁ what
    you had to say, anԁ more thаn that, how уou pгеsеnted it.
    Тoo coоl!

    Check out my webѕіte :: pikavippi
    My page ::

    ReplyDelete
  2. Ι'd like to find out more? I'd wаnt to finԁ
    out mοre ԁetailѕ.

    Hеrе іs my web site; Samsung Galaxy note 2
    My web site ::

    ReplyDelete
  3. I usually dο not drοp many гemarks, however i did some searching and wounԁ up
    hеre "Selection: the if-else statement". And I actually do have a сouple of
    questіonѕ for you іf it's allright. Is it only me or does it give the impression like some of the remarks come across like they are written by brain dead people? :-P And, if you are writing on other online sites, I would like to keep up with everything fresh you have to post. Could you make a list of the complete urls of your shared sites like your Facebook page, twitter feed, or linkedin profile?

    my web-site :: pikavippii.Net
    My site :: pikavippi

    ReplyDelete
  4. Thanks for sharing your thoughts. I really appreciate your efforts and I am waiting for your further write ups thank you once again.


    My website ... best way to lose weight fast

    ReplyDelete