Showing posts with label programming-with-c. Show all posts
Showing posts with label programming-with-c. Show all posts

Thursday, 17 October 2013

Header Files in C and C++

Header files


You have already been using a header file from day-zero. You know that we used towrite at the top before the start of the main() function <iostream.h>, with ‘.h’ as an extension, you might have got the idea that it is a header file.
Now we will see why a Header file is used. In the previous lecture, we discussed a little bit about Function Prototypes. One thing is Declaration and other is Definition. Declaration can also be called as ‘Prototype’. Normally, if we have lot of functions and want to use them in some other function or program, then we are left with only one way i.e. to list the prototypes of all of them before the body of the function or program and they use them inside the function or program. But for frequent functions inside a program, this technique increases the complexity (of a program). This problem can be overcome by putting all these function prototypes in one file and writing a simple line of code for including the file in the program. This code line will indicate that this is the file, suppose 'area.h' containing all the prototypes of the used functions and see the prototypes from that file. This is the basic concept of a header file.

So what we can do is: Make our own header file which is usually a simple text file with '.h' extension ('.h' extension is not mandatory but it is but it is a rule of good programming practice).
Write function prototypes inside that file. (Recall that prototype is just a simple line of code containing return value, function name and an argument list of data types with semi-colon at the end.)
That file can be included in your won program by using the ‘#inclue’ directive and that would be similar to explicitly writing that list of function prototypes.

Function prototypes are not the only thing that can be put into a header file. If we use the standard  variables like ‘pi’ which is equal to 3.1415926 in a file and then include the file as a header file we will be able to use the variable ‘pi’ in any file which includes the containing file, without specifying every time the value of ‘pi’. It would be nice, if we can assign meaningful names to them. There are two benefits of doing this. See, we could have declared a variable of type double inside the program and given a name like ‘pi’:
double pi = 3.1415926;
Then everywhere in the subsequent calculations we can use 'pi'. But it is better to pre-define the value of the constant in a header file (one set for all) and simply including that header file, the constant ‘pi’, is defined. Now, this meaningful name ‘pi’ cab be used in all calculations instead of writing the horrendous number 3.1415926 again and again.
There are some preprocessor directives which we are going to cover later. Moment, we will discuss about ‘#define’ only. We define the constants using this preprocessor directive as:

#define pi 3.1415926

The above line des a funny thing as it is not creating a variable. Rather it associates a name with a value which can be used inside the program exactly like a variable. (Why it is not a variable?, because you can’t use it on the left hand side of any assignment.). Basically, it is a short hand, what actually happens. You defined the value of the ‘pi’ with ‘#difine’ directive and then started using ‘pi’ symbol in your program. Now we will see what a compiler does when it is handed over the program after the writing process. Wherever it finds the symbol ‘pi’, replaces the symbol with the value 3.1415926 and finally compiles the program. Thus, in compilation process the symbols or constants are replaced with actual values of them. But for us as human beings, it is quite readable to see the symbol ‘pi’. Additionally, if we use meaningful names for variables and see a line ‘2 * pi * radius’, it becomes obvious that circumference of a circle is being calculated. Note that in the above statement, ‘2 * pi * radius’; 2 is used as a number as we did not define any constant for it. We have defined ‘pi’ and ‘radius’ but defining 2 would be over killing.




Useful Things for Functions in Programming

Return-value_type:


Function may or may not return a value. If a function returns a value, that must be of a valid data type. This can only be one data type that means if a function returns an int data type than it can only return int and not char or float. Return type may be int, float, char or any other valid data type. How can we return some value from a function? The keyword is return which is used to return some value from the function. It does two things, returns some value to the calling program and also exits from the function. It does two things, returns some value to the calling program and also exits from the function. We can only return a value (a variable or an expression which evaluates to some value) from a function. The data type of the returning variable should match return_value_type data type.
There may be some functions which do not return any value. For such functions, the return_value_type is void. ‘void’ is a keyword of ‘C’ language. The default return_value_type is of data type i.e. if we do not mention any return_value_type with a function; it will return an int value.

Function-name:

The same rules of variable naming conventions are applied to functions name. Function name should be self-explanatory like square, squareRoot, circleArea etc.

Argument-list:

 Argument list contains the information which we pass to the function. Some function does not need any information to perform the task. In this case, the argument list for such functions will be empty. Arguments to a function are of valid data type like int number, double radius etc.

Declarations and Statements:

