Skip to content

Instantly share code, notes, and snippets.

@AlexGabor
Last active September 17, 2019 16:10
Show Gist options
  • Save AlexGabor/db28c05c46857659a234222739f6905f to your computer and use it in GitHub Desktop.
Save AlexGabor/db28c05c46857659a234222739f6905f to your computer and use it in GitHub Desktop.
C++ basic statements
#include <iostream>
using namespace std;
int main () {
// write your stuff here
int x; // declare variable for a an "integer" value
float x; // declare variable for a "real" value
double x; // declare variable for a more precise "real" value
bool x; // declare variable for a value of `true` or `false`
char x; // declare variable for a character value. See ASCII table: http://www.asciitable.com/
cin >> x; // read value from user
cout << "message"; // print a message
cout << x; // print the value from x
cout << endl; // print a new line
x = 5; // assign a value to x
x = 'a'; // assign a character value to x
x = true; // assign true to a variable
x = false; // assign false to a variable
x = y; // assign the value from y to x
x = y + z; // add two numbers and put the resulting number in x
x = y - z; // subtract two numbers and put the resulting number in x
x = y * z; // multiply two numbers and put the resulting number in x
x = y / z; // division two numbers and put the resulting number in x
x = y % z; // get the reminder of the division of y to z of two numbers and put the resulting number in x
x = a * (b + c) // note that you can group with parantheses the expressions above (expression is what is on the right side)
// if else branch
if (condition) {
// if condition is true
} else {
// if condition is false
}
// if without else branch
if (condition) {
// if condition is true
}
// condition can be replaced with:
x == y // checks if the value of x is equal to the value of y
x != y // checks if the value of x is different to the value of y
x > y // checks if the value of x is greater than the value of y
x >= y // checks if the value of x is greater or equal to the value of y
x < y // checks if the value of x is less than the value of y
x <= y // checks if the value of x is less or equal than the value of y
// conditions can be combined:
condition1 && condition2 // evaluates to true if both are true
condition1 || condition2 // evaluates to true if any of them are true
// && has priority over || just like * over +
// just like the usual math operators you cand use parantheses
// repetitive structures
while (condition) {
// statements here are executed while condition is true. Only when the last statement here is executed the condition is checked again
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment