Saturday, April 19, 2025

C++ Do While Loop

Do..While loop is interesting one. It's used when we need at least one operation executed.

It's easy to understand. Part of loop "do" will be first thing to type, than curly bracket with operations and jump, then closing curly bracket.

While part of loop will be OUTSIDE curly brackets.

Of course, starting point must be set first:

int counter = 1;

And do..while loop with simple operation of printing counter. After useful operation we have jump (counter = counter + 1). While part is outside:

do {
        cout << "Counter at the Moment: " << counter << endl;
        counter = counter + 1;
    } while (counter <= 20);

This is full working source:

#include <iostream>

using namespace std;

int main() {

    int counter = 1;

    do {
        cout << "Counter at the Moment: " << counter << endl;
        counter = counter + 1;
    } while (counter <= 20);

    return 0;

}

Result:

Counter at the Moment: 1
Counter at the Moment: 2
Counter at the Moment: 3
Counter at the Moment: 4
Counter at the Moment: 5
Counter at the Moment: 6
Counter at the Moment: 7
Counter at the Moment: 8
Counter at the Moment: 9
Counter at the Moment: 10
Counter at the Moment: 11
Counter at the Moment: 12
Counter at the Moment: 13
Counter at the Moment: 14
Counter at the Moment: 15
Counter at the Moment: 16
Counter at the Moment: 17
Counter at the Moment: 18
Counter at the Moment: 19
Counter at the Moment: 20

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.

Please note, if we use something big for counter, for example 12345, one operation will still work:

Counter at the Moment: 12345

Process returned 0 (0x0)   execution time : 0.089 s
Press any key to continue.

We will not use Do..While loop that much, but it's there if we need it.

C++ While Loop

While loops are awesome. We will use it when there's need for some operation to run all the time, or until specific top level is reached.

First run this code, and then we will explain: 

#include <iostream>

using namespace std;

int main() {

    int counter = 0;

    while (counter <= 20) {
        cout << "Counter at the moment: " << counter << endl;
        counter = counter + 1;
    }

   return 0;
   
}

Result:

Counter at the moment: 0
Counter at the moment: 1
Counter at the moment: 2
Counter at the moment: 3
Counter at the moment: 4
Counter at the moment: 5
Counter at the moment: 6
Counter at the moment: 7
Counter at the moment: 8
Counter at the moment: 9
Counter at the moment: 10
Counter at the moment: 11
Counter at the moment: 12
Counter at the moment: 13
Counter at the moment: 14
Counter at the moment: 15
Counter at the moment: 16
Counter at the moment: 17
Counter at the moment: 18
Counter at the moment: 19
Counter at the moment: 20

Process returned 0 (0x0)   execution time : 0.075 s
Press any key to continue.

First we must establish initial counter to be 0, OUTSIDE while loop:

int counter = 0;

Then while loop where counter must reach 20.

Inside curly brackets we will have operations of simple printing, but also for every pass inside while loop counter must be incremented by one. To do that we will say "counter = counter + 1;":

while (counter <= 20) {
        cout << "Counter at the moment: " << counter << endl;
        counter = counter + 1;
    }

Perhaps "counter = counter + 1;" looks a bit weird but it's very usefull and we will use it all the time in programming. Sure, you can increment by any other number not just by one.

While loop with Input from Keyboard

In this source we will set counter starting position using keyboard. Jumps are hard-coded and set to be 5. Until now you know how to get stuff from keyboard using cin command.

Run this code with value 50, for example:

#include <iostream>

using namespace std;

int main() {

    int counter;

    cout << "Enter Starting Position: " << endl;
    cin >> counter;

    while (counter <= 100) {
        cout << "Counter at the moment: " << counter << endl;
        counter = counter + 5;
        cout << "------------------------" << endl;
    }

   return 0;
   
}

Result:

Enter Starting Position:
50
Counter at the moment: 50
------------------------
Counter at the moment: 55
------------------------
Counter at the moment: 60
------------------------
Counter at the moment: 65
------------------------
Counter at the moment: 70
------------------------
Counter at the moment: 75
------------------------
Counter at the moment: 80
------------------------
Counter at the moment: 85
------------------------
Counter at the moment: 90
------------------------
Counter at the moment: 95
------------------------
Counter at the moment: 100
------------------------

Process returned 0 (0x0)   execution time : 4.583 s
Press any key to continue.

Now we will have example with all 3 values set using keyboard. Values for starting position, top_level and jump (steps).

First we need 3 variables:

int counter;
int top_level;
int jump;

And 3 cins to grab values: 

cout << "Enter Starting Position: " << endl;
cin >> counter;

cout << "Enter Top Level: " << endl;
cin >> top_level;

cout << "Enter Jump : " << endl;
cin >> jump;

In while loop we substituted hard-coded values with variables in their appropriate places:

while (counter <= top_level) {
        cout << "Counter at the moment: " << counter << endl;
        counter = counter + jump;
        cout << "------------------------" << endl;
    }

This is full source: 

#include <iostream>

using namespace std;

int main() {

    int counter;
    int top_level;
    int jump;

    cout << "Enter Starting Position: " << endl;
    cin >> counter;

    cout << "Enter Top Level: " << endl;
    cin >> top_level;

    cout << "Enter Jump : " << endl;
    cin >> jump;

    while (counter <= top_level) {
        cout << "Counter at the moment: " << counter << endl;
        counter = counter + jump;
        cout << "------------------------" << endl;
    }

   return 0;

}

Result for counter = 10, top_level = 50, jump = 5 using keyboard:

Enter Starting Position:
10
Enter Top Level:
50
Enter Jump :
5
Counter at the moment: 10
------------------------
Counter at the moment: 15
------------------------
Counter at the moment: 20
------------------------
Counter at the moment: 25
------------------------
Counter at the moment: 30
------------------------
Counter at the moment: 35
------------------------
Counter at the moment: 40
------------------------
Counter at the moment: 45
------------------------
Counter at the moment: 50
------------------------

Process returned 0 (0x0)   execution time : 7.744 s
Press any key to continue.

Very useful.

C++ If Else Statement

We will use if.else block of code every time when we know exactly what will be last possible option that we don't need to check.

For example, if if checks are not successful there must be last possible option that will work. You will understand this much better through practical examples over time.

In general, this is basic structure of multiple check where at the end we have (under "else") direct execution because nothing else is possible:

#include <iostream>

using namespace std;

int main() {

    if () {}
    if () {}
    if () {}
    else {
        
    }
	
	return 0;

}

In following code first we are establishing one variable with value 2500:

int check = 2500;

Then there's check if number is greater than or equal to 5000:

if (check >= 5000) {
        cout << "I am happy this month" << endl;
    }

Logically, if number is NOT greater or equal  to 5000 only posible option is that number MUST be smaller than 5000 so there's NO need to check any more.

In that case we will use "else" at the end of if..else block of code:

else {
        cout << "Dude, I need to change this job" << endl;
    }

This is full working source: 

#include <iostream>

using namespace std;

int main() {

    int check = 2500;

    if (check >= 5000) {
        cout << "I am happy this month" << endl;
    }
    else {
        cout << "Dude, I need to change this job" << endl;
    }
	
	return 0;

}

Result:

Dude, I need to change this job

Process returned 0 (0x0)   execution time : 0.045 s
Press any key to continue.

If we change initial value from to 2500 to 60000, for example, this will be result:

I am happy this month

Process returned 0 (0x0)   execution time : 0.053 s
Press any key to continue.

If..Else Statement with Inputs from Keyboard

We can combine our knowledge from past tutorials into program that will take input and check things using if..else block of code: 

#include <iostream>

using namespace std;

int main() {

    int check;

    cout << "Please, enter monthly paycheck: " << endl;
    cin >> check;

    if (check >= 5000) {
        cout << "That's fine" << endl;
    }
    else {
        cout << "Meh..." << endl;
    }

   return 0;
   
}