This is the body of the function. It consists of declarations and statements. The task of the function is performed in the body of the function.
//The function multiplies an integers to itself and returns the result
Int multiply(int my_number)
{
            Int my_result = 0;
            my_result = my_number * my_number;
            return result;
}

Calling Mechanism:

How a program can use a function? It is very simple. The calling program just needs to write the function name and provide its arguments (without data types). It is important to note that while calling a function, we don’t write the return value data type or the data types of arguments.


Scope of Identifiers in a Function

Scope of Identifiers


An 'Identifier' means any name that the user creates in his/her program. These names can be of variables, functions and labels. Here the scope of an identifier means its visibility. We will focus Scope of Variables in our discussion.
Suppose we write the function:
void func1() {
int i;
……..      //Some other lines of code
int j = i+2;    //Perfectly alright
. . .
}
Now this variable ‘i’ can be used in any statement inside the function func1(). But consider this variable being used in a different function like:
void func2() {
int k = i + 4; //Compilation error
. . .
}
The variable ‘i’ belongs to func1() and is not visible outside that. In other words, ‘i’ is local to func1().To understand the concept of scope further, we have to see what are Code Blocks? A code block begins with ‘{‘ and ends with ‘}’.Therefore, the body of a function is essentially a code block. Nonetheless, inside a function there can be another block of code like 'for loop' and 'while loop' can have their own blocks of code respectively. Therefore, there can be a hierarchy of code blocks.
A variable declared inside a code block becomes the local variable for that for that block. It is not visible outside that block. See the code below:
void func() {
            int outer; //Function level scope
            . . .
            {
                        Int inner;         //Code block level scope
                        inner = outer; //No problem
                        . . .
            }
inner ++;          //Compilation error
}
Please note that variable ‘outer’ is declared at function level scope and variable ‘inner’ is declared at block level scope.
The ‘inner’ variable declared inside the inner code block is not visible outside it. In other words, it is at inner code block scope level. If we want to access that variable outside its code block, a compilation error may occur.


            

Declaration and Definition of a Function

Declaration and Definition of a Function


Declaration and definition are two different things. Declaration is the prototype of the function, that includes the return type, name and argument list to the function and definition is the actual function code. Declaration of a function is also known as signature of a function. As we declare a variable like int x; before using it in our program, similarly we need to declare function before using it. Declaration and definition of a function can be combined together if we write the complete function before the calling function. Then we don’t need to declare it explicitly. If we have written all of our functions in a different file and we call these functions from mani() which is written in a defferent file. In this case, the main( ) will not be compiled unless it knows about the functions declaration. There we write the declaration of functions before the main() function. Function declaration is a one line statement in which we write the return type, name of the function and the data type of arguments. Name of the arguments is not necessary. The definition of the function contains the complete code of the function. It starts with the declaration statement with the addition that in definition, we do write the names of the arguments. After this, we write an opening brace and then all the statements, followed by a closing brace.

If the function square is defined in a separate file or after the calling function, then we need to declare it:

Delaration:

Double square (double);


Definition:
Double square (double number)
{
            return (my_num * my_num);
}

Here is the complete code to elaborate the topic more:

#include <iostream.h>

//Here we are declaring the function.
double square(double);

main(){
            double num, result;
            result = 0;
            num = 0;
            cout<<”Please Enter a Number to calculate the square of.”;
            cin>>num;
            //now we are going to call the function
            result = square (num);
            cout <<”The square of ”<<num<<”is”<<result;
            // function to calculate the square of a number
            double square ( double num)
{
                        return (num * num ) ;
}

}

A function in a calling program can take place as a stand-alone statement, on hand side of a statement. This can be a part of an assignment expression.




Functions in C and C++ Programming

Functions in Programming 

The functions are like subtasks. They receive some information, do some procedure and provide a result. Functions are invoked through a calling program. Calling program does not require to know what the function is doing and how it is performing its task. There is a specific function-calling methodology. The calling program calls a function by giving it some information and receives the result.
We have a main () in every C program. ‘main()’ is also a function. When we write a function, it must start with a name, parentheses, and surrounding braces just like with main(). Functions are very important in code reusing.
There are two categories of functions:
a)      Functions that return a value
b)      Functions that do not return a value
Suppose, we have a function that calculates the square of an integer such that function will return the square of the integer. Similarly we may have a function which displays some information on the screen so this function is not supposed to return any value to the calling program.
A Function Structure
The declaration syntax of a function is as follows:
Return-value-type function-name (argument-list)
{
            Declarations and statements
}

