Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Beginners Lab Assignments Code Examples

Types of Loops in C++ programming language

Types of Loops in C++ programming language Loop Type Description while loop While a given expression is true it repeats the statement in the loop body. Before executing the loop body it tests the condition for true or false. do...while loop It is like a while loop but it tests the condition after executing the loop body. for loop In above two loops we need to write the increment or decrement operation to break the loop after sometime. But in for loop we have an option of incrementing or decrementing outside the loop body. for-each loop This loop applies a function to the range of elements in a collection. nested loops When using one or more loops inside a loop is known as nested loop. Loop Control Statements Normally the statements inside the loop body executes sequentially. But by using loop control statements we can change the flow of execution of statements inside the loop body. If we are exiting the loop body then all automatic and local variables/objects which got created in loop's scope will be destroyed. C++ supports the three control statements as listed below. Click the following links to check their detail. Control Statement Description break statement Break terminates immediately the loop statement from executing further and execution reaches just outside the loop body containing the break statement. continue statement Continue statement is equivalent to going to the very end of the loop immediately by skipping further statements. goto statement It is equivalent to skipping the further statements and immediately jumping to the labeled statement.

Continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration. The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

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.

In C++, goto is a jump statement and sometimes also referred as unconditional jump statement. It can be used to jump from goto to a labeled statement within the same function. The target label must be within the same file and context. Please note that the use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making hard to understand and modify the program.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

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.

To convert temperature from "Fahrenheit" to centigrade in C++, enter the "temperature" in Fahrenheit to convert it to centigrade to print equivalent temperature value in "centigrade"

Program to check whether two lines intersect to each other. The above-below primitive can be used to test whether a line intersects a line segment. It does iff one endpoint of segment

Adding element to a node and check if node contains element. 'Adding string' in the table. Check if table contains string. Enter String to be inserted. Display table chained with binary

The Fibonacci sequence is a series where the next term is the "sum of pervious two terms". The first two terms of the Fibonacci sequence is 0 followed by 1. Fibonacci Numbers: 0, 1, 1,