Result with input 10:

Please, enter monthly paycheck:
10
Meh...

Process returned 0 (0x0)   execution time : 2.562 s
Press any key to continue.

Result with input 80000:

Please, enter monthly paycheck:
80000
That's fine

Process returned 0 (0x0)   execution time : 3.855 s
Press any key to continue.

So far so good.

C++ Comparing Numbers

This will be easy one. Using If statements we will compare values in variables to see if they are equal or not using multiple if checks:

First we will create two variables that will hold integers:

int a = 43;
int b = 23;

Then If check to see if a and b are equal using double equal symbols:

if (a == b) {
        cout << "Numbers are Same" << endl;
    }

To check if they are not equal the exclamation mark and equal symbol are used together (no space between them):

if (a != b) {
        cout << "Numbers are NOT Same" << endl;
    }

This is full source: 

#include <iostream>

using namespace std;

int main() {

    int a = 43;
    int b = 23;

    if (a == b) {
        cout << "Numbers are Same" << endl;
    }
    if (a != b) {
        cout << "Numbers are NOT Same" << endl;
    }
	
	return 0;

}

Result:

Numbers are NOT Same

Process returned 0 (0x0)   execution time : 0.066 s
Press any key to continue.

Comparing Values taken from Keyboard

Run this code and then we will explain: 

#include <iostream>

using namespace std;

int main() {

    int a;
    int b;

    cout << "Please enter number in a: " << endl;
    cin >> a;
    cout << "Please enter number in b: " << endl;
    cin >> b;

    if (a == b) {
        cout << "Numbers are Same" << endl;
    }
    if (a != b) {
        cout << "Numbers are NOT Same" << endl;
    }
	
	return 0;

}

Result using numbers 10 and 255 as input:

Please enter number in a:
10
Please enter number in b:
255
Numbers are NOT Same

Process returned 0 (0x0)   execution time : 8.171 s
Press any key to continue.

First we are creating two variables that will be populated with keyboard input:

int a;
int b;

Then we grab first number using cin:

cout << "Please enter number in a: " << endl;
cin >> a;

Time to get second number:

cout << "Please enter number in b: " << endl;
cin >> b;

If checks to compare values in a and b variable:

if (a == b) {
        cout << "Numbers are Same" << endl;
    }
if (a != b) {
        cout << "Numbers are NOT Same" << endl;
    }

C++ If Statement

In programming we will use If Statement all the time. It is decision-making statement. We can find it in most of the programming languages heavily used today.

With If Statements we will implement criteria that must be fulfilled.

In this simple example we have variable number with starting value 5.

Then with if statement we are checking if numbers is smaller then 3. If that's the case, we are printing "Number less than 3":

#include <iostream>

using namespace std;

int main() {

    int number = 5;

    if (number < 3) {
        cout << "Number less than 3" << endl;
    }
	
	return 0;

}

There's no printing on screen at the moment because variable is 5:


Process returned 0 (0x0)   execution time : 0.051 s
Press any key to continue.

That means we need to fix our source with one more if statement to cover all options: 

#include <iostream>

using namespace std;

int main() {

    int number = 5;

    if (number < 3) {
        cout << "Number less than 3" << endl;
    }
    if (number > 3) {
        cout << "Number is bigger than 3" << endl;
    }
	
	return 0;

}

Now we have usefull result:

Number is bigger than 3

Process returned 0 (0x0)   execution time : 0.049 s
Press any key to continue.

Sure, we can use not just less and greater than symbol, but we can check for numbers being equal with another If Statement: 

#include <iostream>

using namespace std;

int main() {

    int number = 5;

    if (number < 3) {
        cout << "Number less than 3" << endl;
    }
    if (number > 3) {
        cout << "Number is bigger than 3" << endl;
    }
    if (number >= 5) {
        cout << "Number is equal or bigger than 5" << endl;
    }
	
	return 0;

}

Result:

Number is bigger than 3
Number is equal or bigger than 5

