Introduction To C Part 1
Introduction To C Part 1
Don’t forget:
★ ***** You have to study the previous session before the next session *****
★ It’s better to show your face and open camera.
★ Raise your hand if you want to ask a questions.
★ Stick with discussion time and don’t interrupt the session there is a time for Q / A after every topic
★ Attending sessions is not optional and it’s a part of your graduation
★ You have to attend with PC or Laptop, Phones are not allowed anymore.
WHAT IS PROGRAMMING ?
• You have to talk with it by a language it can understand ,and computers understand only (0,1) language .
• Programming language is high level language that can executed by machine by convert it to machine code, which is compiles by
computers into (0,1) to understand our instructions .
• Programming means writing instructions for a computer to perform desired actions by programmer.
• The instructions are written in some programming language that the computer understands.
• A programming language is a language that a computer understands. And it is an intermediary between a computer and a programmer.
• Programming languages provide the rules for building websites, apps, and other computer-based technologies.
• All the programming languages share a common goal—to be able to control the behavior and output of a computer.
• Low-level programming languages means that the language is close to machine code. This makes it highly efficient. But this also
makes it hard for us developers to understand, debug, and maintain.
• High-level programming languages tend to be more “English-like” languages so that are easier to learn
SESSION 1 OUTLINE
• Very brief history of C++
• Definition object-oriented programming
• Why C++ is a good choice
• Install CodeBlocks IDE
• First program!
• Some C++ syntax
• Variables
• Operators ( Assignment – Arithmetic – Increment/Decrement – Relational – Logical )
• IF / Else Statements
• Casting
• Error ( Syntax – Run time – Logic )
• Exercises
VERY BRIEF HISTORY OF C++
Simu
la 67
C++
WHY AND WHEN TO CHOOSE C++
• Despite its many competitors C++ has remained popular • Choose C++ when:
for ~30 years and will continue to be so in the • Program performance matters
foreseeable future. • Dealing with large amounts of data, multiple CPUs,
complex algorithms, etc.
• Why?
• Complex problems and programs can be effectively • The programming language itself can help organize
implemented your code
• Memory Mangement • Ex. In C++ your objects can closely model elements of
your problem
• No other language quite matches C++’s combination of
performance, expressiveness, and ability to handle • Access to libraries
complex programs. • Ex. Nvidia’s CUDA Thrust library for GPUs
• Multi paradigm language (Functional - OOP)
OBJECT-ORIENTED PROGRAMMING
• The principal of data hiding helps the programmer to build secure programs that cannot be invaded by code in
other part of the program.
• It is easy to partition the work in a project, based on objects.
• This is a highly effective way of modeling real world problems inside of a computer program.
• Object oriented systems can be easily upgraded from small to large systems.
• Through inheritance, we can eliminate redundant code and extend the use of existing classes.
• Software complexity can be easily managed.
• This is a highly effective way of modeling real world problems inside of a computer program.
OBJECT-ORIENTED PROGRAMMING
“Class Car”
public interface
• In this tutorial we will use the CodeBlocks integrated development environment (IDE) for writing
and compiling C++
• About C::B
• cross-platform: supported on Mac OSX, Linux, and Windows
• Oriented towards C, C++, and Fortran, supports others such as Python
• Short learning curve compared with other IDEs such as Eclipse or Visual Studio
• Has its own automated code building system, so we can concentrate on C++
• Project homepage: https://bit.ly/Codeblocks-Setup
OPENING C::B
• The 1st time it is opened C::B will search for compilers it can use.
• A dialog that looks like this will open. Select GCC if there are multiple options:
• The “cout “ object is used to display the output to the user screen.
Examples /
• Create a program to print your name.
• Create a program to print:
“Hello
World!“
BUILT-IN (PRIMITIVE) TYPES
• “primitive” means these types are not objects
• Primitive data type is the data type that allows you to store only single
value and always starts with a lowercase letter.
• Named constants are declared for constant and fixed values which are
referenced by :
• const int MAXITEMS = 100;
• const string NAME = "Fred Flintstone";
• const double PI = 3.141592654;
• const char NEWLINE = '\n’;
The C++ Standard specifies certain symbols and words as the official "vocabulary" of the C++ language.
These reserved words (and symbols) are preempted by the C++ language definition and may only be used in
the manner the language rules specify.
Fortunately the number of reserved words is relatively small (< 300). Here's a sample:
• C++ is strongly typed. It will auto-convert a variable of one type to another in a limited
fashion: if it will not change the value.
short x = 1 ;
int y = x ; // OK
short z = y ; // NO!
• Conversions that don’t change value: increasing precision (float double) or integer
floating point of at least the same precision.
• C++ allows casting with the syntax: (new type) expression
double x = 1.0 ;
int y = (int) x ;
float z = (float) (x / y) ;
BEHIND THE SCENES: THE COMPILATION
PROCESS
STEP 1: PREPROCESSOR CONVERTS SOURCE
CODE TO EXPANDED CODE
When you run the program, the source code is first sent to the tool known as Preprocessor. Preprocessors
basically do two things:
• It removes the comments, black lines and white spaces from the program.
• It expands the Preprocessor directives such as macros or file inclusion.
• It finally converts the source code to expanded code.
• Basically, the source code first-program.cpp is converted to first-program.i which contains the expanded code.
STEP 2: COMPILER CONVERTS THE EXPANDED
CODE TO ASSEMBLY CODE.
• In this last step, the Loader loads the executable file into memory and the program starts to
run in an executable environment.
• Assignment Operators
• Arithmetic Operators
• Compound assignment Operators
• variable = value;
• The right hand side is stored in the left hand side.
• Left Hand Side = Right Hand Side
• Exercise
• Swap two variable using third variable
ARITHMETIC OPERATORS
• Write a program to input number of five digits and print every digit separately in new line.
• Write a program that asks user to enter two numbers and print the Sum, Product, Difference, Division,
Modules, Quotient of the two number.
• Write a program in C++ to find the Area and Perimeter of a Rectangle.
• Write a program in C++ to find the area and circumference of a circle.
• (Advanced) Swap two variable without using third variable.
REFRESH YOUR MIND
• Write program that reads an integer and prints the sum of it’s last 3 digits.
• Write Program to print
*
**
***
****
*****
COMPOUND ASSIGNMENT OPERATOR
INCREMENT AND DECREMENT
OPERATORS
if (Boolean expression) {
//code to be executed
}
● If condition is true then execute statements.
● If condition is false then skip statements.
● You can ignore {} if you write only one statement.
EQUALITY OPERATORS
RELATIONAL OPERATORS
ELSE IF
• If else Statement
• If expression is true then execute statement 1
• If expression is false then execute statement 2
11/13/2022 41
EXERCISES
44
INTRODUCTION TO LOGICAL OPERATOR
• Let x = 5 , y = 2 , z = 5
HELP THE EMPLOYEE, ONE LAST TIME...
Given the Employee’s accuracy.
• Two employees gave you their first letter in their names, and you should print the bigger between them.
• We have an employee that we need to create an ID to him, the ID format is “F\MM\YY”;
Input: E 6 1986 Output: E\06\86
NICE TO SEARCH NEXT SESSION
• Nested If
• Ternary operator
• switch case
Advanced
• Write program to sum number from 1 to N without for loop.
• Write Program to find remainder between two numbers without using % operator.