The first line is the function header and the declaration and statement part is the body of the function.

Goto Statement in Programming

“goto” Statement

Up to now we have covered the basic programming constructs. These include sequences, decisions and repetition structures (i.e. loops). In sequences, we use the simple statements in a sequence i.e. one after the other. In decisions construct we use the if statement, if/else statement, the multi way decision construct (i.e. the switch statement). And in repetition structures, we use the while, do-while and for loops.
Sometime ago, two computer scientists Gome and Jacopi proved that any program can be written with the help of these three constructs (i.e. sequences, decisions and loops).

There is a statement in the computer languages COBOL, FORTRAN and C. This statement is goto statement is used to jump the control anywhere (back and forth) in a program. In legacy programming, the programs written in COBOL and FORTRAN languages have many unconditional branches of execution. To understand and decode such programs that contain unconditional branches is almost impossible. In such programs, it is very difficult, for a programmer, to keep the track of execution as the control jumps from one place to the other and from there to anywhere else. We call this kind of traditional code as spagatti code. It is very difficult to trace out the way of execution and figure out what the program is doing. And debugging and modifying such programs is very difficult. When structured programming was started, it was urged not to use the goto statement. Though goto is there in C language but we will not use it in our programs. We will adopt the structured approach. All of our programs will consist of sequences, decisions and loops.

Continue Statement in C Programming

Continue Statement

(continue statement in c, continue statement in java, continue statement python, continue statement php, continue statement in c programming)


There is another statement relating to loops. This is the continue statement. Sometimes we have a lot of code in the body of a loop. The early part of this code is common that is to be executed every time (i.e. in every iteration of loop) and the remaining portion is to be executed in certain cases and may not be executed in other cases. But the loop should be continuous. For this purpose, we use the continue statement. Like the break statement, the continue statement is written in a single line. We write it as
                        Continue;
 The continue forces the immediate next iteration of the loop. So the statements of the loop body after continue are not executed. The loop starts from the next iteration when a continue statement is encountered in the body of a loop. One can witness very subtle things while using continue.
Consider the while loop. In while loop, we change the value of the variable of while condition so that it could make the condition false to exit the loop. Otherwise, the loop will become an infinite one. We should be very careful about the logic of the program while using continue in a loop. Before the continue in a loop. Before the continue statement, it is necessary to change (increment/decrement) the value of the variable on which the while condition depends. Similarly it is same with the do-while loop. Be careful to increment or decrement the conditional variable before the continue statement.
In for loop, there is a difference. In a while loop when continue is encountered, the control goes to the while statement and the condition is checked. If condition is true the loop is executed again else the loop exits. In a for loop, the three things i.e. initialization, condition and increment/decrement are enclosed together as we write
for (counter_variable = 0;  counter_variable <=5; counter_variable++).

In the for loop when a continue is encountered, the counter_variable (i.e. loop variable) is incremented at first before the execution of the loop condition. Thus, in for loop the increment to the loop variable is built in and after continue the next iteration of the loop is executed by incrementing the loop variable. The condition is checked with the incremented value of the loop variable. In while and do-while loop, it is our responsibility to increment the value of the loop variable to test the condition. In a for loop, the continue automatically forces this increment of value before going to check the condition. 

Nested If Statement in Programming

Nested if Statement 

(nested if statement in c, nested if statement java, nested if statement example, nested if statement php, nested if statement c++ examples, nested if statement python, nested if statement in java example, nested if statement matlab)

The 'if statement' is computationally one of the most expensive statement in a program. We call it costly due to the fact that the process has to proceed through many cycles to execute an if declaration to assess a single conclusion. So to make a program more effective, try to use the smallest number of if statements. This will make the performance of the program better.

So if we have distinct conditions in which only one will be true as glimpsed in the demonstration of student’s grades, the use of if statement is very costly. To bypass this expensiveness, an alternate of multiple if declarations can be utilized that is if/else declarations. We can compose an if statement in the body of an if declaration which is known as nested if. We can write the cipher of switch statement in the following nested if/else form.
If ( grade == ‘A’ )
cout << “Excellent” ;
else if ( grade == ‘B’ )
            cout << “Very Good” ;
else if ( grade == ‘C’ )
cout << “Good” ;
else if ( grade == ‘D’ )
cout << “Poor” ;
else if ( grade == ‘F’ )
            cout << “Fail” ;
In the code, there is single statement with each ifstatement. If there are more statements with an if statement, then don’t forget the use of braces and make sure that they match (i.e. there is a corresponding closing brace for an opening brace). Proper indentation of the blocks should also be made.

