Elite Diviner
- Joined
- Mar 30, 2013
- Messages
- 456
- Reaction score
- 42
Humoring the thought anyone actually needs these
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 ONCEelse) 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.
Thank you!"count++;" would add 1 to the value countsame as "count--;" would subtract one..
count += 1 and count++ both work in all C++, Java and JavaScriptThank you!
On Java and Javascript, we use "Count =+ 1"
while(true)(infiniteloop) {
count ++;
}
[*@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’
// 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 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++.
#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;
}