Posts

Showing posts from 2020

Declaring c++ variables

C++ Variables Variables are containers for storing data values. In C++, there are different  types  of variables (defined with different keywords), for example: int  - stores integers (whole numbers), without decimals, such as 123 or -123 double  - stores floating point numbers, with decimals, such as 19.99 or -19.99 char  - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes string  - stores text, such as "Hello World". String values are surrounded by double quotes bool  - stores values with two states: true or false Declaring (Creating) Variables To create a variable, you must specify the type and assign it a value: Syntax type   variable  =  value ; Where  type  is one of C++ types (such as  int ), and  variable  is the name of the variable (such as  x  or  myName ). The  equal sign  is used to assign values to the variable. To create a variable that should store a number, look at the following example: Example Create a

C++ New lines

New Lines To insert a new line, you can use the  \n character: Example #include <iostream> using namespace std; int  main() {    cout <<  "Hello World!  \n " ;   cout <<  "I am learning C++" ;    return   0 ; } Tip:  Two  \n  characters after each other will create a blank line: Example #include <iostream> using namespace std; int  main() {    cout <<  "Hello World!  \n\n " ;   cout <<  "I am learning C++" ;    return   0 ; } Another way to insert a new line, is with the  endl  manipulator: Example #include <iostream> using namespace std; int  main() {    cout <<  "Hello World!"  <<  endl ;   cout <<  "I am learning C++" ;    return   0 ; } Both  \n  and  endl  are used to break lines. However,  \n  is used more often and is the preferred way.

C++ output (print text)

C++ Output (Print Text) The  cout  object, together with the  << operator, is used to output values/print text: Example #include <iostream> using namespace std; int  main() {    cout  <<  "Hello World!" ;    return   0 ; } Run example » You can add as many  cout  objects as you want. However, note that it does not insert a new line at the end of the output: Example #include <iostream> using namespace std; int  main() {    cout  <<  "Hello World!" ;    cout  <<  "I am learning C++" ;    return   0 ; }

C++ syntax

C++  Syntax ❮ Previous Next ❯ C++ Syntax Let's break up the following code to understand it better: Example #include <iostream> using namespace std; int  main() {    cout <<  "Hello World!" ;    return   0 ; } Run example » Example explained Line 1:   #include <iostream>  is a header file library  that lets us work with input and output objects, such as  cout  (used in line 5). Header files add functionality to C++ programs. Line 2:   using namespace std  means that we can use names for objects and variables from the standard library. Don't worry if you don't understand how  #include <iostream>  and  using namespace std  works. Just think of it as something that (almost) always appears in your program. Line 3:  A blank line. C++ ignores white space. Line 4:  Another thing that always appear in a C++ program, is  int main() . This is called a  function . Any code inside its curly brackets  {}  will be executed. Line 5:   cout  (pronounced &q

C++ QUICK START

Image
Let's create our first C++ file. Open Codeblocks and go to  File > New > Empty File . Write the following C++ code and save the file as  myfirstprogram.cpp  ( File > Save File as ): myfirstprogram.cpp #include <iostream> using namespace std; int  main() {    cout <<  "Hello World!" ;    return   0 ; } Don't worry if you don't understand the code above - we will discuss it in detail in later chapters. For now, focus on how to run the code. In Codeblocks, it should look like this: Then, go to  Build > Build and Run  to run (execute) the program. The result will look something to this: Hello World! Process returned 0 (0x0) execution time : 0.011 s Press any key to continue.
Setting up C++ Development Environment C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully. If you do not want to set up a local environment you can also use online IDEs for compiling your program. Using online IDE : IDE stands for integrated development environment. IDE is a software application that provides facilities to a computer programmer for developing software. There are many online IDEs available which you can use to compile and run your programs easily without setting up a local development environment. include<iostream> using namespace std; main() {      cout << "Learning c++ with ankit" ; }