Break Statement in Programming

The Break Statement 

(break statement java, break statement in c, break statement python, break statement matlab, break statement in perl, use of break statement in c language, break statement c++ tutorial, break statement in c example)

The break declaration cuts off the flow of command. We have glimpsed in switch declaration that when a true case is discovered, the flow of control moves through every declaration down ward. We desire that only the declarations of factual case should be performed and the remaining should be skipped. For this reason, we use the break declaration. We compose the shatter declaration after the declarations of a case. Therefore, when a factual case is found and its declarations are executed then the break declaration cuts off the flow of command and control leaps out of the witch statement. If we want to do the identical task for two cases, like in preceding demonstration for ‘A’ and ‘a’, then we don't put break statement after the first case. We write both the situations ( or the situations may be more than two) line by line then write widespread statements to be executed for these cases. We write the shatter declaration after these widespread declarations. We should use the break statement is necessary after the declarations of each case. The shatter declaration is necessary in swap structure, without it the swap structure becomes illogical.  As without it all the declaration will execute after first agree case is found.

The break statement is also used in decision structures other than switch structure. We have seen that in while, do-while and for loops, we have to violate some condition explicitly to terminate the loop before its complete repetitions. As in a program for guessing a character, we make a variable myNum greater than 5 to violate the while condition and exit the loop if the correct character is guessed before five tries. In these loops, we can use the break statement to exit a loop. When a break statement is encountered in a loop, the loop terminates immediately. The control exits the inner most loop if there are nested loops. The control passes to the statement after the loop. In the guessing character example, we want that if the character is guessed in first or second attempt, then we print the message ‘Congratulations, You guess is correct’ and exit the loop. We can do this by using a break statement with an if statement. If the character is guessed, we print the message. Afterwards, the break statement is executed and the loop terminates. So we can write this as follows.

if ( c == ‘z’ ) // c is input from user
{
cout << “Great, Your guess is correct” ; break;
}
Thus, break statement can be used to jump out of a loop very quickly.

Switch Statement in Programming

Switch Statement and its Use

(switch statement c programming, switch statement c++, switch statement tutorial, switch statement example, switch statement syntax, switch statement as3, switch statement java example, switch statement in c language)



Switch Statement
Sometimes, we have multiple conditions and take some action according to each condition. For example, in the payroll of a company, there are many conditions to deduct tax from the salary of an employee. If the salary is less than Rs. 10000, there is no deduction. But if it falls in the slab Rs. 10000 – 20000, then the income tax is deducted. If it exceeds the limit of Rs. 20000, some additional tax will be deducted. So the appropriate deduction is made according to the category or slab of the salary. We can also understand this from the example of grades of a student. If the student has grade ‘A’ we print ‘Excellent’ and 'Very good', 'good', 'poor' and 'fail' for grades B, C, D, and F respectively. Now we have to see how this multi-condition situation can be applied in a program. We have a tool for decision making i.e. 'if statement'. We can use 'if statement' to decide what description for a grade should be displayed. So we check the grade in if statement and display the appropriate description. We have five categories of grades-- A, B, C, D, and F. We have to write five if statements to check all the five possibilities (probabilities) of grade. So we write this in our program as under-
if ( grade == ‘A’ )
cout << “Excellent” ;
if ( grade == ‘B’ )
cout << “Very Good” ;
 if ( grade == ‘C’ )
cout << “Good” ;
if ( grade == ‘D’ )
cout << “Poor” ;
if ( grade == ‘F’ )
 cout << “Fail” ;
These statements are correct and perform the required task. In the following code we are using nested if statement.
If ( grade == ‘A’ )
cout << “Excellent” ;
else if ( grade == ‘B’ )
            cout << “Very Good” ;
else if ( grade == ‘C’ )
cout << “Good” ;
else if ( grade == ‘D’ )
cout << “Poor” ;
else if ( grade == ‘F’ )
            cout << “Fail” ;
