Dec 12, 2015

Use of goto Statement

Using goto Statement

goto statement is used to jump on a particular location in a program defined by label. Label is a name given to a location. 
Label is defined by a <locationName> followed by a colon.
goto is use with goto <labelName>;

syntax of label & goto

statment 1;
label: //syntax of label
statment 2;
statment 3;
if(statement) //if condition true
{
goto label;
//jump to label i.e. after statement 1 and program will execute again statement 2 and 3
}


Let Check out Example for better understanding

Ex1: Print first 10 Natural Number
#include<iostream.h>
int main()
{
    int i=1; 
    print:
    cout << i <<endl; 
    if(i<10) 
    {
        i++; 
        goto print; //jump to print label as define above after initialization of i
    }
/*
In Above Program we are printing first 10 natural number using goto statment
in this example goto is used as loop to print 1 to 10
first we initialize the variable i to 1
then we define a checkpoint or label for using with goto
then we print value of variable i
we check if variable i is less than 10 if true then increment of variable i and jump to checkpoint or label we define earlier to print again this process continue till condition is false similar in a loop
*/
 return 0;
}

OUTPUT




Ex2: Divide Two Numbers [check this program using loop]
#include<iostream.h>
int main()
{
//Divide Two Numbers
    char ch;
    float a,b;
    tryAgain:
    cout << "Enter First Number : ";
    cin >> a; //Input First Number
    cout << "Enter Second Number : ";
    cin >> b; //Input Second Number
    if(b == 0) // Check for divisor for zero
    {
        cout << "Error: Number Cannot Divide by Zero"<<endl; //return error message
        goto tryAgain;
    }
    cout << "Answer = " << a/b<<endl;

    cout << "Try Again(y/n): ";
    if(cin >> ch && ch != 'n') 
        goto tryAgain;

  /*
In this example we are dividing two number and check for divisor not equal to zero
first we define label before taking input from user 
then we take input two numbers 
if (divisor(here b) = 0) then print error message and goto label; that jump back to a point where we have to take input again
now if divisor is not zero then we print answer and ask user for more
here you can see expression in if condition 
if(cin>> ch && ch != 'n')
above if statement has two conditions for check as there is AND(&&) between them means both must be true
check each conditions cin >> ch this will return true when it take input successfully 
and then it compare ch != 'n'
means here we are do two operation that first we take input and check for equality in one statement
second condition will run only when we get input from first statement
so if its true then it goto label; 
else <end of program>
*/
    return 0;
}

Output



Dec 11, 2015

Use of Continue Statement

Using Continue Statement

Continue statement is used to skip the current iteration and force to execute next iteration by skipping any code after continue statement inside a loop.
In simple term whenever you encounter continue statement in a loop it will skip the current pass execute next pass and also it skip the other statement below the continue statement inside a loop.

Let check the following examples for better understanding:

Ex1: Print Odd Number
#include<iostream.h>
int main()
{
//Print odd number
for(int i=1; i<=10;i++)
 {
    if(i%2==0)
    continue;
/*
 Above two lines first line check if i is even or not, If its even then i%2 return zero
means condition is true for even number of i then continue statement will execute.
As you can see in output we are printing only odd numbers so whenever continue statement runs it will skip all next line inside a loop and go to next iteration that increment of i
*/
    cout << i<<endl;
 }
return 0;
}

OUTPUT





Ex2: Divide Two Numbers
#include<iostream.h>
int main()
{
//Divide Two Numbers
char ch;
float a,b;
do //Using Do While Loop
 {
  cout << "Enter First Number : "; 
  cin >> a; //Input First Number
  cout << "Enter Second Number : ";
  cin >> b; //Input Second Number
  if(b == 0) // Check for divisor for zero
   {
    cout << "Error: Number Cannot Divide by Zero"<<endl; //return error message
    continue; 
/*
In this whenever we encounter with divisor = 0 then error message is display
and continue statement will execute. Whenever Continue statement runs it will skip all the code after that inside a loop and start from next iteration. As in this example we are using Do while loop which check condition at the end but we already skip the statements after continue so we will don't check the condition and we will start loop from begin. As you can seen in Output
*/
   }
  cout << "Answer = " << a/b<<endl;
  
  cout << "Try Again(y/n): ";
  cin >> ch; 
 }while(ch != 'n');

 return 0;
}