Process returned 0 (0x0)   execution time : 0.051 s
Press any key to continue.

We have 2 confirmed cases here, number is bigger than 3 and is equal as 5. That means we can have multiple if checks one after another.

C++ Data Types

C++ is one of those programming languages where we must extremely precise while creating variables because end result of operations heavely depend on us thinking in advance about inputs for programs and functions we write.

At the moment we are familiar with whole numbers (integers) where we use int as data type:

int a = 5;

If there's need to be more precise float is used as data type: 

float b = 4323.4323;

And if some operations demands extra precision we will use double as data type:

double z = 4323.4323546;

To use individual characters we must use single quotes only, and data type will be char:

char let = 'A';

For simplicity reasons we wil just print our variables using this source:

cout << "Number from b: " << b << endl;

cout << "Character from container let: " << let << endl;

cout << "Number from z: " << z << endl;

This is full working source:

#include <iostream>

using namespace std;

int main() {

    int a = 5;
    float b = 4323.4323;
    double z = 4323.4323546;

    char let = 'A';

    cout << "Number from b: " << b << endl;

    cout << "Character from container let: " << let << endl;

    cout << "Number from z: " << z << endl;

    return 0;

}

Result:

Number from b: 4323.43
Character from container let: A
Number from z: 4323.43

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

These are basic data types in C++, but over time you will learn many more

If you have same time there's nice page on Wiki about C++ data types. You don't need to memorize ranges of values, just make note that you can use it, if you need it.

C++ Basic Math Operations

In this tutorial we are dealing with basic math operations. It's easy. Just run this code, and than we will explain:

#include <iostream>

using namespace std;

int main() {

    int a = 81;
    int b = 20;

    int add = a + b;
    int sub = a - b;
    int mul = a * b;
    int div = a / b;

    int rem = a % b;

    cout << "---------------------------" << endl;
    cout << "Add: " << add << endl;
    cout << "Sub: " << sub << endl;
    cout << "Mul: " << mul << endl;
    cout << "Div: " << div << endl;
    cout << "---------------------------" << endl;

    cout << "Remainder: " << rem<< endl;

    cout << "---------------------------" << endl;

    return 0;

}

Result:

---------------------------
Add: 101
Sub: 61
Mul: 1620
Div: 4
---------------------------
Remainder: 1
---------------------------

Process returned 0 (0x0)   execution time : 0.054 s
Press any key to continue.

First we have 2 variables that will hold some values, in this case integers 81 and 20 stored in a and b:

int a = 81;
int b = 20;

Then basic math operations. Integers will be data type for all results:

int add = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;

Also we have modulo operations, which just means remainder of division. We use percent simbol here:

int rem = a % b;

Then our source to print results:

cout << "---------------------------" << endl;
cout << "Add: " << add << endl;
cout << "Sub: " << sub << endl;
cout << "Mul: " << mul << endl;
cout << "Div: " << div << endl;
cout << "---------------------------" << endl;

cout << "Remainder: " << rem<< endl;

cout << "---------------------------" << endl;

Only thing here that's interesting is modulo operation that return remainder after one number is devided by another.

For example if we say 5 % 2, remainder will be 1, because 2 * 2 is 4 and to get to 5 we need to add 1.

C++ Keyboard Input

This one will be useful and fun. We will get values from keyboard.

Run this code, and then we will explain: 

#include <iostream>

using namespace std;

int main() {

    int first_number;
    int second_number;
    int result;

    cout << "Please, enter First Number: " << endl;
    cin >> first_number;

    cout << "Please, enter Second Number :" << endl;
    cin >> second_number;

    result = first_number + second_number;

    cout << "Result: " << result << endl;

    return 0;

}

Result:

Please, enter First Number:
10
Please, enter Second Number :
20
Result: 30

Process returned 0 (0x0)   execution time : 5.078 s
Press any key to continue.

First we will have 3 variables. They will be without value at start. We need variable for result too. Integers, of course:

int first_number;
int second_number;
int result;

