Upload
freslyncababat
View
12
Download
0
Embed Size (px)
Citation preview
Turbo C / C++ Programming Language
By : Erminigildo S. Lampitoc
Brief History
• C language is invented by Dennis Ritchie• C++ is developed by Bjarne Stroustrup in 1979,
at Bell Laboratories• C++ was initially called C with Classes• However, in 1983 the name was change to C++
The Hello World Program
Turbo C#include<stdio.h>#include<conio.h>main(){system(“cls”);printf(“Hello World”);getch();return 0;}
C++#include<iostream.h>#include<conio.h>main(){system(“cls”);cout<<“Hello World”;getch();return 0;}
Program Dissection
#include<stdio.h>#include<conio.h>//hello world programmain() {system(“cls”);printf(“Hello World”);getch();return 0;}
headerheadercommentthe main programterminalfunction to clear the screenfunction to display the stringfunction that will wait for key pressterminate the program normallyterminal
Flowchart Symbols
Terminal Symbol
Decision
Process
Input / Output
Preparation
Off Page Connector
On Page Connector
Flow lines
#include<stdio.h>#include<conio.h>//hello world programmain() {clrscr();printf(“Hello World”);getch();return 0;}
Start
Hello world
End
Syntax : Calling A Header
# include <headername.h>Ex.# include <iostream.h># include <stdio.h># include <math.h># include <conio.h>
Syntax: Output (iostream.h)
cout<<“string constant”;cout<<variable;cout<<“string constant”<<variable;cout<<variable<<“string constant”;Ex.cout<<“hello world”;cout<<sum;cout<<“the sum is ”<<sum;cout<<number<<“ is the total”;
Syntax: Output (stdio.h)
printf(“string constant”);printf(“format string”, variable);printf(“string constant format string”, variable);printf(“format string string constant”, variable);Ex.printf(“hello world”);printf(“%d”,number);printf(“the sum is %f”,sum);printf(“%f is the average”, average);
Format String
%d - denary integer (int or long int) %ld - long decimal integer %x -hexadecimal integer %o - octal integer %h -short integer %f -float type %lf -long float or double %e -float type %le -double %c -single character %s -character string
Escape Characters
\b - backspace BS \f - form feed FF (also clear screen) \n - new line NL (like pressing return) \r - carriage return CR (cursor to start of line) \t - horizontal tab HT \v - vertical tab (not all versions) \" - double quotes (not all versions) \' - single quote character ' \\ - backslash character \
Syntax: Input (iostream.h)
cin>>variable;ex.cin>number;cin>>name;cin>>x;
Syntax: Input (stdio.h)
scanf(“format string”,&variable);Ex.scanf(“%s”,&name);scanf(“%d”,&number);scanf(“%c”,&character);
Syntax: Variable Declaration
datatype variablenameex.int number;float number;char choice;char name[20];Note: Variables are container of data
Data TypesType Length Range
unsigned char 8bits 0 to255
char 8 bits -128 to 127
unsigned int 16 bits 0 to 65,535
short int 16 bits -32,768 to 32,767
Int 16 bits -32,768 to 32,767
unsigned long 32 bits 0 to 4,294,967,295
Long 32 bits -2,147,483,648 to -2,147,483,647
Float 32 bits 1.8E-37 to 34E+37
Double 64 bits 2.2E-308 to 1.8E+308
long double 80 bits 2.2E-308 to 1.8E+308
bool n/a True or false
Arithmetic Operator
+ - addition- - subtraction* - multiplication/ - division++ -increment -- -decrement % -modulo(remainder)
Ex. a=4 b=2a+b = 6a-b = 2a*b = 8a/b = 2a++ = 5a-- = 3a%b = 0
Relational Operator
> - greater than>= - greater than or equal< - less than<= - less than equal== - equal!= -not equal
Logical Operator
&& - And || - Or ! - Not
A B A && B A||B A!
False False False False True
False True False True True
True False False True False
True True True True False
Syntax: if statement
if (condition)statement(s)
Note: for multiple lines of code there must be a {}
ex. if (gender==‘m’)
printf(“boy”);
if (gender==‘f’){
cout<<“girl”;cout<<“woman”;
}
Syntax: if - else statement
if (condition)statement(s)
Else statement(s)
Ex.if (gender==‘f’)
printf(“girl”);Else
printf(“boy”);
Syntax: if - else if – else statement
if (condition)statement(s)
else if (condition)statement(s)
::else if (condition)
statement(s)else
statement(s)
ex.if (gender==‘f’)
cout<<girl;else if (gender==‘m’)
cout<<“boy”;else
cout<<“unknown”;
Syntax: Switch Case Statementswitch(variable) {case possiblevalue1:
statement(s)break;case possiblevalue2:
statement(s)break;::case possiblevalueN:
statement(s)break;default:
statement(s)}
Ex. switch(gender) {case ‘m’:
printf(“male”);break;case ‘f’:
printf(“female”);break;default:
printf(“invalid”);}
#include<conio.h>#include<stdio.h>main(){char gender;system(“cls”);printf(“enter gender:”);scanf(“%c”,&gender);if (gender==‘m’)
printf(“boy”);else if(gender==‘f’)
printf(“girl”);else
printf(“invalid gender”);getch();return 0;}
Gender==‘m’
start
end
enter gender
gender
male
female
invalid
Gender==‘f’
a
a
a
#include<conio.h>#include<stdio.h>main(){char gender;clrscr();printf(“enter gender:”);scanf(“%c”,&gender);switch(gender){
case ‘m’:printf(“boy”);
break;case ‘f’:
printf(“girl”);break;default:
printf(“invalid gender”);}getch();return 0;}
Gender
start
end
enter gender
gender
male female invalid
m f Not in list
Loops
1. For loop2. Do while loop3. While loop
Syntax: for loop
for (initialization ; condition; step)Statement(s);
Initialization = assigning a value to a variableCondition = sets the termination of a loopStep = sets the increase or decrease of the variableEx. For (a=;a<=10;a++){
printf(“\n%d”,a);printf()
}
Syntax: do while loop
do {statement(s)}while(condition);
Ex.do{cout<<a;a++;}while(a<10);
Syntax: while loop
while(condition){Statement(s)}
Ex.while(condition){cout<<a;A++;}
Function -is a subroutine that contains one or more statements
Arguments – is a value that is passed into a function
Parameter –are essentially variables that receive the value of the arguments passed to the function when it is called
Function
Syntax: Function Declaration
Return type function name(parameter list);
Ex .void myfunction();int meter();float object();