A "for" loop combines the three steps into one statement. The three steps are initializing, testing, and incrememting. A statement consists of the keyword 'for' followed by a pair of parenthesis within the parenthesis are three statements separated by a colon for example
for(initialization; test; action)
{
......
}
Tuesday, 18 June 2013
Saturday, 15 June 2013
week 14 day 3
The condition tested in a while loop can only be a valid C++ expression. As long as that condition remains true, the while loop continues. You can create a loop that never ends bt using the value true for the condition to be tested.
#include<iostream>
int main()
int counter = 0;
while(true)
{
counter ++;
if(counter>0)
break;
}
std::cout<<"Counter: "<<counter<<std::endl;
return 0;
}
#include<iostream>
int main()
int counter = 0;
while(true)
{
counter ++;
if(counter>0)
break;
}
std::cout<<"Counter: "<<counter<<std::endl;
return 0;
}
week 14 day 2
At times you will want to return to the top of a while loop before the entire set of statements in the while loop is executed . The 'continue' statement jumps back to the top of the loop. At other times, you might want to exit the loop before the exit conditions are met. The 'break' statement immediately exits the while loop, and the program resumes after the closing brace.
Tuesday, 11 June 2013
week 14 day 1
when programming while loops, you'll often find yourself going through three steps; setting up a starting condition, testing to see whether it worked, and incrementing an other wise changing variable.eg:
#include<iostream>
int main()
{
int counter = 0;
while(counter>5)
{
counter++;
std::cout<<"Looping! ";
}
std::cout<<"\nCounter: "<<counter<<std::endl;
return 0;
}
#include<iostream>
int main()
{
int counter = 0;
while(counter>5)
{
counter++;
std::cout<<"Looping! ";
}
std::cout<<"\nCounter: "<<counter<<std::endl;
return 0;
}
week 13 day 3
many programming problems are solved by repeatedly acting on the same data. two ways to do this are recursion and acting on the same data. two ways to do this are recursion and iteration. iteration means doing the same thing over and over again.
if the primitive days of early computer science, programs were nasty, brutish and short. loops consisted of a label, some statements and a jump that went to the label.
in C++ a label is just a name followed by a colon (;). the label is placed to the left of a legal c++ statement. a jump is accomplished by writing 'gots' followed by the name of a label.
if the primitive days of early computer science, programs were nasty, brutish and short. loops consisted of a label, some statements and a jump that went to the label.
in C++ a label is just a name followed by a colon (;). the label is placed to the left of a legal c++ statement. a jump is accomplished by writing 'gots' followed by the name of a label.
week 13 day 2
then C++ was developed, it was built as an extension of the 'c' language. 'C' has structures, although 'c' structures dont have class methods. Bjarne Stroustrup, the creator of C++, built upon 'structs', but he changed the name to class to represent the new, expanded functionality, and the change in the default visibility of members. This also allowed the continued use of a vast library of 'c' functions in C++ programs.
week 13 day 1
a very close cousin to the keyword 'class' is the keyword 'struct' which is the came as a class, except that its members are public by default. you can declare a struct exactly the same way as a class, and you can give it the same data members and functions. in fact if you follow the good programming practice of always declaring the private and public sections of your class, nothing will be different
Subscribe to:
Posts (Atom)