C++ Tutorial: Introduction for Beginners (Basic)

c++ tutorial

C++ is a General Purpose, cross-platformed Programming language. It’s a World one of the most popular programming languages. It designed by Bjarne Stroustrup in 1979, as an extension to the C language. It is close to Java and C#. In this tutorial, you will learn the basic syntax of C++. To start using C++ you need a text editor such as notepad++, VSCode, and A compiler like GCC. We will use Code::Blocks for this tutorial.

Website: isocpp.org

Read: How to Be a Programmer

How to be a Programmer | Part 1: Introduction
How to be a Programmer | Part 2: Top 20 Programming Languages
How to be a Programmer | Part 3: Basic Concept of Programming
How to be a Programmer | Part 4: Control Structures
How to be a Programmer | Part 5: Number Systems
How to be a Programmer | Part 6: Coding Systems in Computer
How to be a Programmer | Part7: Logic Gates and Boolean Algebra

C++ “Hello World”

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

C++ Tutorial – Installation

Download the latest “mingw-setup.exe” file and install it. (mingw-setup.exe file includes additionally the GCC/G++ compiler and GDB debugger from TDM-GCC)

C++ Tutorial – Start

c++ code blocks

Now open Code::Blocks and create a new C++ file (File -> New -> Empty File)

CPP Tutorial

then enter the following code and save it. (helloworld.cpp)

#include <iostream> //header file library
using namespace std;

int main() {
  cout << "Hello World!"; //print text
  return 0;
}
CPP Tutorial

Then, go to (Build > Build and Run) to execute the program

c++ tutorial first output
Output

The cout and << operator is used to output values:

C++ Tutorial – Comments

#include <iostream>
using namespace std;

int main() {

  // This is a single-line comment
  cout << "Hello World!";

  /* This is a multi-line 
  comment */

  return 0;
}

C++ Tutorial – Variables

Strings– Stores text (“Hello World”, “Text”)
char – Stores single character (‘a’, ‘c’)

int – Stores Integers (1234, -242)
float– Stores floating-point numbers (11.25, -54.21) ;storing 7 decimal digits
double– Stores floating-point numbers (11.25, -54.21) ;storing 15 decimal digits
bool– Stores boolean values (true or false)

type variable = value;

enter the following code and save it (variables.cpp).

#include <iostream>
using namespace std;

int main() {
  int num = 25001;
  cout << num;
  return 0;
}

Then, go to (Build > Build and Run) to execute the program

CPP Tutorial variables

Unchangeable Variables

#include <iostream>
using namespace std;

int main() {
  const int year = 2019; // constant keyword 
  cout << year;
  return 0;
}

C++ Tutorial – Array

type name_of_array[]; //declared a array

string name_of_arra[] = {"Values", "Values", "Values", "Values"}; //declared a array and insert the values

First, enter the following code and save it (array.cpp).

#include <iostream>
using namespace std;

int main() {
  int numbers[6] = {100, 150, 524, 2487, 98547, 999974};
  cout << numbers[3];
  return 0;
}

Array indexes start with 0 – numbers[0] = 100

Then, go to (Build > Build and Run) to execute the program

Change an Array Element

First, enter the following code and save it (array1.cpp).

#include <iostream>
using namespace std;

int main() {
  int numbers[6] = {100, 150, 524, 2487, 98547, 999974};
  cout << numbers[3];

  numbers[3] = 1547896
  cout << numbers[3];
  return 0;
}

Operators

Arithmetic operators

Comparison operators

Logical operators

Conditions

if statement

if (condition) {
  // code to be executed. (if the condition is true)
}

First, enter the following code and save it (if.cpp).

#include <iostream>
using namespace std;

int main() {
  if (100 == 100) {
    cout << "Code executed";
  }
  return 0;
}

Then, go to (Build > Build and Run) to execute the program.

CPP Tutorial if condition

if-else statement

if (condition) {
  // code to be executed. (if the condition is true)
} else {
  // code to be executed. (if the condition is false)
}