In the above example, we see that there are two approaches for a multi way decision. In the first approach, we use as many if statements as needed. This is an expensive approach. The second is the use of nested if statements. The second is little more-efficient than the first one. In the 'nested if statements' the nested else is not executed if the first if condition is true and the control goes out of the if block. The C language provides us a stand-alone construct to handle these instances. This construct is switch structure. The switch structure is a multiple-selection construct that is used in such cases (multi way decisions) to make the code more efficient and easy to read and understand. The syntax of switch statement is as follows.
switch ( variable/expression )
{
case constant1 : statementLlist1 ;
case constant2 : statementLlist2 ;
case constantN : statementListN ;
default : statementList ;
}
In the switch statement, there should be an integer variable (also include char) or an expression which must evaluate an integer type (whole numbers only, the decimal numbers 2.5, 14.3 etc are not allowed). We can’t use compound conditions (i.e. the conditions that use logical operators && or ||) in switch statement and in case statements. The constants also must be integer constants (which include char). We can’t use a variable name with the case key word. The default statement is optional. If there is no case which matches the value of the switch statement, then the statements of default are executed. The switch statement takes the value of the variable, if there is an expression then it evaluates the expression and after that looks for its value among the case constants. If the value is found among the constants listed in cases, the statements in that statementList are executed. Otherwise, it does nothing. However if there is a default (which is optional), the statements of default are executed.
Thus our previous grade example will be written in switch statement as below.
switch ( grade )
{
 case ‘A’ : cout << “Excellent” ;
case ‘B’ : cout << “Very Good” ;
case ‘C’ : cout << “Good” ;
case ‘D’ : cout << “Poor” ;
case ‘F’ : cout << “Fail” ;
}
We know that C language is 'case sensitive'. In this language, ‘A’ is different from ‘a’. Every character has a numeric value which is stored by the computer.. The numeric value of a character is known as ASCII code of the character. The ASCII code of small letters (a, b, c etc ) are different from ASCII code of capital letters (A, B, C etc). We can use characters in switch statement as the characters are represented as whole numbers inside the computers. Now we will see how the use of ' the letter a' instead of 'A' can affect our program. We want our program to be user- friendly. We don’t want to restrict the user to enter the grade in capital letters only. So we have to handle both small and capital letters in our program. Here comes the limitations of switch statement. We can’t say in our statement like case ‘A’ or ‘a’ : statements ; We have to make two separate cases so we write
 case ‘A” :
 case ‘a’ :
statements;
In the switch statement, the cases fall through the case which is true. All the statements after that case will be executed right down to the end of the switch statement. This is very important to understand it. Let's suppose that the user enters grade ‘B’. Now the case ‘A’ is skipped. Next case ‘B’ matches and statement cout << “Very Good” ; is executed. After that, all the statements will be executed. So cout << “Good” ; cout << “Poor” ;and cout << “Fail” ; will be executed after one another. We don’t want this to happen. We want that when a case matches, then after executing its statement, the control should jump out of switch statement leaving the other cases. For this purpose we use key work break.


Increment and Decrement Operators in Programming

Increment decrement operator in c
(increment decrement operator overloading c++, difference between increment and decrement operators, increment and decrement operators in java, increment and decrement operators in c, decrement operator in c)



We have seen that in while, do-while and for loop we write a statement to increase the value of a variable. For example, we used the statements like counter = counter + 1; which adds 1 to the variable counter. This increment statement is so common that it is used almost in every repetition structure (i.e. in while, do-while and for loop). The C language provides a unary operator that increases the value of its operator by 1. This operator is called increment operator and sign ++ is used for this. The statement counter = counter + 1; can be replaced with the statement
counter ++ ;
 The statement counter++ adds 1 to the variable counter. Similarly the expressions i = i + 1 ; and j = j + 1 ; are equivalent to i++ ; and j++; respectively. There is also an operator -- called decrement operator. This operator decrements, the value of its operand by 1. So the statements counter = counter - 1; and j = j - 1; are equivalent to counter--; and j--; respectively.

The increment operator is further categorized as pre-increment and post-increment. Similarly, the decrement operator, as pre-decrement and post-decrement. In pre-increment, we write the sign before the operand like ++j while in post-increment, the sign ++ is used after the operand like j++. If we are using only variable increment, pre or post increment does not matter. In this case, j++ is equivalent to ++j. The difference of pre and post increment matters when the variable is used in an expression where it is evaluated to assign a value to another variable. If we use pre-increment (++j ), the value of j is first increased by 1. This new value is used in the expression. If we use post increment ( j++ ), the value of j is used in the expression. After that it is increased by 1. Same is the case in pre and post decrement.

The operators ++ and -- are used to increment or decrement the variable by 1. There may be cases when we are incrementing or decrementing the value of a variable by a number other than 1. For example, we write counter = counter + 5; or j = j – 4;. Such assignments are very common in loops, so C provides operators to perform this task in short. These operators do two things they perform an action (addition, subtraction etc) and do some assignment. These operators are +=, -=, *=, /= and %=. These operators are compound assignment operators. These operators assign a value to the left hand variable after performing an action (i.e. +, -, *, / and %). The use of these operators is explained by the following examples.