Output



Oct 5, 2013

Program: Display Fibonacci Series IN C++

Program : DISPLAY FIBONACCI SERIES IN C++

Check out Program

#include<iostream.h>
#include<conio.h>
// Display Fibonacci Series 
// In Fibonacci Series first two numbers in the Fibonacci series are 0 and 1, and each subsequent number is the sum of the previous two.
int main()
{
int a=0,b=1, c,n; 
cout << "Enter the value of n ";
cin >> n; // n store the integer value that series will shown upto n terms
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
/* First we Print first Two Term of Series then using for loop we generate rest of series upto n term. Let Begin with loop first it begin from 0 till the n-2 term because we already print first two term of series, then inside loop we initialize c = a + b, c contain value sum of a + b which become the third term of series. In Fibonacci series sum of previous two term is the next term. After getting value of next term and store in variable c. Then swapping of variable take place, now we don't require first term for next term(i.e. 4th term) so we replace value of a with b and replace value of b with c. Now 1st contain 2nd and 2nd contain 3rd, so they can generate 4th and so on. This process going on until the nth term of series. It will Print output after every assigning value of c.
*/
getch(); //http://cplspls.blogspot.com
return 0; 
}

OUTPUT:




Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
int a=0,b=1, c,n; 
cout << "Enter the value of n ";
cin >> n; 
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
getch(); //http://cplspls.blogspot.com
return 0; 
}

Oct 4, 2013

Program: Find Factorial of a Given Number

Program : FIND FACTORIAL OF A GIVEN NUMBER

Program to find factorial of a number using For Loop
Check out Program

#include<iostream.h>
#include<conio.h>
// Find factorial of a Given Number
int main()
{
long double factorial = 1; /* factorial has long double type which take 8 bytes in memory and it can store number within range of 1.7E -308 to 1.7E + 308 */
int n;
cout <<"Enter a Number : "<<endl; // endl for new line
cin >> n; // Take Integer value from user and store in variable n 

for(int i =n; i>0; i--)
     factorial *= i;
/* You Know Factorial is product of all positive number integers less than or equal to n. In above loop we initialize i equal to value of n that is taken from user, then set condition that i > 0 because we do multiplication here if i = 0 then it will give result 0. Then in loop statement is factorial * = i , it means that factorial = factorial * i , Now we already assign factorial value as 1, if we don't assign value 1 it will give error or junk value, because we are doing multiply if there is no number to multiply with it give junk value. In this we use decrement of value of i from n to 1, so it will multiply number like this n * (n-1) * (n-2) * ... * 2 * 1 . In this loop i didn't put braces because any loop or condition if you didn't put braces then it will automatic consider next line or till the termination(;) it will consider that part inside loop. So next line to loop inside the loop after that loop scope is finished.
*/
cout << n << "! = " << factorial <<endl; 
getch(); //http://cplspls.blogspot.com
return 0;
}

OUTPUT:






Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
long double factorial = 1; 
int n;
cout <<"Enter a Number : "<<endl; 
cin >> n; 

for(int i =n; i>0; i--)
     factorial *= i;

cout << n << "! = " << factorial <<endl; 
getch(); 
return 0;
}

Jan 28, 2012

Jan 11, 2012

Using Switch & break Statement

Using Switch & Break Statement

Switch statement is used instead of if statement using if statement multiple time can replace with switch and this make program short.
Break statement is used break that code proceed next like for loop i set to run 5 time and i write break it only run 1 time it skip braces part and continue to rest of code. Its used in loops and switch...

Jan 7, 2012