#include <iostream> using namespace std; // int main() // { // // int myNum = 5; // Integer (whole number) // // float myFloatNum = 5.99; // Floating point number // // double myDoubleNum = 9.98; // Floating point number // // char myLetter = 'D'; // Character // // bool myBoolean = true; // Boolean // // string myText = "Hello"; // String // } // boolean 1 byte //char 1 bytes //int 2 or 4 bytes //float 4 bytes //double 8 bytes //Double vs float //Double can have upto 15 decimal points where as float only 6 or 7; // int main(){ // double mynum=4.75; // float myNum=4111.5555; // cout<<mynum<<endl; // cout<<myNum; // } //operators //Arthemetic // + Addition Adds together two values x + y // - Subtraction Subtracts one value from another x - y // * Multiplication Multiplies two values x * y // / Division Divides one value by another x / y // % Modulus Returns the division remainder x % y // ++ Increment Increases the value of a variable by 1 ++x // -- Decrement Decreases the value of a variable by 1 --x //Assignment // = x = 5 x = 5 // += x += 3 x = x + 3 // -= x -= 3 x = x - 3 // *= x *= 3 x = x * 3 // /= x /= 3 x = x / 3 // %= x %= 3 x = x % 3 // &= x &= 3 x = x & 3 // |= x |= 3 x = x | 3 // ^= x ^= 3 x = x ^ 3 // >>= x >>= 3 x = x >> 3 // <<= x <<= 3 x = x << 3 //Comparison operators // 0 means false 1 means true // == Equal to x == y // != Not equal x != y // > Greater than x > y // < Less than x < y // >= Greater than or equal to x >= y // <= Less than or equal to x <= y // int main(){ // int x=4,y=10; // cout<<(x>y);returns 0 means false // } //Logical operators //Used to find the logic between variables and values. // && Logical and,Returns true if both statements are true x < 5 && x < 10 // || Logical or, Returns true if one of the statements is true x < 5 || x < 4 // ! Logical not, Reverse the result, returns false if the result is true !(x < 5 && x < 10) //Strings // int main(){ // string name="Ravindra"; // cout<<name; // } //String Concatenation // int main(){ // string name="John "; // string fullname="Cena"; // cout<<name+fullname; // } //String Concatenation by Append() // int main(){ // string name="John "; // string fullname="Smith"; // cout<<name.append(fullname); // } //If number are used as strings then they will be concatenated // int main(){ // string x = "10"; // string y = "20"; // string z = x + y; // cout<<z;//retursn 1020 Concatenation occurs // } //String and an int can be added // int main(){ // string x = "10"; // int y = 20; // string z = x + y; // cout<<z;// Error // } //String length //Both length and size are same // int main(){ // string goal="This is my goal"; // // cout<<goal.length(); // cout<<goal.size(); // } //String Access //You can access string elements same as std::array<T, N> ; // int main(){ // string greet="Hello"; // cout<<greet[0]; // } // Change String Characters // int main(){ // string greet="Hello"; // greet[0]='g'; // cout<<greet; // } //User Input strings int main(){ string name; cout<<"Enter Your name\n"; cin>>name; cout<<"Your name is: "<<name; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}