Wednesday, 19 September 2012

Continue.break and goto statement

Continue statement
Continue statement transfer the control to the start of block.It is used in the body of loop
Syntax 

Example


break statement
It is a tool to take out the control.It transfers control to end of block.It is used in body of loop and switch statements.
Example





Flow chart





Syntax















goto statement
It is an unconditional transfer of control.It transfers the control to the specific point.The goto statement is marked by label statement.Label statement can be used anywhere in the function above or below the goto statement.It is written as goto label;
where label is an identifeir
label: statement

Syntax


Example



Nested loop

A loop within a loop is called nested loop. In this the outer loop is used for counting rows and the internal loop is used for counting columns.Any loop can be used as inner loop of another loop.

Syntax
for (initialization;condition;increment/decrement)
{
  for(initialization;condition,increment/decrement)
  {
    statements(s);
    }
}

Flow chart


Example
#include<stdio.h>
#include<conio.h>
void main (void)
{
clrscr();
for(int a=1;a<=3;a++)
{
for(int s=1;s<=3;s++)
{
printf("*");
} //end of internal loop
printf("\n");
} //end of external loop
getch();
} //end of main

For loop

For loops are used when the number of iterations is known before entering the loop. It is also known as counter-controlled loop.
Syntax

for (initialization;condition;increment/decrement)
{
statement()s;
}

Flow chart

Example
#include<stdio.h>
#include<conio.h>
void main (void)
{
int n;
for(n=1;n<=5;n++)
{
printf("\n %d",n);
}
getch();
}

Do while loop

Do while loops are useful where loop is to be executed at least once. In do while loop condition comes after the body of loop.This loop executes one or more statements while the given condition is true.
Syntax
initialization
do
{
statement(s);
increment/decrement;
}
while (condition);

Flow chart

Example
#include<stdio.h>
#include<conio.h>
void main (void)
{
int a;
a=1;
clrscr();
do
{
printf("\n %d",a);
a++;
}
while (a<=5);
getch();
}

While loop

It executes one or more statements while the given condition remains true.It is useful when number of iterations is unknown.

Syntax

initialization
while (condition)
{
statement;
increment/decrement;
}

Flow chart




Example#include<stdio.h>
#include<conio.h>
void main (void)
{
int n;
n=1;
clrscr();
while (n<=5)
{
printf("\n %d",n);
n++;
}
getch();
}

loops

loop is used to repeatedly execute set of statement.Structure that repeats a statement is known as iterative,repetitive or looping construct.

Purpose:
  • Execute statements for a specified number of times.
  • To use a sequence of values.
Types
  1. While loop
  2. Do while loop
  3. For loop
Flow chart



Monday, 17 September 2012

Conditional operator

It is also a decision-making statement. It is an alternative to if-else.it is known as ternary operator as it has three operands.
Syntax:(condition)? true-case statement: false case statement;
Flow chart

Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int x,y;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&x,&y);
x>y ? printf("%d is larger",x): printf ("%d is larger",y);
getch();
}

Switch statement


Switch statement is alternative of nested if-else.it is executed when there are many choices and only one is to be executed.
Syntax:
switch(expression)
{ 
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;

default:
statement;
}
Flow chart
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char c;
clrscr();
printf("Enter an alphabet:");
scanf("%c",&c);
switch(c)
{
 case'a':
 case'A':
 printf("You entered vowel.");
 break;
 case'e':
 case'E':
 printf("You You entered vowel.");
 break;
 case'i':
 case'I':
 printf("You entered vowel.");
 break;
 case'o':
 case'O':
 printf("You entered vowel.");
 break;
 case'u':
 case:'U'
 printf("You entered vowel.");
 break;
 default:
 printf("You entered a consonant.");
}
getch();
}


Nested If


In nested-if statement if the first if condition is true the control will enter inner if. If this is true the statement will execute otherwise control will come out of the inner if and the else statement will be executed.
Syntax:
If (condition)
if(condition)
{
    statement(s);
}
else
{
   statement(s);
}
else
{
statement(s);
}
Flowchart


Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c;
clrscr();
printf("Enter three number:");
scanf("%d %d %d,&a,&b,&c);
if (a<b)
            if (a>c)
        printf("%d is a smallest number.",a);
        else
            printf("%d is a samallest number.",c);
        else
            if(b<c)
        printf("%d is smallest number.",b);
else
printf("%d is smallest number.",c);
getch();
}

If-else if statement



It can be used to choose one block of statements from many blocks of statements. The condition which is true only its block of statements is executed and remaining are skipped.
Syntax:
if (condition)
{   
   statement(s);
                        }

else if (condition)
{
   statement(s);
                           }

