Programming Fundamental Lab Manual 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Programming fundamentals

What is programming?
Programming is the process of creating sets of instructions that tell a computer what to
do.
It involves writing code in a programming language to solve problems or automate tasks .

Key Elements of Programming


● Instructions: Creating step-by-step instructions to guide the computer's actions.
● Language: Using a programming language to communicate with the computer.
● Problem Solving: Analyzing problems and designing solutions using logical
thinking.

Programming Languages
Definition: Programming languages are formal languages designed to communicate
instructions to a computer.
Examples: Python, Java, C++, Kotlin

what is computer language?


The computer language is defined as code or syntax which is used to write programs.
Low level language
• Machine Language
• Assembly Language
High Level Language
• C, C++, java, C# etc
Language processors:
• Assembler
• Compiler
• Interpreter

Assembler converts program written in assembly language into machine language.

Compilers and interpreters are program that help convert the high level language
into machine code.

Compiler Vs Interpreter

• Compiler convert the whole high level language program to machine language
at a time while

• Interpreter converts high level language program to machine language line by


line.

C++ Compiler:

• GCC stands for GNU Compilers Collections is used to compile mainly C and
C++ language.

Installation & overview


Installation of IDE
An IDE (Integrated Development Environment) is used to edit AND compile the code.
Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and
they can be used to both edit and debug C++ code.

Note: Web-based IDE's can work as well, but functionality is limited.

We will use Code::Blocks

Download and Install Code Blocks C++ IDE on Windows


Code Blocks is a free and cross platform IDE for C, C++, and Fortran. Here is the list of
features available in Code Blocks IDE –

How to download and install Code Blocks


Download the latest version of Code Blocks for your Operating System from
here http://www.codeblocks.org/downloads
'OR' You can also download the latest version for Windows directly from the following
download button if the above method doesn't work -
DOWNLOAD CODE BLOCKS
Run the downloaded .exe file to install Code Blocks in your system.
Follow the setup instructions.
You can watch the following video to check how to download and install Code Blocks IDE
for Windows –
https://www.youtube.com/watch?v=eXx2bmHm7ZQ

Installation Steps
What is C++?

C++ is a cross-platform language that can be used to create high-performance applications.


C++ was developed by Bjarne Stroustrup, as an extension to the C language.
C++ gives programmers a high level of control over system resources and memory.
The language was updated 4 major times in 2011, 2014, 2017, and 2020 to C++11, C++14,
C++17, C++20.

Why Use C++

C++ is one of the world's most popular programming languages.


C++ can be found in today's operating systems, Graphical User Interfaces, and embedded
systems.
C++ is an object-oriented programming language which gives a clear structure to programs
and allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
C++ is fun and easy to learn! As C++ is close to C, C# and Java, it makes it easy for
programmers to switch to C++ or vice versa.

Create First Project


C++ Quickstart

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
1. #include <iostream>
2. using namespace std;
3. int main() {
4. cout << "Hello World!";
5. return 0;
6. }

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:

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:

 #include <iostream> is a header file library that lets us work with input and output
objects, such as cout. Header files add functionality to C++ programs.
 using namespace std means that we can use names for objects and variables from
the standard library.

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.

C++ Output (Print Text)

The cout object, together with the insertion operator (<<), is used to output
values/print text.

Note: Every C++ statement ends with a semicolon ;.

New Lines

To insert a new line, you can use the endl or \n character:

#include <iostream>
using namespace std;

int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}

Or

#include <iostream>
using namespace std;

int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}

C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It can also
be used to prevent execution when testing alternative code. Comments can be singled-
lined or multi-lined.
Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be
executed).
This example uses a single-line comment before a line of code:

Example

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

C++ Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

Example

/* The code below will print the words Hello World!


to the screen, and it is amazing */
cout << "Hello World!";

You might also like