Then we are asking for first value to populate variable using cin (Character In) instruction. This is important to note, just as 2 right arrows:

cout << "Please, enter First Number: " << endl;
cin >> first_number;

Same approach with second number:

cout << "Please, enter Second Number :" << endl;
cin >> second_number;

Then we have addition:

result = first_number + second_number;

Time to print results: 

cout << "Result: " << result << endl;

Just don't forget that for printing we re using cout, and for taking stuff from keyboard we need cin command. Also, semicolons need to be at the end of all lines.

You can expand this approach even if you are at the start of programming career to do some calculations at the job or school. Just with this knowledge only we can do a lot.

C++ Simple Calculator

Last tutorial about Telnet Server Header will be useful in this one. We will change welcome message to "Welcome to Simple Calculator v 0.1". Nothing special in header for calculator.

Then we are creating 2 variables first_number and second_number (integers, whole numbers) that will hold some values.

After that, we have another integer "result" which is result of addition of 2 variables we have.

Then, we are creating simple report using cout instructions. Run this code:

#include <iostream>

using namespace std;

int main() {

    cout << "#########################################" << endl;
    cout << "#" << endl;
    cout << "#" << endl;
    cout << "# Welcome to Simple Calculator v 0.1 #" << endl;
    cout << "#" << endl;
    cout << "#" << endl;
    cout << "#########################################" << endl;

    int first_number = 5;
    int second_number = 10;

    int result = first_number + second_number;

    cout << "-------------------------------------" << endl;
    cout << "Result of Additon: "                   << endl;
    cout << result                                  << endl;
    cout << "-------------------------------------" << endl;

    return 0;

}

 Result:

#########################################
#
#
# Welcome to Simple Calculator v 0.1 #
#
#
#########################################
-------------------------------------
Result of Additon:
15
-------------------------------------

Process returned 0 (0x0)   execution time : 0.022 s
Press any key to continue.

 You can also have simple version of some hard-coded calculation, but this is not useful that much. It's better to use variables.

#include <iostream>

using namespace std;

int main() {

    int result = 5 + 10;

    cout << result << endl;

    return 0;

}

Result: 

15

Process returned 0 (0x0)   execution time : 0.051 s
Press any key to continue.

C++ Telnet, Headers

In this tutorial we will play with Telnet "Server Header". We are trying to create (just for fun) old Telnet Welcome Message when users log in.

If you don't know what Telnet is, go to Google Image option and type "Telnet", or "Telnet Games" in a search box.

We are just creating bunch of string. You can use this type of Menu or Header in all other programming languages that you will learn over time. Especially if you are beginner and like to play around in Terminal or DOS.

Yes, you can copy-paste, but it's better idea to type this source manually, at least while you are learning.

Over time we will learn how to create real Menus in C++ that will run specific functions to get some job done. Run this code:

#include <iostream>

using namespace std;

int main() {

    cout << "#########################################" << endl;
    cout << "#" << endl;
    cout << "#" << endl;
    cout << "# Welcome to Telnet Control Panel v 0.1 #" << endl;
    cout << "#" << endl;
    cout << "#" << endl;
    cout << "#########################################" << endl;

    return 0;

}

Result:

#########################################
#
#
# Welcome to Telnet Control Panel v 0.1 #
#
#
#########################################

Process returned 0 (0x0)   execution time : 0.054 s
Press any key to continue.

There's corresponding YouTube tutorial that covers this topic.

And this is How to Activate Telnet Client on Windows 10, if you like to try it. This one is for FTP Client.

C++ Variables

Think about variables as names for containers where we will store data. Once we create variable there's no need to change their names, just values if we need to do that. We will use variables all the time, not just in C++ but also in many other programming languages.

In this example, we are creating variable "number" with value 10. Variable is whole number and in C++ we must state what is data type for variable so int (integer) is used.

Variable is created inside main() function, and after that it's printed using cout on dedicated line. Once we have variable created we can use it meny times. Run this code:

#include <iostream>

using namespace std;

