Search This Blog

Tuesday, 9 September 2014

BRANCHING STATEMENTS

Conditional statements 
These statements can be used to change the flow of program after making some decision or condition.

Basically these are again sub categorized into four types as follows

1. If statement

2. If…..else Statement

3. Nested if statement

4. Switch - case statement


If Statement :


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.

 
 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 braces. If there are more than 1 (one) statements in if statement then use { } braces else it is not necessary to use.

Program :


 /*  Program to demonstrate if statement.
#include <stdio.h>
#include <conio.h>
void main()
{
     int a;
     a=5;
     clrscr();
     if(a>4)
         printf("\nValue of A is greater than 4 !");
     if(a==4)
         printf("\n\n Value of A is 4 !");
     getch();
}

Output :

Value of A is greater than 4 !_ 4 !_



If-Else Statement :


 
This is also one of the most useful conditional statement used in C to check conditions.

Syntax:
 
if(condition)
{
     true statements;
}
else
{
     false 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 it executes the else part of a program.
 
/*  Program to demonstrate if-else statement.
 
#include <stdio.h>
#include <conio.h>
void main()
{
     int no;
     clrscr();
     printf("\n Enter Number :");
     scanf("%d",&no);
     if(no%2==0)
         printf("\n\n Number is even !");
     else
                            printf("\n\n Number is odd !");
                            getch();
                          }

Output :
 
En Enter Number :11
 N Number is odd




Nested If-Else Statement :


It is a conditional statement which is used when we want to check more than 1 conditions at a time in a same program. The conditions are executed from top to bottom checking each condition whether it meets the conditional criteria or not. If it found the condition is true then it executes the block of associated statements of true part else it goes to next condition to execute.

Syntax:
 
if(condition)
 
{
 
Statements…..
 
     if(condition)
     {
         statements;
     }
     else
     {
         statements;
     }
}
else
{
 
Statements…..
 
     if(condition)
     {
         statements;
     }
     else
     {
         statements;
     }
 
 
}

In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the braces and again checks the next condition. If it is true then it executes the block of statements associated with it else executes else part.

Program :


/*  Program to demonstrate nested if-else statement
#include <stdio.h>
#include <conio.h>
void main()
{
     int no;
     clrscr();
     printf("\n Enter Number :");
     scanf("%d",&no);
     if(no>0)
     {
         printf("\n\n Number is greater than 0 !");
     }
     else
     {
         if(no==0)
         {
              printf("\n\n It is 0 !");
         }
         else
         {
              printf("Number is less than 0 !");
         }
     }
     getch();
}

Output :

Enter Number : 0
It is 0 !_

If..else if Statement
If else if structure is the other way of representing an if..else …if structure. i.e instead of using if statement again in the else part of the if …else structure we can use a single statement as “else if”. This statement works exactly the same as if..else if structure.

Syntax:  

If(condition)

{ statement to be done;
}

Else if(condition)
{
Statetements…….
}
Switch case Statement :

This is a multiple or multiway branching decision making statement.
When we use nested if-else statement to check more than 1 conditions then the complexity of a program increases in case of a lot of conditions. Thus, the program is difficult to read and maintain. So to overcome this problem, C provides 'switch case'.
Switch case checks the value of a expression against a case values, if condition matches the case values then the control is transferred to that point.

Syntax:
 
switch(expression)
{
     case expr1:
         statements;
             break;
     case expr2:
         statements;
        break;   case exprn:
         statements;
             break;                           
   default:
         statements;
}

In above syntax, switch, case, break are keywords.

 expr1, expr2 are known as 'case labels.'

Statements inside case expression need not to be closed in braces.

Break statement causes an exit from switch statement.

Default case is optional case. When neither any match found, it executes.

Program :


/*  Program to demonstrate switch case statement.
#include <stdio.h>
#include <conio.h>
void main()
{
     int no;
     clrscr();
     printf("\n Enter any number from 1 to 3 :");
     scanf("%d",&no);
     switch(no)
     {
         case 1:
              printf("\n\n It is 1 !");
              break;
         case 2:
              printf("\n\n It is 2 !");
              break;
         case 3:
              printf("\n\n It is 3 !");
              break;
         default:
              printf("\n\n Invalid number !");
     }
     getch();
}

Output 1 :

Enter any number from 1 to 3 : 3
 
It is 3 !_



RULES FOR DECLARING SWITCH CASE :


·         The case label should be integer or character constant.

·         Each compound statement of a switch case should contain break statement to exit from case.

·         Case labels must end with (:) colon.

ADVANTAGES OF SWITCH CASE :


·         Easy to use.

·         Easy to find out errors.

·         Debugging is made easy in switch case.
     

CONTROL FLOW STATEMENTS



Control statements or Control structure

Generally ,  By default Complier executes the statements of a program from top to bottom sequentially  as in the order they appear in the
program., But some times need comes to deviate the Sequence of control.The statements which cause deviation from the sequence are called  as “Control statements or Control structures”.
These statements can be categorized into three types they are

1. Branching statements
2. Looping  statements

3. Jumping  statement

 

 

Monday, 15 April 2013

STRUCTURE UNIONS

Download structures and union notes 
Click here

Download Type def and enumerated data types

Click here

Read  structure and pointer  ,structure and function questions also.

There is no time to read new material now. better read what u have earlier (rahul or vikram)

                                  All the best 4 ur Exam

Tuesday, 9 April 2013

variable

Variable is a name of memory location which is used to store constant values.
 Or
Variable is a name given to memory cells (location) of a computer where constant value is stored. Unlike constant variable values may change during the program execution.( It is opposite to constant).
Syntax
<data_type><variable_name>=Constant value;
                 EX: int a=5;
                     or
<data type><variable name>;
Ex:int  a;
Rules for variables:
·         First character should be letter or alphabet.
·         Keywords are not allowed to use as a variable name.
·         White space is not allowed.
·         C is case sensitive i.e. UPPER and lower case are significant.
·         Only underscore, special symbol is allowed between two characters.
·         The length of identifier may be up to 31 characters but only only the first 8 characters are significant by compiler.
·         (Note: Some compilers allow variable names whose length may be up to 247 characters. But, it is recommended to use maximum 31 characters in variable name. Large variable name leads to occur errors.)
Identifier :
    Identifier is the name of a variable that is made up from combination of alphabets, digits and underscore.


 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code