0% found this document useful (0 votes)
17 views

Chapter One

Uploaded by

werkezebo5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter One

Uploaded by

werkezebo5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CHAPTER ONE

Basics of C++

November 6, 2023
Outline

 Introduction

 Structure of a Program

 Constants

 Operators

 Basic input / Output


2
Introduction
• Computer requires programs to function, and a computer
programs does nothing unless its instructions are executed by
a CPU.
• Computer programming shortened to programming or coding
is the process of writing, testing, debugging/troubleshooting,
and maintaining the source code of computer programs.
• Writing computer programs means writing instructions that
will make the computer follow and run a program based on the
instructions.
• computer programs=software programs=programs -instructions
that tells the computer what to do.
3
…continued
• A computer program(source code) written by professionals known
as Computer Programmers (programmers).
• Source code is written in one of programming languages. A
programming language is an artificial language that can be used to
control the behavior of a machine, particularly a computer.
• Programming languages are defined by syntactic and semantic
rules which describe their structure and meaning respectively.
• The syntax of a language describes the possible combinations of symbols
that form a syntactically correct program.
• Semantics handled meaning of a given combined symbols.
4
…continued
• Program Development Lifecycle
• Feasibility Study, Requirement analysis, Designing solution, Testing,
Implementation, Integration and system testing, Maintenance
• An algorithm is defined as a step-by-step sequence of
instructions that must terminate and describe how the data is
to be processed to produce the desired outputs.
• Tools to describe algorithm: flowcharts, Structure chart,
Pseudo code.

5
Structure of a Program
• Documentation

• Link Section

• Definition Section

• Global Declaration Section

• Function Definition Section

• Main Function
6
Structure of a Program
// Documentation Section
// Global Declaration Section
/* This is a C++ program to find the factorial of a
int num = 0, fact = 1, storeFactorial = 0;
number
The basic requirement for writing this program is to
// Function Section
have knowledge of loops
int factorial(int num)
To find the factorial of a number iterate over the
{
range from number to 1
// Iterate over the loop from
*/
// num to one
for (INTEGER i = 1; i <= num; i++) {
// Linking Section
fact *= i;
#include <iostream>
}
using namespace std;
// Return the factorial
return fact;
// Definition Section
}
#define msg "FACTORIAL\n"
typedef int INTEGER; 7
Structure of a Program
// Main Function
int main()
{
// Given number Num
int Num = 5;

// Function Call
storeFactorial = factorial(Num);
cout << msg;

// Print the factorial


cout << Num << "! = "<< storeFactorial << endl;
return 0;
}

8
…Continued

9
Constants
• Variables -name given to a memory location. It is the basic unit of
storage in a program.
• The value stored in a variable can be changed during program execution.

• A variable is only a name given to a memory location, all the operations


done on the variable effects that memory location.
• All variables must be declared before use(Single and Multiple
declaration)
• Have Data type, Variable name, value 10
…continued
• Variable name can be start with letters and underscore(not number)
• Contain number, letters, underscore
• Can not be keywords
• Declaration
• Definition = Declaration + initialization
• Reserved/Key words have a unique meaning within a C++
program. These symbols, the reserved words, must not be used
for any other purposes.
• All reserved words are in lowercase letters. (float,if,do…)
11
…Continued
• Constants - read-only variables whose values cannot be
modified once they are declared
• Using the keyword const

• Const float gravity = 9.8

• Using #define preprocessor (#define pi 3.14)

12
Operators
• An operator is a symbol that operates on a value to perform
specific mathematical or logical computations.
• An operator operates the operands. For example,
• int c = a + b; ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands
that are being ‘added’.
• Operators in C++
• Arithmetic Operator(+, -, *, /, %,++,--)
• Assignment operator (=, +=,-=,*=, /=)

13
…Continued
• Relational Operator( comparsion)-
<,>,==, >= , <=
• Logical Operator(combine 2 or more
condition/constraint) - &&,||,!
 Increment/Decrement Operator
• Prefix(++Age) and postfix(Age++)
 Operator Precedence
14
 int a,b,c,d, a=b+c*d (* have priority)
Basic input / Output
• C++ have libraries that provide many ways for performing
input and output. In C++ input and output are performed in
the form of a sequence of bytes or more commonly known
as streams.
• Input Stream: If the direction of flow of bytes is from the
device(Keyboard) to the main memory then this process is
called input.
• Output Stream: If the direction of flow of bytes is opposite,
i.e. from main memory to device( display screen ) then this
process is called output.
15
…Continued
• Header files available in C++ for Input/Output operations are:
• iostream: iostream stands for standard input-output stream. This header
file contains definitions of objects like cin, cout, cerr….
• iomanip: iomanip stands for input-output manipulators. The methods
declared in these files are used for manipulating streams. This file contains
definitions of setw, setprecision, etc.
• fstream: This header file mainly describes the file stream. This header file
is used to handle the data being read from a file as input or data being
written into the file as output. 16
…Continued
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<“Enter your age?”; //insertion operator
cin>>age; //extraction operator
cout<<“Your age is: ”<<age;
return 0;
}
17
Exercises
1. int a=5, y,
y=++a + 10;
y= a++ + 10;
y= --a + 10;
y= a-- + 10;
2. Write C++ program that display the sum of two numbers(user
have to enter those two numbers)
3. Write program that compare two number.

18
Exercises
#include <iostream>
cout << mark << endl;
using namespace std;
int main() mark--;
{ cout << mark << endl;
int mark = 76;
--mark;
cout << mark << endl;
mark++; cout << mark << endl;
cout << mark << endl; mark = mark - 1;
++mark;
cout << mark << endl;
cout << mark << endl;
return 0;
mark = mark + 1;
}
19
Thank you
Questions?

20
Next
Chapter 2
Control Structure
21

You might also like