C++ Programming Code Examples
C++ > For Loops and While Loops Code Examples
For finite loops we should have an expression which should give a false value some how.
1
2
3
4
5
6
7
8
9
10
11
12
/* For finite loops we should have an expression which should give a false value some how.
In below code the expression will give false value if value of x will be greater than or equal to 10. Since we are incrementing value of x so after some time condition x < 10 will return false and while loop will terminate. */
int x = 0;
while (x < 10) // the condition is "x < 10"
{
++x; // statement executed in loop
}
cout << "Now x is " << x << endl;
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.
The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Prints a total number of combination possible for given n and r value. The "time complexity" of this algorithm is O(n). This algorithm takes the input of n and r value. Function to find the
This is a C++ Program to check whether tree is Subtree of another tree. Given two binary trees, check if the first tree is subtree of the second one. A subtree of a tree T is a tree S
Test if the stack is logically full. Return true if full, false otherwise. Get the "most recently" inserted item in the stack. Does not alter the stack. Return the most recently inserted item
Complex numbers are entered by the user. In the problem, two complex numbers entered by the user is stored in structures 'n1' and 'n2'. These are passed to 'addComplexNumbers()'