int main() {

    int number = 10;

    cout << number << endl;

    return 0;

}

Result:

10

Process returned 0 (0x0)   execution time : 0.058 s
Press any key to continue.

We can create and use more than one variable with dedicated values, on dedicated lines. Multiple times if we need to:

#include <iostream>

using namespace std;

int main() {

    int number = 10;
    int container = 5000;
    int port_number = 80;

    cout << number << endl;
    cout << container << endl;
    cout << "------------------------" << endl;
    cout << port_number << endl;
    cout << "------------------------" << endl;

    return 0;

}

Result:

10
5000
------------------------
80
------------------------

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

We can align endlines vertically, but result will be the same: 

#include <iostream>

using namespace std;

int main() {

    int number = 10;
    int container = 5000;
    int port_number = 80;

    cout << number                      << endl;
    cout << container                   << endl;
    cout << "------------------------"  << endl;
    cout << port_number                 << endl;
    cout << "------------------------"  << endl;

    cout << container                   << endl;
    cout << container                   << endl;

    return 0;

}

Result:

10
5000
------------------------
80
------------------------
5000
5000

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

C++ Strings

In this tutorial we will talk more about strings. Strings are just bunch of a text that we need to print. That text is inside double quotes. 

#include <iostream>

using namespace std;

int main() {

    cout  << "This is first string";
    cout  << "This is second string";
    return 0;

}

When we run this code, this will be result:

This is first stringThis is second string
Process returned 0 (0x0)   execution time : 0.019 s
Press any key to continue.

Because we don't have endl at the end of instructions, string are concatenated.

#include <iostream>

using namespace std;

int main() {

    cout << "This is first string      \n";

    cout << "This is done using endl" << endl;
    cout << "This is done using endl" << endl;

    cout << "This is second string     \n";
    return 0;

}

After we run this code we can notice that having \n at the end of instruction also do job of that endl does. But we will use endl; mostly.

Having whitespaces verticaly will not influence end result, only \n and endl; instructions. Don't forget semicolons, they are important.

Result:

This is first string
This is done using endl
This is done using endl
This is second string

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

To get to new empty line we can use "\n" on dedicated line. Also, you can have empty string (just quotes) and with endl; that will be printed too:

#include <iostream>

using namespace std;

int main() {

    cout << "This is first string      \n";
    cout << "\n";
    cout << "This is done using endl" << endl;
    cout << "This is done using endl" << endl;
    cout << "" << endl;
    cout << "This is second string     \n";
    return 0;

}

Result:

This is first string

This is done using endl
This is done using endl

This is second string

Process returned 0 (0x0)   execution time : 0.317 s
Press any key to continue.

We can also style our output a bit using tabs, aligning quotes verticaly. Special simbols like underlines are also normal characters, there's no problem printing them:

#include <iostream>

using namespace std;

int main() {

    cout << "This is first string        " << endl;
    cout << "--------------------------- " << endl;
    cout << "This is done using endl     " << endl;
    cout << "This is done using endl     " << endl;
    cout << "___________________________ " << endl;
    cout << "This is second string       " << endl;

    return 0;

}

Result:

This is first string
---------------------------
This is done using endl
This is done using endl
___________________________
This is second string

Process returned 0 (0x0)   execution time : 0.051 s
Press any key to continue.

Strings are simple things.

C++ Line by Line Explanations

In this tutorial objective will be to understand this C++ code line by line:

#include <iostream>

using namespace std;

int main() {

    cout  << "Our custom string to print" << endl;
    return 0;

}

Explanation

#include <iostream>

In the most programming languages to activate additional functionalities that are writen by other programmers we need to use some form of "include" instruction.

For example, In C++ that's #include and in Python "import". After we type #include in our editor, on right side from it we must state what we need to import, in this case that's iostream (with arrows). Iostream contains source that will help us to display output on the screen and read input from the keyboard.

In general, lines staritng with # are called a preprocessor directives. They must be typed at the beggining of program, because other source will depend on it.

