Friday, 9 January 2015

Conditional Statement in C

If Else actually conditional statement in c

If Else actually conditional statement in C language.Conditioning in c language means to check something is correct or not. If it is correct then do some task else do another task. for this there are several syntax is available for e.g. 



Type 1:  Only if statement
                                                 if(condition) 
                                                 { 
                                                               //true block statement 
                                                  } 


Type 2  If else statement

                                                  if(condition) 
                                                  { 
                                                                // true block statement 
                                                  } 
                                                  else 
                                                  { 
                                                                //else block statement 
                                                  } 

Type 3: Else if condition
                                                if(condition) 
                                                { 
                                                                // true block statement 
                                                } 
                                                else if(condition) 
                                                { 
                                                                //else block statement 
                                                } 
                                               else if (condition) 
                                               { 
                                                                 //else block statement 
                                                } 
                                               else 
                                               { 
                                                                 //else block statement 
                                                }


What are the differences between if, else, and else if?



If, else and else if are all constructs to help 'branch' code. Basically, you employ them whenever you want to make a decision.

An example would be 'if it's sunny, I'll go outside. otherwise, I'll stay inside'

In code (ignoring the extra stuff)

if (sunny) {
  goOutside();
}
else {
  stayInside();
}
You CAN use 'else if' statements if you want to add 'additional' conditions. Extending the previous example, "if it's sunny, I'll go outside. If it's stormy, I'll go into the basement otherwise I'll stay inside"

In code

if (sunny) {
  goOutside();
}
else if (stormy) {
  goDownstairs();
}
else {
  stayInside();
}
EDIT section:

Here is how you can write multiple ifs as and conditions. The following example can be written in at least two ways:

'If it's sunny and warm, go outside. If it's sunny and cold, do nothing'

if (sunny) {
   if (warm) {
     goOutside();
   }
   else if (cold) {
     doNothing();
   }
}
OR

if (sunny && warm) {
   goOutside();
}
else if (sunny && cold) {
   doNothing();
}

No comments:

Post a Comment