Newbie here.

Posts 1–5 of 5 · Page 1 of 1
Newbie here. How do i program?
Hey all im new here
But not a complete programming noob
Well sort of
What method do you do whilst learning a programming langauge
Im trying to learn C++ but want to no a method of learning
for example do i read for example Cout << "Hello World";
and write it in notepad over and over ?
Or do i read it move onto something else?
I don't understand how you do it i have books but im still stuck here on the learning
process any tips on learning and practising the langauge?
im determined to do this.

Also i no im pure noob but what are the things called in programming like for example

void, print, while, if, etc

Are they called commands ??
Well you don't write things in notepad over and over, you get an idea and make up things to do or use the exercises from the book. If you're reading from a book take notes, re-read, make sure you understand everything it just talked about. If you're reading about cout << "Hello World!"; you should know what #include is, you should know what <iostream> is, you should know what using namespace std (or std:, you should know what cout is, you should know what << is, you should know what "Hello World!" is, you should know what ; is. (cout is an operand for the ostream, >> is an operator for the ostream, "Hello World!" is a string litera, ; is used to let the IDE know that this is the end of the line of code).

You're in the beginning parts of the language, you're going to learn the basics. Practice them, understand them, think of ways to use them. (Once you learn cin you can do a lot more, you could use a for loop to compound interest made on an investment using the forumula I=PRT. You could take a string and convert every character to upper or lower case, you could use a for loop or a range for for this)

As for the second part, if you don't know what these are you're a beginner, you're at the very very very beginning and haven't learned anything past int main() { cout << "Hello World!" << endl; return 0; } (You probably didn't even know you could execute and compile this code and it would work just fine)
void is a type associated with functions (int main(), int is a type). Every function is expected to return a value except for void. Main is the only other function where you do not have to give a return.
Code:
void PrintString(){
cout >> "Hello World!";
Any other function that had a type you would have to do a return.
Code:
string PrintString(){
cout << "Hello World!";
return "Hello World!";
Without the return at the end of the string function this code would fail to compile.

Print if a function from the C language (actually it's "printf"). It takes a char * (aka a C-style string. A c-style string is really an array of chars)
Code:
	string cppString = "Hello World";
	char cStyleString[] = "Hello World Too!";
	printf(cStyleString);
	printf(cppString);
the cppString won't work because that is a C++ String, which is a string literal, and the function (printf) takes a const char*

And last while, if, do while, and for are all looping statements.
string cppString = "Hello World";
Code:
if (cppString.size() > 0){	// while the size of cppString is > 0
		cout << cppString;
		cppString = "";	//	makes cppString empty, ultimately making the size 0
	}
Code:
while (cppString.size() > 0){	// while the size of cppString is > 0
		cout << cppString;	//	makes cppString empty, ultimately making the size 0
	}
Code:
do{	//	do
	cout << cppString;	//	this
	cppString = "";	//	makes cppString empty, ultimately making the size 0
	}
Code:
for (int i = 0; i != cppString.size(); ++i)	//	a for loop
		cout << cppString[i];
The code after each loop statement only executes if the value it's operating one is true, in the case the size of cppString (cppString.size()). Since it's initially "Hello World!" (Which is 12 characters long) and is > 0 then the code executes. The only different ones would be do .. while and for.
Do while executes any code in it's scope BEFORE checking the statement, so you're guaranteed to print cppString no matter what it's checking for.
My for loop turns cppString into a C-Style string (It can be treated the same way as an array, depending on how you use it). Using the variable "i" to cout the character at the position of the array. (h is 0 in the index, e is 1, l is 2, l is 3, o is 4 etc).
Just a further explanation on the looping statements (cause the post was getting big).
If I hadn't made cppString to an empty string every loop, except for, would have continued to print "Hello World!" indefinitely until it hit end-of-file.
Code:
	string cppString = "Hello World!";
	int size = cppString.size();
	while (size){
		cout << cppString << endl;
		size -= 1;	//	or --size or size-- or size = size - 1
	}
Would print Hello World! 12 times, until size = 0 which makes the statement while (size) fail, because any number other than 0 is true, 0 is false.

And the for loop executes off the variable i (which is initialized to 0) not the cppString.size() (It checks it against that). for loop works like this
Code:
//Taken from my notes on the book I'm reading
for (int val = 1; val <= 10; ++val)
Each for statement has two parts: a header and a body.
	The header controls how often the body is executed.
	an int statement (val = 1)
	a condition (val <= 10)
	and an expression (++val)
The variable val exists only inside the for; it is not possible to use val after this loop terminates
The init-statement is executed only once, on entry to the for
The condition (val <= 10) is tested each time through the loop. As long as val is less than or equal to 10, we excute the for body.
The expression is executed after the for body (it skips the first ++val and goes to the body first then back to the ++val and loops)
After executing the expression, the for retests the condition.
Wow thanks alot. but in general are the codes for a langauge called functions?
i know im a complete newbie but getting there.
The "codes"? Idk what you mean....

I guess a function is a block of code... lemme give an example.

Code:
#include <iostream>
#include <string>//library used to be able to print a parameter variable in the operator cout
using namespace std;

string myStrFunction(string myStr){//string is type, myStrFunctionis the name of our function
cout << myStr << endl;
return myStr;//the return, control exits this function
}

int main(){
cout << "Hello World!" << endl;
myStrFunction("Hello World!");//calls upon the function "myStrFunction" and passes the argument "Hello World!"
return 0;
}
"main" and "myStrFunction" are both functions. The code inside these are often referred to as "scopes". A function has to have a type (main is type int, myStrFunction is type string). You create a function, type up some code in it, and can call upon those function inside main (or other functions).

Main first starts by doing print "Hello World!" to the screen. Right after that it calls upon our function called "myStrFunction" using the argument "Hello World!" (if you don't know what argument/parameter are google arguments and parameters). Since we called upon the function the control passes over to function where it takes a parameter of type string (myStr) where it then also does a cout (if you execute this "Hello World!" will be printed twice). At the end the does a return (because function is type string and every function has to return except for void and main) which means that function is no longer in control and it passes back over to main where it then also does a return (and a return at the end of main means the code is finished).
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us