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
Thursday, 30 May 2013
Week 12 day 3
It is not uncommon to build up a complex class by declaring simpler classes and including them in the declaration of the more complex class. Fir example, you might declare a wheel class, a motor class, and a transmission class and ect... then combine them into a car class. This declares the relationship between the motor, wheels and transmission.
Tuesday, 28 May 2013
Week 6 day 2
This week I am learning the basics of more function calling and global variables. Global Variables can be called from any function but are dangerous in their own way. they mean that the programmer can call them from anywhere inside the program. They can also be changed in any place from the program to an unintended value which interferes with the proper operation of the intended program.
Week 12 day 2
*/ this is the Cat header file/*
#include <iostream>
class Cat
{
public:
cat(int initial age);
~cat();
int GetAge() const {return itsAge;} //inline function
void SetAge (int age) {itsAge=age;} //inline function
void meow() const {stc::cout<<"Meow.\n";} //inline function
private:
int itsAge;
};
*/ this is the cat.cpp file /*
#include "cat.hpp"
#define cout std::cout
Cat::Cat(int initialAge)//constructor
{
itsAge= initialAge;
}
Cat::~Cat() //destructor
{
}
int main()
{
cat frisky(5);
frisky.meow();
cout<<"frisky is a cat who is "<<GetAge()<<" years old!\n";
frisky.meow();
frisky.SetAge(7);
cout<<"frisky is now "<<GetAge()<<" years old!\n";
frisky.meow();
return 0;
}
#include <iostream>
class Cat
{
public:
cat(int initial age);
~cat();
int GetAge() const {return itsAge;} //inline function
void SetAge (int age) {itsAge=age;} //inline function
void meow() const {stc::cout<<"Meow.\n";} //inline function
private:
int itsAge;
};
*/ this is the cat.cpp file /*
#include "cat.hpp"
#define cout std::cout
Cat::Cat(int initialAge)//constructor
{
itsAge= initialAge;
}
Cat::~Cat() //destructor
{
}
int main()
{
cat frisky(5);
frisky.meow();
cout<<"frisky is a cat who is "<<GetAge()<<" years old!\n";
frisky.meow();
frisky.SetAge(7);
cout<<"frisky is now "<<GetAge()<<" years old!\n";
frisky.meow();
return 0;
}
Week 12 day 1
You can also put the definition of a function into the declaration of the class, which automatically makes that function inline for example;
class Cat
{
public:
int weight(){return itsweight;}
void setweight(intaweight);
};
Week 11 day 3
Just as you ask your compiler to make a regular function 'inline' you can make class methods inline. The keyword 'inline' appears before the return type. The inline implementation of the GetWeight() function looks like this;
inline int Cat::GetWeight()
{
return itsWeight;
}
inline int Cat::GetWeight()
{
return itsWeight;
}
Week 11 day 2
If you make GetAge() a 'constant' function -as you should- the contract also promises that GetAge() wont change the Cat on which it is called. Each function that you declare for your class must have a definition. The definition is also called the function implementation. Like other functions, the definition of a class method has a function header and a function body. The definition must be a file that the compiler can find. Must C++ compilers want that file to end in ".C" or ".CPP".
Tuesday, 21 May 2013
week 11 day 1
In the class cat declaration, for example you create a contract that every cats age can be initialized in its constructor assigned to it by its SetAge() accessor function and read by its GetAge() accessor. You also promise that every cat will know how to Meow(). Note that you say nothing in the public interface about the member variable ItsAge, that is an implementation detail that is not part of your contract.
Friday, 10 May 2013
Week 10 day 2
This day was a mixture of Trial and error getting a C++ program to call a webpage from your default browser. It took several tries just to get the program to do this simple task. Everywhere I looked the programmers kept putting up this very long code. Yet all it took was:
#include <windows.h>
int main()
{
ShellExecute(NULL, "open", "http://google.net",
NULL, NULL, SW_SHOWNORMAL);
}
That simple program calls the webpage Google.ca.
#include <windows.h>
int main()
{
ShellExecute(NULL, "open", "http://google.net",
NULL, NULL, SW_SHOWNORMAL);
}
That simple program calls the webpage Google.ca.
Tuesday, 7 May 2013
Week eight day 3
This was an interesting learning curve for C++,
#include <iostream>
class Cat
{
public:
int GetAge();
void SetAge(int age);
void Meow();
private:
int itsAge();
};
int Cat::GetAge()
{
return itsAge();
}
void Cat::SetAge(its age)
{ itsAge = age); }
void Cat::Meow();
{ cout << "Meow!"; }
int main()
{
Cat Frisky;
Frisky.SetAge(5);
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge() << " years old\n";
Frisky.Meow();
return 0;
}
#include <iostream>
class Cat
{
public:
int GetAge();
void SetAge(int age);
void Meow();
private:
int itsAge();
};
int Cat::GetAge()
{
return itsAge();
}
void Cat::SetAge(its age)
{ itsAge = age); }
void Cat::Meow();
{ cout << "Meow!"; }
int main()
{
Cat Frisky;
Frisky.SetAge(5);
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge() << " years old\n";
Frisky.Meow();
return 0;
}
Week 10 day 1
Today I wrote a program what was just above simple for me but for most people it would be still very basic. The idea behind the program was to match a string password so that it would "unlock" the program. Once inside you answered basic questions which didnt do anything but try to make the person understand how the program worked. After that you got to the final question of did you like the program and if you liked it. You got the option to leave a "comment" for the programmer which then created a file called "comments.txt".
This file would hold whatever you said.
I ran into several problems when trying to compile this, the main one was it would only save to a network directory from my computer and no other one. When I tried to get someone else to try the program it would fully run and it would get to the end. BUT it would not save the "comments.txt" file on the network at all. I tried everything until I finally got the program to save it to the local folder on the computer on the network instead of saving it to the network its self.
Sunday, 5 May 2013
week 9 day 1
I missed a week due to not being able to work as I dislocated my lower leg and knee cap and was in to much pain to do anything. I have however started to work with an online Harvard free courseware program.
This program teaches you the basics of computer programming off the CS-50 course.
This program teaches you the basics of computer programming off the CS-50 course.
Friday, 12 April 2013
Week eight day 2
Both the increment operator (++) and the decremented operator (--) come in
two varieties prefix and post fix. The prefix variety is written before the
variable name (++myAge); the post-fix variety is written after the variable name (myAge++);.
in a simple statemet is doesnt matter which you use but in a complex statement
when you are incremental or decrements a variable and then assigning the result to
another variable it doesn't matter
two varieties prefix and post fix. The prefix variety is written before the
variable name (++myAge); the post-fix variety is written after the variable name (myAge++);.
in a simple statemet is doesnt matter which you use but in a complex statement
when you are incremental or decrements a variable and then assigning the result to
another variable it doesn't matter
Week eight day one
Learned different cout functions for displaying text
/a Bell(alert)
/b form new line
/f new line
/n carriage return
/r tab
/t vertical tab
/' single quote
/" double quote
/? question mark
// backslash
/000 octal notation
/a Bell(alert)
/b form new line
/f new line
/n carriage return
/r tab
/t vertical tab
/' single quote
/" double quote
/? question mark
// backslash
/000 octal notation
Thursday, 4 April 2013
week seven day 2
I have moved on to constant functions and classes. Using the keyword 'const' you can make anything in C++ unchangeable which solves a lot of problems for example;
int x = 5 const;
this makes the int X unchangable.
int x = 5 const;
this makes the int X unchangable.
Tuesday, 2 April 2013
Week seven day 1
So far this week I have been working on classes and how they can be used to further shorten the coding process. Using classes is very useful if you are making many things that will need to use the same functions, using a class makes it so you can use any amount of function names as you wish keeping them under the same roof instead of making all new functions when you make a new name.
for example:
class Cat
{
public:
int itsAge;
int itsWeight;
};
Cat Frisky;
Frisky.itsAge = 10;
Frisky.itsWeight = 100;
Cat Billie;
Billie.itsAge = 2;
Billie.itsWeight = 50;
This is used as you can see to help make it shorter to have all this code instead of writing out all the ints for each 'Cat'.
Sunday, 10 March 2013
Week 6 day 1
Today I learned about global variables. There are variables that can be called from any function in the program. For example;
#include <iostream>
using namespace std;
int myInt = 6; //This is your global variable
int main()
{
cout << myInt;
return 0;
}
that is a basic global variable program. Using global variables are very dangerous though, as they are shared data that any other function can change the data to. Which in turn creates a bug that is very challenging to find.
Friday, 8 March 2013
Week 5 day 4
Today I learned more about the function controlling for C++. It was quite boring though, functions can be used to do almost anything that you can tell or think of. You can make a function for sending a signal app-on an error, or make it do something when something else is done. This is good if you have an error that happens and you want it to return a lone of code when the error occurs in the program. Or if you want it to send you a message when it has completed a task.
Thursday, 7 March 2013
week 5 day 3
Today, I learned more about functions, I learned about how they can be used to add or subtract and multiply or divide numbers you enter into it. using a lone of code like this:
int addFunction (int a, int b)
{
x = a+b;
return (a + b)
}
this would add the numbers together and then assign them to the letter X, to be called by any function. This is the basics of using math to call something from another function just to add them together.
int addFunction (int a, int b)
{
x = a+b;
return (a + b)
}
this would add the numbers together and then assign them to the letter X, to be called by any function. This is the basics of using math to call something from another function just to add them together.
Week 5 day 2
Today I have downloaded and burned two DVD's: one is a Hirens boot CD, which is used for finding and repairing any problems on a computer as well as recovering files; and Geek squad MRI tools, this is used for diagnosing any problems with components of a computer. These two CD's are very useful for any computer technician as they can find almost any problem on a computer. I have also learned about basic math using C++ for example:
int x;
x = 12(22*4) + 66/100;
which assigned the value of those numbers to the int x. this is useful as everything with computers is done with numbers.
for example if you wanted something (for example a servo motor controlled from a computer) if you wanted it to go to a specific position you would have to write a code to make it go to the specific number from the position it is at.
int x;
x = 12(22*4) + 66/100;
which assigned the value of those numbers to the int x. this is useful as everything with computers is done with numbers.
for example if you wanted something (for example a servo motor controlled from a computer) if you wanted it to go to a specific position you would have to write a code to make it go to the specific number from the position it is at.
Wednesday, 6 March 2013
Week 5 day 1
This day, I learned about how a function is called in C++. when defining a function you use a call function eg;
int myFunction
{
cout << "This is a function to display this message";
return 0;
}
That is a basic function, when using C++ you always use the "main" function which is used to call any other functions for example look at the following script;
#include <iostream>
using namespace std;
int myFunction ()
{
cout << "called from myFunction!\n";
return 0;
}
int main ()
{
cout << "Called from main()!";
myFunction();
cout << "Back to main!";
return 0;
}
this prints:
Called from main()!
called from myFunction!
Back to main!
this is the basics of using more than one function with C++. It is also easier for the system to handle if you break up the work into many functions instead of making one do all the work.
int myFunction
{
cout << "This is a function to display this message";
return 0;
}
That is a basic function, when using C++ you always use the "main" function which is used to call any other functions for example look at the following script;
#include <iostream>
using namespace std;
int myFunction ()
{
cout << "called from myFunction!\n";
return 0;
}
int main ()
{
cout << "Called from main()!";
myFunction();
cout << "Back to main!";
return 0;
}
this prints:
Called from main()!
called from myFunction!
Back to main!
this is the basics of using more than one function with C++. It is also easier for the system to handle if you break up the work into many functions instead of making one do all the work.
Wednesday, 27 February 2013
Week 4
This week I have learned about various functions, and more of the math. I have been unable to do to much so far as I broke one of my finders and have been in severe pain. I have however worked on different variables in C++. I have been working with C++ with different function types and learning mistakes people make with functions when declaring them. In C++ functions have a important role, they contain everything that is needed to run the program and to store the data.
Wednesday, 20 February 2013
Week 2/3
Last week as it was only a 3 day week, and I have this course every other day I ended up only having the class once. I have however gotten into the more complex mathematical and if else functions this week.
Wednesday, 6 February 2013
Week one.
This week I have started putting more effort into learning C++. I have found a book that will take me 21 days to complete and this should allow me to learn the programming language. So far in this book I have gone over; The basics, Cin and Cout, Data storage over variables via memory addresses for example "int x;" or "char myChar;", and creating an alias for longer sentences so I do not have to type this out for example: eg "unsigned short int" can be shortened so all you need to type is "USHORT" saving time and space in the document.;
I have learned about constant data values, where the value cannot change at all, I learned how to incorporate different functions into a new function. I learned basic C++ math as well, there was also keywords that I learned about that do different functions in C++.I learned about #define function, which has the same use as 'typedef'. I also learned about how to make an 'enum' which assigns what is in it a data value just as if it was in an array.
I have learned about constant data values, where the value cannot change at all, I learned how to incorporate different functions into a new function. I learned basic C++ math as well, there was also keywords that I learned about that do different functions in C++.I learned about #define function, which has the same use as 'typedef'. I also learned about how to make an 'enum' which assigns what is in it a data value just as if it was in an array.
Subscribe to:
Posts (Atom)