using namespace std;

This line means that we need "std" namespace with all functionalities in it. It's not enough to have iostream file included in our program, we also must say where those individual commands that we will use from iostream exactly resides in that file. All those functionalities are grouped into "std" namespace.

Consider namespaces as borders around specific piece of code that is located in some external file. Namespaces are like source locators in external files, so we call them to use them with lines like "using namespace std;".

Std means "Standard". Individual instructions from "std" namespaces are cin, cout, cerr, clog. They do specific tasks. We will talk about them more, don't worry.

int main() {
   
}

Every C++ program must have main function. That's place where all useful operations will be done. Name for function is "main" and after it we have parentheses. We need to have parentheses because that's the place where we will put "food" for functions so they can grab it and do something with it, some form of processing. 

Functions in C++ must have return type. In this case that's "int" as "integer". That means that result of function will be some whole number, in this case that's 0, which means that everything is ok with program.

cout  << "Our custom string to print" << endl;

cout is command from std namespace from iostream. It means "whatever is right from me it will be printed on screen". After that we must type 2 left-oriented arrows. 

After arrows we have some string. String is defined with 2 double-quotes and bunch of text in-between. This is what we must see on screen printed.

After that we have again 2 left-oriented arrows, and endl with semicolon. Endl in C++ will be used to insert a new line characters and flushes the stream. That just means we are going into another line.

We must use semicolon after end of any instructions. This is how we instruct C++ to finish that one, and go to next operation.

 return 0;

With "return 0" we are stating manually that our program is 100 % correct. 

int main() {

    cout  << "Our custom string to print" << endl;
    return 0;

}

Please note, all our custom code will go inside pair of braces (Curly brackets). They are like border around our own code.

Saturday, March 22, 2025

C++ First Program and Compilation

Once we run Code::Block we will be presented with this interface. Click on "Create a new project":


This form will open up. Click on "Empty Project" and than click on "Go":


Click on "Next":


Name for our project will be "Main". You can leave default path for files, just remember where your files are:


We will set compiler to be GNU GCC, and also checkboxes "Create Debug configuration" and "Create Release configuration". Leave paths default. Click on "Finish":


Go to Menu and click on File > Empty File:



Also, confirm adding this file to Project:



File explorer will open up. Save file as
main.cpp Please note, file name is lowercase main.cpp, don't forget .cpp extension.

 New window will start. Check both "Debug" and "Release" options and click on "OK":


Copy-Paste this source code in main.cpp. We need to compile it into executable.

We will explain every line in future tutorials, but for now we are happy if we can get executable file.

#include <iostream>

using namespace std;

int main() {

    cout  << "Our custom string to print" << endl;
    return 0;

}

After source is pasted, or typed, click in Menu on Build and Run to create and execute main.exe:


If you see black screen with these lines, we compiled main.cpp into main.exe just fine:

You will find main.exe in Main project folder, inside bin, inside Debug folder. We can also use DOS to run our .exe programs.

In future tutorials we will explain source code from this tutorial.

So far, so good.

You are strongly advised to check corresponding YouTube video on How to Compile C++ source into Executable.

C++ Introduction

How to Set Up C++ Development Environment on Windows

If you are using older versions of Windows and 32 bit machines consider installation of Dev-C++ from official bloodshed.net. Their C++ IDE supports Windows 98, NT, 2000, XP. As they state on their site: "Millions of developers, students and researchers use Dev-C++ since the first version was released in 1998."

For Windows 10 machines our best bet will be to grab Code::Block which is "Free C/C++ and Fortran IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable."

In tutorials on this site and in corresponding YouTube playlist we will use Code::Block. It's easy to use.

Go to official Code::Block site and grab setup files for system you have.

Install Code::Block and in next tutorial we will run our first simple C++ program.

You can check corresponding YouTube Tutorial down below if you like video format more. Whole YT C++ playlist is here.

Don't worry about Object Oriented C++, there's dedicated playlist for that too. But first we need to handle basic C++ programming.



Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...