Computer Programming C/C++ | DIT| First Semister
Computer Programming C/C++:
C Introduction:
What is C?
C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.
It is a very popular language, despite being old.
C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Why Learn C?
- It is one of the most popular programming languages in the world
- If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar
- C is very fast, compared to other programming languages, like java
- C is very versatile; it can be used in both applications and technologies
Difference between C and C++
- C++ was developed as an extension of C, and both languages have almost the same syntax
- The main difference between C and C++ is that C++ supports classes and objects, while C does not.
Syntax of C:
int main() {
printf("Hello World!");
return 0;
}
C++ Introduction:
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.
Difference between C and C++
C++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ supports classes and objects, while C does not.
C++ Get Started
To start using C++, you need two things:
- A text editor, like Notepad, to write C++ code
- A compiler, like GCC, translates the C++ code into a language that the computer will understand
There are many text editors and compilers to choose from. In this tutorial, we will use an IDE (see below).
C++ Install IDE
An IDE (Integrated Development Environment) is used to edit AND compile the code.
Popular IDEs include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug C++ code.
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):
C++ Syntax
Let's break up the following code to understand it better:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
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. But we use it to make the code more readable.
Line 4: Another thing that always appears 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 "see-out") is an object used together with the insertion operator (<<
) to output/print text. In our example, it will output "Hello World".
Note: Every C++ statement ends with a semicolon ;
.
Note: The body of int main()
could also be written as:int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines make the code more readable.
Line 6: return 0
ends the main function.
Line 7: Do not forget to add the closing curly bracket }
to actually end the main function.
Understanding The Structure Of C++
Understanding The Date Types & Variable Of C++:
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 -123double
- stores floating point numbers, with decimals, such as 19.99 or -19.99char
- stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesstring
- stores text, such as "Hello World". String values are surrounded by double quotesbool
- stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax
type variableName = value;
Where type is one of C++ types (such as int
), and variableName 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 variable called myNum of type int
and assign it the value 15:
int myNum = 15;
cout << myNum;
Understanding The Control Structure Of C++:
C++ Conditions and If Statements
You already know that C++ supports the usual logical conditions from mathematics:
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
C++ has the following conditional statements:
- Use
if
to specify a block of code to be executed, if a specified condition is true - Use
else
to specify a block of code to be executed, if the same condition is false - Use
else if
to specify a new condition to test, if the first condition is false - Use
switch
to specify many alternative blocks of code to be executed
The if Statement
Use the if
statement to specify a block of C++ code to be executed if a condition is true
.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if
is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two values to find out if 20 is greater than 18. If the condition is true
, print some text:
Understanding The Loops Of C++:
C++ Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
C++ For Loop
When you know exactly how many times you want to loop through a block of code, use the for
loop instead of a while
loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
Example
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
C++ While Loop
The while
loop loops through a block of code as long as a specified condition is true
:
Syntax
while (condition) {
// code block to be executed
}
In the example below, the code in the loop will run, over and over again, as long as a variable (i
) is less than 5:
Example
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
The Do/While Loop
The do/while
loop is a variant of the while
loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while
loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
Example
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
No comments:
Post a Comment