First, enter the following code and save it (if-else.cpp).

#include <iostream>
using namespace std;

int main() {
  int a = 100;
  int b = 245;

  if (a > b) {
    cout << "A > B";
  } else {
    cout << "B > A";
  }
  return 0;
}

//colla recipiente - McBSUhxH

Then, go to (Build > Build and Run) to execute the program.

if elseif c++

if and else-if- statement

if (condition) {
  // code to be executed. (if the condition is true)
} else if (condition2) {
  // code to be executed. (if the first condition is false and second condition is true)
} else {
  // code to be executed. (if the all conditions are false)
}

First, enter the following code and save it (if-elseif.cpp).

#include <iostream>
using namespace std;

int main() {
  int Result = 88;
  if (Result > 75) {
    cout << "Result: A";
  } else if (Result > 65) {
    cout << "Result: B";
  } else if (Result > 55) {
    cout << "Result: C";
  } else if (Result > 45) {
    cout << "Result: S";
  } else {
    cout << "Result: F";
  }
  return 0;
}

Then, go to (Build > Build and Run) to execute the program.

if else condition c++

Short Hand If

variable = (condition) ? True : False;
#include <iostream>
using namespace std;

int main() {
  int a = 100;
  int b = 245;
  string result = (a > b) ? "A > B" : "B > A";
  cout << result;
  return 0;
}

Switch Statements

switch(expression) {
  case x:
    // code to be executed.
    break;
  case y:
    // code to be executed.
    break;
  case z:
    // code to be executed.
    break;
  default:
    // code to be executed.
}

First, enter the following code and save it (switch.cpp).

#include <iostream>
using namespace std;

int main() {
  int month = 8;
  switch (month) {
  case 1:
    cout << "January";
    break;
  case 2:
    cout << "February";
    break;
  case 3:
    cout << "March";
    break;
  case 4:
    cout << "April";
    break;
  case 5:
    cout << "May";
    break;
  case 6:
    cout << "June";
   break;
  case 7:
    cout << "July";
    break;
  case 8:
    cout << "August";
    break;
  case 9:
    cout << "September";
    break;
  case 10:
    cout << "October";
    break;
  case 11:
    cout << "November";
    break;
  case 12:
    cout << "December";
    break;
  default:
    cout << "Invalid Month";
  }
  return 0;
}

Then, go to (Build > Build and Run) to execute the program.

switch condition cpp

Loops

While Loop

while (condition) {
  // code to be executed.
}

First, enter the following code and save it (while.cpp).

#include <iostream>
using namespace std;

int main() {
  int i = 0;
  while (i < 10) {
    cout << i << "\n";
    i++;
  }
  return 0;
}

Then, go to (Build > Build and Run) to execute the program.

while loop cpp

while loop will execute the code block until the condition is false

Do/While Loop

do {
  // code to be executed.
}
while (condition);

First, enter the following code and save it (dowhile.cpp).

#include <iostream>
using namespace std;

int main() {
  int i = 0;
  do {
    cout << "Hello World" << "\n";
    i++;
  }
  while (i < 10);
  return 0;
}

Then, go to (Build > Build and Run) to execute the program.

do/while loop cpp

do/while loop will execute the code block once, before checking if the condition is true.

Sources : 
https://pixabay.com/ (images) 

Hello World,

I hope this post (C++ Tutorial: Introduction for beginners) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in programming and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page: https://www.facebook.com/ctechf

Best Free eBooks to Learn Python Programming

Free eBooks to Learn Python

Free eBooks to Learn Python, Here are 13 of the best Python eBooks for python lovers.
Download Now

What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
Python Tutorial: How to Connect MySQL Database in Python
Python Tutorial: Python GUI Programming (Tkinter)
Python Tutorial: Send Emails Using Python
Python Tutorial: Django Tutorial for Beginners

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

45 Shares
Share
Tweet
Share
Pin
Share