Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Some basic C++ Methods

Elite Diviner
Joined
Mar 30, 2013
Messages
456
Reaction score
42
I will be explaning: DoWhile, While, If, Ternary Operators, and SwitchCase


DoWhile:
The best way to explain this, is:

do { DO ALL OF THIS UNTIL IT IS NO LONGER HAPPENING} while(THIS IS HAPPENING)

Example:
int count = 1;
do{count++}while(count <= 9)

While:
while(THIS IS HAPPENING) { DO ALL OF THIS UNTIL IT IS NO LONGER HAPPENING }

Example;
int count = 1;

while(true)(infiniteloop) {
count ++;
}

If:
if ( IF THIS IS TRUE ) { THEN RUN THIS ONCE }

Example:

int count = 1;
if(true) {count ++;}

Ternary Operators:
WHILE THIS IS TRUE ? DO THIS ONCE :(else) DO THIS

Example:
int count = 1;
count == 1(if true) ? count++(do this) : count--;(else do this)

Switch Case:
switch (what you want to be checking for)
case (One option of what you want to be true for the above info): break;
case (Second option of what you want to be true for the above info): break;
case (Third option of what you want to be true for the above info): break;

Examples:

Switch Case:

int count = 1;
switch (count)
case 1(is true, will run once): break; (used to stop the switch)
case 2(not true, will never be true): break; (used to stop the switch)


I WILL be adding more to this, such as some functions, classes, inheritance, and much more.
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
Humoring the thought anyone actually needs these, your post is quite lacking. First serious flaw is your examples aren't complete: you don't say what the outcome is in each of your examples. Secondly, to make it more than a hurried index of c++ control flow structures you could give examples of problems you'd solve using these and explain why you chose what you did in each case as many of them are interchangeable.

Btw, it's not a good idea to mix explanation and code unless you can write it so the code still compiles correctly e.g. by using comments. The example for the ternary operator is just incomprehensible to someone who doesn't know how it works - and that's your target audience, right. Especially "(if true)" really threw even me off because it contains only valid C++ symbols.. well actually so do all other notes (do this) (else do this)

Sorry for being all criticism this time. I really appreciate what you're trying to do here though and I hope you'll get even better at it :)
 
Last edited:
Initiate Mage
Joined
Jan 8, 2014
Messages
2
Reaction score
0
Humoring the thought anyone actually needs these

This says it all. Your post contained basic material. You should post custom classes or uncommon classes you personally use and find useful.
 
Newbie Spellweaver
Joined
Jun 14, 2014
Messages
16
Reaction score
1
I will be explaning: DoWhile, While, If, Ternary Operators, and SwitchCase


DoWhile:
The best way to explain this, is:

do { DO ALL OF THIS UNTIL IT IS NO LONGER HAPPENING} while(THIS IS HAPPENING)

Example:
int count = 1;
do{count++}while(count <= 9)

While:
while(THIS IS HAPPENING) { DO ALL OF THIS UNTIL IT IS NO LONGER HAPPENING }

Example;
int count = 1;

while(true)(infiniteloop) {
count ++;
}

If:
if ( IF THIS IS TRUE ) { THEN RUN THIS ONCE }

Example:

int count = 1;
if(true) {count ++;}

Ternary Operators:
WHILE THIS IS TRUE ? DO THIS ONCE :(else) DO THIS

Example:
int count = 1;
count == 1(if true) ? count++(do this) : count--;(else do this)

Switch Case:
switch (what you want to be checking for)
case (One option of what you want to be true for the above info): break;
case (Second option of what you want to be true for the above info): break;
case (Third option of what you want to be true for the above info): break;

Examples:

Switch Case:

int count = 1;
switch (count)
case 1(is true, will run once): break; (used to stop the switch)
case 2(not true, will never be true): break; (used to stop the switch)


I WILL be adding more to this, such as some functions, classes, inheritance, and much more.

What does the "++" mean in "count++"? That it adds itself twice or?
 
Newbie Spellweaver
Joined
Jun 14, 2014
Messages
16
Reaction score
1
Oh, so I can use count++ in Java too? :eek:
Didn't know that, thanks alot!
 
Junior Spellweaver
Joined
May 21, 2011
Messages
154
Reaction score
47
I don't like this at all.
First of all these 'methods' are the basics of nearly evert programming language.
Second this example:
Code:
while(true)(infiniteloop) {
count ++;
}
You should explain the 'while(true)' with comments or just after the statement itself. Because beginners get easily confused about it.