Let’s say we have an expression, counter = counter + 5;. The equivalent of this expression is counter += 5;. The statement counter += 5; does two tasks. At first, it adds 5 to the value of counter and then assigns this result to counter. Similarly the  following expressions

x = x + 9;
x = x - 6 ;
 x = x * 8 ;
x = x / 4 ;
 x = x % 2;

can be written in equivalent short statements using the operators ( +=, -=, *=, /=, %= ) as follows:

x += 9;
x -=  6 ;
 x *= 8 ;
x /= 4 ;
 x %= 2;


Note that there is no space between these operators. These are treated as single signs. Be careful about the operator %=. This operator assigns the remaider to the variable. These operators are alternate in short hand for an assignment statement. The use of these operators is not necessary. A programmer may use these or not. It is a matter of style.

For Loop | For Loop in Programming

for loop c++,  for loop java, for loop vb, for loop matlab, for loop php
for loop vba, for loop oracle
Let’s see what we do in a loop. In a loop, we initialize variable(s) at first. Then we set a condition for the continuation/termination of the loop. To meet the condition to terminate the loop, we affect the condition in the body of the loop. If there is a variable in the condition, the value of that variable is changed within the body of the loop. If the value of the variable is not changed, then the condition of termination of the loop will not meet and loop will become an infinite one. So there are three things in a loop structure i.e. (i) initialization, (ii) a continuation/termination condition and (iii) changing the value of the condition variable, usually the increment of the variable value.
To implement these things, C provides a loop structure known as for loop. This is the most often used structure to perform repetition tasks for a known number of repetitions. The syntax of for loop is given below.
for ( initialization condition ; continuation condition ; incrementing condition )
{
            Statement(s);
}
We see that a ‘for statement’ consists of three parts. In initialization condition, we initialize some variable while in continuation condition, we set a condition for the continuation of the loop. In third part, we increment the value of the variable for which the termination condition is set.

Let’s suppose, we have a variable counter of type int. We write for loop in our program as

for ( counter = 0; counter < 10 ; counter = counter + 1 )
            {
                        cout << counter <<endl;
            }

This ‘for loop’ will print on the screen 0, 1, 2, …, 9 on separate lines ( as use endl in our cout statement). In for loop, at first, we initialize the variable counter to 0. And in the termination condition, we write counter < 10. This means that the loop will continue till value of counter is less than 10. In other words, the loop will terminate statement, we write counter = counter + 1 this means that we add 1 to the existing value of counter. We call it incrementing the variable.

Infinite Loop Problem C++ and in All Programming Langauges

c++ stream infinite loop, infinite loop problem c++, c++ infinite loop,  cin
make infinite loop c++, infinite while loop c++, how to stop an infinite loop in c++, 
c++ cin infinite loop
Consider the condition in the while structure that is (number<=upperLimit) and in the while block the value of number is changing (number = number + 1) to ensure that the condition is tested again next time. If it is true, the while block is executed and so on. So in the while block statements, the variable used in condition must change its value so that we have some definite number of repetitions. What will happen if we do not write the statement number = number + 1; in our program? The value of number will not change, so the condition in the while loop will be true always and the loop will be executed forever. Such loops in which the condition is always true are known as infinite loops as there are infinite repetitions in it.
Tips:
Always use the self explanatory variable names
Practice a lot. Practice makes a man perfect.
While loop may execute zero or more times.

Make sure that loop test (condition) has an acceptable exit. 

The Negation Operator in Programming Languages

The Negation Operator in Programming


There is another logical operator that is called logical negation. The sign ! is used for
this operator. This operand enables a programmer to ‘reverse’ the meaning of a
condition. This is a unary operator that has only a single condition as an operand. The
operator ! is placed before a condition. If the original condition (without the !
operator) is false then the ! operator before it converts it to true and the statements
attached to this are executed.
Look at the following expression
if ( ! (age > 18 ))
cout << “ The age is less than 18”;
Here the cout statement will be executed if the original condition (age > 18) is false
because the ! operator before it reverses this false to true.
The truth table for the logical negation operator ( ! ) is given below.


Expression
!Expression
True
False
False
True

negation operator in perl, negation operator in vba, negation operator in php,  negation operator python, negation operator in javascript, negation operator java