else
{
    (statement);
                         }
Flow Chart



Example:

#include<stdio.h>
#include<conio.h>
void main ()
{
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
            printf("The number is positive.");
else if (n<=0)
            printf("The number is negetive.");
else
            printf("The number is zero.");
getch();
}

If-else statement



In if-else statement if the condition is true, then the true statement(s), immediately following the if-statement are executed otherwise the false statement(s) are executed. The use of else basically allows an alternative set of statements to be executed if the condition is false.
Syntax:
If (condition)
   {
Statement(s);
}
else
{
   statement(s);
}
Flow chart:





Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else
printf("%d is not a leap year.",y);
getch();
}

If statement

The simplest if structure involves a single executable statement. Execution of the statement occurs only if the condition is true.
Syntax:
if (condition)
    statement;

Flow chart:








Example:

#include<stdio.h>
#include<conio.h>
void main ()
{
int marks;
clrscr();
{
printf("Enter your marks:");
scanf("%d",&marks);
if(marks >=50)
printf("CONGRATULATIONS...!!! you have passed.");
}
getch();
}

Limitation of if statement
The statement(s) are executed if the condition is true; if the condition is false nothing happens in other words we may say it is not the effective one. Instead of it we use if-else statements etc.

Selection Structure


Selection structures allow the computer to make decisions in your programs. It selects a statement or set of statements to execute on the basis of a condition.

Types:
·        If-else structure
·        Switch structure

If-else structure:
The if-else statement is an extension of the simple if statement. It executes one statement if it is true and other one if condition is false.

Switch structure:
It is used when there are many choices and only one statement is to be executed.

Control Structure

A statement that is used to control the flow of execution in a program is called control structure. It combines instruction into logical unit. Logical unit has one entry point and one exit point.

Types of control structures
1.     Sequence
2.     Selection
3.     Repetition
4.     Function call
1.  Sequence: Statements are executed in a specified order. No statement is skipped and no statement is executed more than once.

Flow Chart:


                          


For Example
#include<stdio.h>
void main()
int a;

{
int a=5;

printf(“Square of a = %d”,a);
}

  
      2.     Selection: 
ItIt selects a statement to execute on the basis of condition. Statement is executed when the condition is true and ignored when it is false e.g if, if else, switch structures.
 

Flow Chart:
                               
                     












For Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else
printf("%d is not a leap year.".y)
getch();
}
3.     Repetition: In this structure the statements are executed more than one time. It is also known as iteration or loop e.g while loop, for loop do-while loops etc.
Flow Chart:



For Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=5)
{
printf(“I Love Pakistan\n”);
a++;
}//end of while
}//end of main
4.    Function call: It is used to invite or call a piece of code or statement. In this case control jumps from main program to that piece of code and then returns back to main program.

Flow Chart:

 

Clear screen and size of function


The function used to clear the screen for the current executed program to display it output on screen.clrscr stands for clear screen.

Syntax:
clrscr();

sizeof() function

The function used to give the number of bytes occupied given value, that to find the size of the given value.

Syntax:
sizeof (operand/value);
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
printf(“size of char = %d”,sizeof(char));
getch();                   
}
Output:
size of char=1

Character Input


The specialized function used only to get or input character values from the user is called character inputs. This function allows entering value without pressing enter key.It gives two function for input character:
  •      getche();
  •     getch();
        These function are part of conio.h headerfile.

getche function


The getche() = get character echo. It is also used to get a single character from the keyboard during the program execution. When this function is executed, the character entered by the user is displayed on the screen.

Syntax:
Variable name=getche();
Example
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
char p;
printf("enter any character:");
p=getche();
printf("the character u entered=%c",p );
getch();
}

Output
enter any character:x
the character u entered=x

getch function

“The getch = get character. This function is used to get a single character input from the user” during execution of program. It also force to wait the output to stay on screen until any key pressed from keyboard.

Syntax:
  •      For value input: Variable name= getch();
  •      For screen holding at the end of program: getch();

Example:

     #include<stdio.h>
     #include<conio.h>
      void main ()
      {
      clrscr();
      char m;
      printf("Enter character:");
      m=getch();
      printf("The character you Entered=%c",m );
      getch();
      }

     Output:
     Enter character:
     The character u Entered=a

gets function


This special function is used to input string values from the user during execution of the program. As the user press enter key when typing after the string. A null character (\0) is automatically entered at the end of the string.
Syntax:
gets(variable name);
Example:

#include<stdio.h>
#include<conio.h>
void main ()
{
char str[];
clrscr();
printf("Enter a string:");
gets(str);
getch();
}
Output:
Enter a string:hello