Last but not least: brackets!
Even this is confusing for beginners, take a look at 'Switch Case'.

After all it's OK, but I don't think there was a thread needed for this subject.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I tried executing your code and it doesn't work.. Can you please fix your thread so that the examples work, I've tried everything, and they won't compile for any of my compilers.

I'm getting the error:
Code:
[*@localhost scratch]$ g++ scratch.cpp -o scratch.o
scratch.cpp:1:1: error: expected unqualified-id before ‘do’
scratch.cpp:1:54: error: expected unqualified-id before ‘while’

No, but really, try to make your code actually execute.. Saying "this does that" and "that does this" doesn't tell me anything, try to explain things better.

Here's an example of code with comments. Copy-and-pasting this code into a file will actually be a program that can be compiled and run.
PHP:
// is a comment in C++. (That is, `//`)
// The `//` tells the compiler to skip contents 
// of the file for the rest of the line.
// `//` can be after whitespace or even after
// code which gets executed. Comments, `//`, 
// are very usefu for documenting code.


// Include the iostream library so we can use 
// cout to send data to the terminal.
#include <iostream>


// The main function will get executed when
// the program is run. Returning 0 means 
// we are reporting success, and returning
// anything else could be looked at as an 
// error- especially 1. The int returned should
// be between 0 and 255.
int main () {
    // We're declaring an int named 'count'
    // with a value of 1.
    int count = 1;


    // We're declaring an int named 'max'
    // with a value of 10.
    int max = 10;


    // This is a loop. A `while` loop will 
    // execute the code in the curly brackets `{}`
    // over and over again until the boolean value 
    // in the parens `()` is false, or until a 
    // statement such as `break` is used to stop 
    // the loop. This loop has the `true` value,
    // so will execute the code in the curly braces
    // forever!! OR until a statement makes the loop
    // stop.
    while (true) {
        // `if` Statements execute the code in the curly 
        // brackets `{}` one time if the boolean 
        // value in the parens `()` happens to be a 
        // true answer. This one reads:
        // If count is greater than or equal to max, 
        // execute the code in the curly brackets {}.
        if (count >= max) {
            // The 'break' statement is one of the only 
            // ways to exit a loop that starts with: 
            // `while (true)`
            break; 
        }


        // Add 1 to the count variable.
        count += 1;
    }


    // Tell the standard output what the count is.
    // (std::cout sends data to my terminal 
    // where I execute the program ;)
    // The output should  be a line with:
    // `Count is: 10`
    std::cout << "Count is: " << count << "\n";

    // We made it this far, so return success!
    // ... in a way unix and other
    // ... things can understand.
    return 0;
}

I'm not entirely sure about my note on comments or many of the other things, as I don't normally code C++.
 
Last edited:
Joined
Aug 10, 2011
Messages
7,401
Reaction score
3,299
I tried executing your code and it doesn't work.. Can you please fix your thread so that the examples work, I've tried everything, and they won't compile for any of my compilers.

I'm getting the error:
Code:
[*@localhost scratch]$ g++ scratch.cpp -o scratch.o
scratch.cpp:1:1: error: expected unqualified-id before ‘do’
scratch.cpp:1:54: error: expected unqualified-id before ‘while’

No, but really, try to make your code actually execute.. Saying "this does that" and "that does this" doesn't tell me anything, try to explain things better.

Here's an example of code with comments. Copy-and-pasting this code into a file will actually be a program that can be compiled and run.


I'm not entirely sure about my note on comments or many of the other things, as I don't normally code C++.

Does this compile for you?

Code:
#include <iostream>

int main(int argc, char** argv)
{
        int count = 1;
        int max = 10;

        while(count < max)
                ++count;

	std::cout << count << std::endl;
	return 0;
}
 
Joined
Jul 8, 2009
Messages
445
Reaction score
63
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int count = 0;
int max = 10;

while(count < max)
{
++count;

cout <<"Count Position : "<<count <<endl;
}
return 0;
}



I will make a code here to validate if the triangle is rectangle or obsolete

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int a , b ,c;
cout<<" Enter 3 values to see if you can form a triangle: \ n";
cin>>a>>b>>c;
if (a^2 == b^2 + c^2)
{
cout<<" This Form a Triangle Rectangle"<<endl;
}
else
if (a^2 > b^2 + c^2)
{
cout<<" This Form a Triangle Obsolete"<<endl;
}
return 666;
}
 
Last edited:
Back
Top