Thursday, July 16, 2015

Decision Control Structure:

There are some circumstances where we need different sets of action to be performed at a particular instant of time.A C language has three major decision making instructions------
1->The if statement.
2->The if else statement.
3->switch statement.
Apart from these three we have fourth type also i.e. conditional operators.
In this post I will show you about the if ,if else method, rest I will describe in another post.

The if Statement:

C uses a keyword if to implement the decision control instructions,it's syntax is
                               if(condition true)                                                                                                                                                  {    execute these set of statement;.....
                                                __________________}    
Flow Chart of" if "Structure

The if statement  tells the compiler that to check the condition is true or not enclosed within a pair of parentheses. If true then it will undergo the execution block of "if".otherwise proceed to other statements.
NOTE: In C a non zero value is considered to be true and 0 is considered to be false. i.e.
if(5)
//true statements are executed
if(-5)
//here too statements gets executed true
if(0)
//false it will terminate the if block

there are operators which can be used for particular conditions like:
RELATIONAL OPERATORS
1)
"= =" :Equal to
example:
 a==b means a is equal to b
2)
"!=" : Not equal to
example:
a!=b means a is not equal to b.

3)
"<" :Less then
example:
a<b means a is less then b.

4)
">":Greater then
example
a>b means a is greater then b.

5)
"<=" :Less then or equal to
example
a<=b means a is less then or equal to b
6)
">=" :Greater then or equal to
example
a>=b means a is greater then or equal to b.

LOGICAL OPERATORS
7) The logical And is represented as " && "

8) The logical OR is represented as "  || "

9) The logical NOT is represented as " ! "

The if -else Statement.

The main drawback of  if statement is that the following set of statements within "if block " executes only when the condition is true .It does nothing when expression evaluates to false.This is what exactly the purpose of  else statement  is being used.

syntax:


          if(condition)
         {........//statements; }
        else
    {,,,,,,,,,,,,,,,,,,,,,,,,,,//statements;}
Flow chart for " if-else"structure.
NOTE:The else block should be written exactly below the "if "block.

Nested if-else :
We can also write an entire if-else construct within either the body of if statement  or the body of else statement. This is called nesting of ifs
The example of nested if-else is shown below:
Nested if-else.









Tuesday, July 7, 2015

Types of C Instructions

As we all know that a Program is a set of Instructions.Different Instructions helps us  to achieve different task in the program.In previous post i described you about the simple C program,Now lets discuss over C instructions.
Generally there are three types of instructions:
1) Arithmetic Instructions:Such instructions are used to perform arithmetic operations on constants      and variables.
Example:

                     a=i.j.k(x+y)                   // a normal arithmetic expression                                                                                                  a=i*j*k*(x+y)                // a C statement.

2)Type Declaration: This is used to declare the type of the variable.These statements are usually    written at the beginning of main( ) function.
 NOTE: The order in which we define the variables is sometimes important sometimes not.
Example:
                   int i=5,j=2;
                   float a=5.3,b=a-2.6;    //these above two examples are acceptable however the below  declaration is not

                  (float b=a-2.6,a=5.3; ) is incorrect as here we are trying to use variable a ,even before defining it.

NOTE:In order to use any variable in instruction it must be defined in memory first . 
3)Control Instructions: These set of instructions are used to control the sequence of execution of  various statements.
There are four types of  control instructions in  C.
1) Sequence Control
2) Selection and Decision Control.
3)Loop or Repetition Control.
3)Case Control.

All these types you can see in programming part