Friday, 24 June 2016

Basic programming in C

Basic C structure




C Datatype

Type
Size
Value
Unsigned Char
8 bit
0 to 255
Signed char
8 bit
-128 to +127
Unsigned int
16 bit
0 to 65536
Signed int
16 bit
-32768 to +32768

Statement in C

  1. "if" statement
  2. "if...else" statement
  3. "else...if" statement
  4. "switch...case" statement
  5. "while loop" statement
  6. "Do...while loop" statement
  7. "for loop" statement 

if Statement

It takes an expression in parenthesis and a statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
Flow chart of if statement
if(condition1)

     {

      statement1;
      statement2;

     }
statement3;
statement4;

else...if Statement

It takes an expression in parenthesis and a statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise second condition check if this expression is true then statement gets execute otherwise statement skipped.
Flowchart of  else...if statement
if(condition1)

     {

      statement1;
      statement2;

     }
else...if(condition2)
    {
     statement3;
     statement4;
    }

if...else Statement

It takes an expression in parenthesis and a statement or block of statements. if the expression is true then the statement or block of statements gets executed if the expression is false then other statement are executed.
Flow chart of if...else statement
if(condition1)

     {

      statement1;
      statement2;

     }
else
    {
     statement3;
     statement4;
    }

Switch...case Statement

The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read.

switch(n)
   {
     case 0 : statement1;
     case 1 : statement2;
     case 2 : statement3;
     case 3 : statement4;
     default : default statement;
    } 

While loop statement

The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.
Flow chart of while loop statement
while(condition)

     {

      statement1;
      statement2;

     }
statement3;
statement4;

Do...while loop statement

do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.
Flow chart of do...while loop
do

     {

      statement1;
      statement2;

     }
while(condition)

statement3;
statement4;

for loop statement

for loop is similar to while, it's just written differently. for statements are often used to process lists such a range of numbers.

for(initialization;condition;increment/decrement)  // for(m=0;m<10;m++)
      {
      statement1;
      statement2;
      }
statement3;
statement4;

No comments:

Post a Comment