0% found this document useful (0 votes)
24 views56 pages

Combine PDF

The document provides an introduction to C++ programming, including a brief history of C++ and its development, the structure of a basic C++ program including header files and namespaces, and an example of a simple "Hello World" program to demonstrate how to write, build, and run a basic C++ program in Code::Blocks IDE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views56 pages

Combine PDF

The document provides an introduction to C++ programming, including a brief history of C++ and its development, the structure of a basic C++ program including header files and namespaces, and an example of a simple "Hello World" program to demonstrate how to write, build, and run a basic C++ program in Code::Blocks IDE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

INTRODUCTION

TO
COMPUTERS
AND LOGIC
PROGRAMMING LOGIC AND DESIGN
UNIT 1 (LESSON 1 – 3)
What is Algorithm?
It is a finite sequence of well-defined,
computer-implementable instructions, typically to
solve a problem.
What is Algorithm?
Algorithm is a step-by-step procedure for solving a
problem.
What is Algorithm?
Algorithm is not the complete code or program.

It is just the core logic of a problem.


Sequence

Classification
of Algorithm Branching (Selection)
according to
Structures
Loop (Repetition)
Define Define the problem.

List List the inputs.

How to Write Describ Describe the steps needed to manipulate the inputs to
produce the desired outputs.
e
an Algorithm?
Display Display the outputs.

Test Test the algorithm


It is an outline of a
program, written in a form
that can easily be
converted in real
What is a programming statements.
Pseudocode? It is an informal language
that has no syntax rules
and is not meant to be
compiled or executed.
Why do we need to create a
Pseudocode?
The main goal of a pseudo-code is to explain what
exactly each line of a program should do, hence
making the code construction phase easier for the
programmer.
Commonly used keywords in writing a pseudocode
Commonly used keywords in writing a pseudocode
How to write a Pseudocode?

1. Start with the statement of a pseudo-code which establishes the main


goal or the aim.
How to write a Pseudocode?

2. The way the if-else, for, while loops are indented in a program, indent
the statements likewise, as it helps to comprehend the decision
control and execution mechanism. They also improve the readability to
a great extent.
How to write a Pseudocode?
3. Use appropriate naming conventions.

4. Elaborate everything which is going to happen in the actual code. Don’t


make the pseudo code abstract.

6. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’


the way we use it in programming.

7. Check whether all the sections of a pseudo-code are complete, finite, and
clear to understand and comprehend.

8. Don’t write the pseudocode in a complete programmatic manner.


Pseudocode Example 1:
Traffic Light Processing
• Start
• Input color
• If color = “red”
• Output “STOP”
• Else if color = “yellow”
• Output “WAIT”
• Else
• Output “PASS”
• Stop
Pseudocode Example 2:
Compute Age for the
Current Year

• Start
• Declare variable N as year of
birth, A as age;
• Read input of user then
store to N;
• A = 2022 - N;
• Print A;
• Stop
Pseudocode Example 3:
Calculation of Simple
Work Pay

•Begin
•Input hours
•Input rate
•pay = hour * rate
•Print pay
•End
Pseudocode Example
4:Calculation of Work
Pay with Overtime Pay

•Begin
•Input hours, rate
•If hours <= 40 then
• pay = hours * rate
•Else
• pay = (40 * rate) + ((hours - 40) * rate * 1.5)
•Print pay
•End
What is a
Flowchart?
It is a type of diagram that
represents an algorithm or
process, showing the steps as
boxes of various kinds, and
their order by connecting these
with arrows.
—A flowchart is a pictorial or
diagrammatically
representation of the flow of
steps in to solve a given
What is a problem.
Flowchart? —Frank Gilberth introduced
flowcharts in 1921, and they
were called “Process Flow
Charts” at the beginning.
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
1. All boxes of the flowchart are connected with Arrows or flow lines.

2. Flowchart symbols have an entry point on the top of the symbol with
no other entry points. The exit point for all flowchart symbols is on the
bottom except for the Decision symbol.

3. The Decision symbol has two exit points; these can be on the sides
General of the bottom and one side.

Rules for 4. Generally, a flowchart will flow from top to bottom. However, an
upward flow can be shown as long as it does not exceed 3 symbols.
Flowcharting
5. Connectors are used to connect breaks in the flowchart.

6. All flow charts start and end with a Terminal symbol.


Flowchart
Example 1:
Calculation
of simple
work pay
Flowchart
Example 2:
Calculation
of work pay
with
overtime
pay
Flowchart Example 3: Get the Average of 3 inputted numbers
using a predefined process for average.
Create a Pseudocode for a
problem getting the average of
10 inputted numbers using
Loop.

Seatwork 1
Draw a flowchart to sort A, B,
and C in ascending order.
Review of C Programming

• C language is a general purpose programming


language.

• C language is called as a system programming


language.

• C language is designed by Dennis Ritchie in


