C++

Topic: Object oriented programming

Explain about iteration structures (loops) ?

Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.The while loopIts format is:while (expression) statementand its functionality is simply to repeat statement while the condition set in expression is true.For example, we are going to make a program to countdown using a while-loop:// custom countdown using while#include <iostream>using namespace std;int main (){  int n;  cout << "Enter the starting number > ";  cin >> n;  while (n>0) {    cout << n << ", ";    --n;  }  cout << "FIRE!\n";  return 0;}output :Enter the starting number > 88, 7, 6, 5, 4, 3, 2, 1, FIRE!When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows the condition will be executed and repeated while the condition (n>0) remains being true.The whole process of the previous program can be interpreted according to the following script (beginning in main):    User assigns a value to n    The while condition is checked (n>0). At this point there are two possibilities:    * condition is true: statement is executed (to step 3)    * condition is false: ignore statement and continue after it (to step 5)    Execute statement:    cout << n << ", ";    --n;    (prints the value of n on the screen and decreases n by 1)    End of block. Return automatically to step 2    Continue the program right after the block: print FIRE! and end program.When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. In this case we have included --n; that decreases the value of the variable that is being evaluated in the condition (n) by one - this will eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific, when n becomes 0, that is where our while-loop and our countdown end.Of course this is such a simple action for our computer that the whole countdown is performed instantly without any practical delay between numbers.The do-while loopIts format is:do statement while (condition);Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. For example, the following example program echoes any number you enter until you enter 0.// number echoer#include <iostream>using namespace std;int main (){  unsigned long n;  do {    cout << "Enter number (0 to end): ";    cin >> n;    cout << "You entered: " << n << "\n";  } while (n != 0);  return 0;}output : Enter number (0 to end): 12345You entered: 12345Enter number (0 to end): 160277You entered: 160277Enter number (0 to end): 0You entered: 0The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, like in the previous case, where the user input within the block is what is used to determine if the loop has to end. In fact if you never enter the value 0 in the previous example you can be prompted for more numbers forever.The for loopIts format is:for (initialization; condition; increase) statement;and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.It works in the following way:initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).    statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.    finally, whatever is specified in the increase field is executed and the loop gets back to step 2.Here is an example of countdown using a for loop:// countdown using a for loop#include <iostream>using namespace std;int main (){  for (int n=10; n>0; n--) {    cout << n << ", ";  }  cout << "FIRE!\n";  return 0;}output : 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!The initialization and increase fields are optional. They can remain empty, but in all cases the semicolon signs between them must be written. For example we could write: for (;n<10;) if we wanted to specify no initialization and no increase; or for (;n<10;n++) if we wanted to include an increase field but no initialization (maybe because the variable was already initialized before).Optionally, using the comma operator (,) we can specify more than one expression in any of the fields included in a for loop, like in initialization, for example. The comma operator (,) is an expression separator, it serves to separate more than one expression where only one is generally expected. For example, suppose that we wanted to initialize more than one variable in our loop:for ( n=0, i=100 ; n!=i ; n++, i-- ){   // whatever here...}This loop will execute for 50 times if neither n or i are modified within the loop:n starts with a value of 0, and i with 100, the condition is n!=i (that n is not equal to i). Because n is increased by one and i decreased by one, the loop's condition will become false after the 50th loop, when both n and i will be equal to 50.

Browse random answers:

What is abstraction?
what are the disadvantages of copy constructor??
What will happen if I say delete this ?
What are the different types of polymorphism?
Differences of C and C++Could you write a small program that will compile in C but not in C++ ?
A positive integer entered through the keyboard. Write a function to obtain the prime factors of this number.For example : prime factors of 24 are 2,2,2 and 3,, where as prime factor of 35 are 5
What do you meant by active and passive objects?
Can the structures contain the constructor?If yes, then how?
plz tell how can i attach oracle database with c++?
Anything wrong with this code?T *p = new T[10];delete p; 
What do you mean by analysis and design? 
Q1.write C++ program to accept a string and display ascii value letter by letter then display sum of all ascii values.Q2.write C++ program to accept 20 nos and then display max & min. 
How can i create a program in C++which would input 5 random numbers, then it would arrange them in an ascending order, in descending order, from highest to lowest value and from lowest to highest value? 
What do you mean by binding of data and functions? 
Differentiate Aggregation and containment? 
How to write a program to read sequences of integer from keyboard.program should sum all even and odd numbers? 
How can we define a dynamic memory allocation in UNIX operating system?
What is the object-oriented design,inheritance and dynamic polymorphism in c++?
What are 2 ways of exporting a function from a DLL? 
What is an object? 
How can I make application program using c++, that means, execute as application that have setup and install on other computer run with out c++ compiler? 
Write a program on abstract class. ?
Given an integer N>0,write a recursive c++ function that writes the integer 1,2,.......N ? 
For the following C program#define AREA(x)(3.14*x*x)main(){float r1=6.25,r2=2.5,a;a=AREA(r1);printf("Area of the circle is %f", a);a=AREA(r2);printf("Area of the circle is %f", a);}What is the Output? 
What is encapsulation? 
What are the steps involved in designing? 
How can you tell what shell you are running on UNIX system? 
List out some of the object-oriented methodologies. 
Write programs using nested loops to produce the following design:** * ** * * * ** * ** 
Write a c++ programme in order to find the day you born,such that the programmer will take your birth date (day-month-year)? 
Imagine you're maintaining a big project which is having 50k+ lines of code and as part of enhancements, you received a request to add a functionality in one of the .cpp files. For this, you need to add a new function. How do you write a new fucntion without adding a prototype to the header file (because, if you add prototype in the headerfile, the compiler has to compile all the other files). So, the requirement is not to touch the header file but add functionality in the cpp file. How do you add? 
What are the defining traits of an object-oriented language? 
What is multithreading? 
Will it execute or not?
Explain about structure of a program ?
Explain about conditional structure if and else ?
Explain about iteration structures (loops) ?