Function 'getch( )' is misused in programs
Generally when we pause our program's output screen by using the function 'getch( )' .
But the actual use of the function 'getch()' is to catch a single character from the keyboard dynamically.
So,when we use the 'getch( )' function in the end of the program,it actually dosen't pause the program or screen while execution, but it waits for the entry of any type of character for further execution of the program and then it gets terminated.
For example :- #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Hello Everyone /n";
getch(); //misused 'getch()' function
}
So, here after printing hello on the screen,the program waits for the entry of the charcater through the getch() function and when we press any key, the job is completed and the program finds no more
statements and hence terminates.
Hence, the program was actually waiting for the response but not pausing the execution.
-------------------------------------------------------------------------------------------------------------
*Use 'System( )' function for pausing the execution*
-------------------------------------------------------------------------------------------------------------
So,for actual pausing of the execution screen, we should use the 'system()' function.
for example :
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Hello Everyone /n";
system("pause"); // use of 'system()' function
}
Output will be as :
Hello Everyone
press any key to continue
Output will be as :
Hello Everyone
press any key to continue
What's Arrow Operator ??
Actually, arrow operator is used to tend down from any higher value to the lower value using the while loop in the programs.
---------------------------------------------------------------------------------------------------------
* Used for tending down any higher value to lower value*
---------------------------------------------------------------------------------------------------------
Lets say, we have the value of 'i=10'. and we want to move towards '0' by printing all these values of the variable 'i'.
Then, we can simply apply the 'arrow operator(-->)' in the while loop without using any decremental statement like 'i--' in the while loop for the variable 'i'.
So the Simple code for this example would be :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{ clrscr();
int i=10;
cout<<"Tending Down Value of Variable i is \n";
while(i-->) // Arrow Operator ( --> )
{
cout<<i<<"\n";
}
system("pause");
}
So, the output of the code will be :
Tending Down Value of Variable 'i' is
10
9
8
7
6
5
4
3
2
1
0
press any key to continue
--------------------------------------------------------------------------------------------------------
*It reduces the use of decremental statements in the loop*
--------------------------------------------------------------------------------------------------------
So, By using the 'arrow operator', we can get rid of the decremental statements in the while loop.
Here, this task could also be performed simply using the statements as :
while(i>0)
{
i--; // decremental statement ( i-- )
cout<<i<<"\n";
But , the 'arrow operator' makes this conditional useless.
So, By using the 'arrow operator', we can get rid of the decremental statements in the while loop.
Here, this task could also be performed simply using the statements as :
while(i>0)
{
i--; // decremental statement ( i-- )
cout<<i<<"\n";
}
But , the 'arrow operator' makes this conditional useless.


No comments:
Post a Comment