1972
Parts of C program
<HEADER FILE>

MAIN()
{
VARIABLE DECLARATION
BODY OF PROGRAM
}
Example
/* A simple C Program */
#include<stdio.h>
#include<conio.h>
main()
{
printf(“This is the output of my first C program. \n”);
getch();
}
// end of Program D.1

OUTPUT:
This is the output of my first C program.
Data Types

Type Description
Char Characters
Int Integer
Float Floating point
Double Double floating point
void Valueless
Arithmetic Operators

SYMBOLS MEANING EXAMPLE


+ Addition A+B
- Subtraction A-B
* Multiplication A*B
/ Division A/B
% Modulus N = R%2
Relational Operators

OPERATOR ACTION
> Greater Than
>= Greater Than Equal
< Less Than
<= Less Than Equal
== Equal
!= Not Equal
Logical Operators

OPERATOR ACTION
&& AND
|| OR
! NOT
If

If else

Conditional If elseif else

Statements Nested If

Nested If else

Switch case
For loop

Looping While loop

Do while loop
Single
dimensional
array
Array
Multi-dimension
al array
Write a C program that will Print Floyd’s Triangle.
A user will input how many numbers of rows of
Floyd's triangle to print.

First four rows of Floyd's triangle are: 4


Laboratory 1
23
Activity 1: 456
7 8 9 10

It's clear that in Floyd's triangle, nth row contains n


numbers.
Laboratory Activity 1:
INTRODUCTION TO
COMPUTERS AND LOGIC
Basics of C ++
Dr. Mhuggz V. Gatdula
A BRIEF HISTORY OF C++
O In the early 1980's, at Bell Laboratories, another
programming language was created which was
based upon the C language and was called C++. 
O This new language was developed by a Danish
computer scientist named Bjarne Stroustrup. 
O According to Stroustrup, the primary purpose of
C++ is to make writing good programs easier and
more pleasant for the individual programmer. 
O C++ was designed with added features such as
OOP(Object Oriented Programming).
2
A BRIEF HISTORY OF C++
O C++ is a superset of C language which means that
any valid program in C is also a valid program in C++.
O Many other software implement the C++ language in
a slightly different manner, these software includes
Borland C++, Turbo C++, and Code Warrior (Mac).
O The first commercial edition of C++ programming
language was released in October 1985.
O Due to their power and ease of use, C and C++ were
used in the programming of the special effects for
Star Wars.
3
STRUCTURE OF A PROGRAM
O According to 2016 IEEE Top Programming
Language ranking, C++ ranks 4th in popularity.
Learning C++ is an advantage for all
programmers.

O To write a program in code blocks environment:


O File ⇒ New ⇒ Empty File.
O Enter the following codes:

4
STRUCTURE OF A PROGRAM

1
2

3
4 1
5

5
STRUCTURE OF A PROGRAM
O Save the file as "Hello.cpp" in your project
directory.
O Select "Build" menu ⇒ Build (Ctrl-F9) or
Select "Build" menu ⇒ Run (Ctrl-F10).
O After running the program you should get
the output on the next slide.

6
STRUCTURE OF A PROGRAM

7
Explanation of the Program
1. These are called comments, they are NOT executable and are
ignored by the compiler; but they provide useful explanation
and documentation to other readers. A comment starts with
//, extending to the end of the line. Comments may also start
with /* and end with */.
2. The "#include" is called a preprocessor directive. This tells the
preprocessor to include the "iostream" header file to support
input/output operations. The "using namespace std;"
statement declares std as the default namespace used in this
program. The names cout and endl belong to the std
namespace.
3. The main() function is the entry point of program execution.
main() is required to return an int (integer). 8
Explanation of the Program
4. "cout" refers to the standard output (or Console OUTput).
The symbol << is called the stream insertion operator (or
put-to operator), which is used to put the string "hello,
world" to the console. "endl" denotes the END-of-Line or
newline, which is put to the console to bring the cursor to
the beginning of the next line.
5. Terminates the main() function and returns a value of 0 to
the operating system. Typically, return value of 0 signals
normal termination; whereas value of non-zero (usually 1)
signals abnormal termination.
6. The semicolon is a statement terminator. Each individual
statement must be ended with a semicolon and it
indicates the end of one logical entity.

9
C++ Quickstart
Let's create our first C++ file.

#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}
Omitting Namespace
C++ Output (Print text)
The cout object, together with the << operator, is used to output
values/print text:
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:
C++ User Input
cin is a predefined variable that reads data from the keyboard
with the extraction operator (>>).
C++ New Lines
To insert a new line, you can use the \n character:

Another way to insert a new line, is with the endl manipulator:


Example 1: Creating a Simple Calculator
Write a C++ program that performs basic arithmetic operations of
addition, subtraction, multiplication, and division of two
numbers/integers that user inputs.

You might also like