Friday 30 August 2013

The “ goto” Statement | C Programming Language Tutorial pdf



The “ goto” Statement:

=> C supports the “goto" statement to branch unconditionally from one point to another in the program.
=> Although it may not be essential to use the “goto” statement in a highly structured language like "C", there may be occasions when the use of goto is necessary.
=> The goto requires a label in order to identify the place where the branch is to be made.
=> A label is any valid variable name and must be followed by a colon( : ).
=> The label is placed immediately before the statement where the control is to be transferred.
=> The label can be any where in the program either before or after the goto label statement.
=> During running of a program, when a statement like “goto begin;”
is met, the flow of control will jump to the statement immediately following the label “begin:” this happens unconditionally.
=> 'goto' breaks the normal sequential execution of the program.
=> If the “label:” is before the statement “goto label;” a loop will be formed and some statements will be executed repeatedly. Such a jump is known as a 'backward jump'.
=> If the “label:” is placed after the “goto label;” some statements will be skipped and the jump is known as a “forward jump”.

Write a program to detect the entered number as to whether it is even or odd. Use goto statement.
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
void main( )
{
int x;
clrscr( );
printf(“Enter a Number:”);
scanf(“%d”, &x);
if(x % 2 = = 0)
goto even;
else
goto odd;
even:
printf(“\n %d is Even Number”);
return;
odd:
printf(“ \n %d is Odd Number”);
}

Output:
Enter a Number : 5
5 is Odd Number.

ALGORITHM:
=> The approach or method that is used to solve a specific problem is known as an Algorithm.
=> A set of pseudo code or broken English steps written sequentially to solve a problem is termed as on algorithm.
=> Any documentation that clearly holds the procedure of problem solving, including pictorial representations are termed as algorithms.

Example:
Step 1: start
Step 2: - - -
Step 3: - - -
Step 4: stop
=> Algorithm is a step-by step procedure.
FLOW CHART:
=> The most common method of describing an algorithm is through the use of flowcharts.
=> The flow chart presents the algorithm pictorially.
=> All the operations to the performed and all the paths of processing to be followed while solving problem are indicated in a flow chart.
=> Flow charts are represented using different geometric shapes.

Flow Chart Symbols:

Example (1):
Program to find the sum of two given numbers.
# include<conio.h> /* including header file * /
# include<stdio.h>
main( )
{
int a,b, sum; /* Declaration of variables * /
clrscr( );
printf( “ \n Enter two numbers ”);
/* A message is displayed on the monitor indicating the action to be taken by the user */
scanf(“ %d %d”, &a, &b); /* The input statement follows */
printf( “ \n a = %d \t b = %d ”, a,b);
sum = a + b; /* computing the sum * /
printf( “ sum = %d \ n”, sum); /* printing the sum * /
getch( );
}

Flow Chart:

Example (2):
Program to compute the area of a triangle.
# include<conio.h> /* including Header files */
# include<stdio.h>
# include<math.h>
main( )
{
float a,b,c, s, area; /* declaration of variables */
clrscr( ) ;
printf( “ \n Enter the 3 sides of the triangle ”);
printf( “such that sum of any 2 sides is greater than 3rd side \n”);
scanf(“ %f %f %f ”, &a, &b, &c);
printf(“ \n 3 sides of the triangle are: a = %f \t b = %f \t c=%f ”, a , b , c);
s = (a+b+c)/2; /* computing value of s */
area = sqrt ( s* (s-a) * (s-b) * (s-c)); /* computing area */
printf( “ \n Area of the Triangle is: %f ”, area);
getch( );
}

Flow Chart:
Example (3):
Program to swap the contents of two variables
# include<stdio.h>
# include<conio.h>
main( )
{
int a, b, temp; /* Declaration of variables */
clrscr( ) ;
printf(“ \n Enter two numbers ”);
scanf(“ %d %d ”, &a, &b); /* input statement */
printf(“ \n a = %d \t b= %d ”, a,b);
temp = a;
a = b ;
b = temp; /* swapping contents of a & b */
printf(“After swapping \n a = %d \t b=%d ”, a, b);
getch( );
}

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved