30
Turbo C / C++ Programming Language By : Erminigildo S. Lampitoc

Turbo C

Embed Size (px)

Citation preview

Page 1: Turbo C

Turbo C / C++ Programming Language

By : Erminigildo S. Lampitoc

Page 2: Turbo C

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++

Page 3: Turbo 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;}

Page 4: Turbo C

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

Page 5: Turbo C

Flowchart Symbols

Terminal Symbol

Decision

Process

Input / Output

Preparation

Off Page Connector

On Page Connector

Flow lines

Page 6: Turbo C

#include<stdio.h>#include<conio.h>//hello world programmain() {clrscr();printf(“Hello World”);getch();return 0;}

Start

Hello world

End

Page 7: Turbo C

Syntax : Calling A Header

# include <headername.h>Ex.# include <iostream.h># include <stdio.h># include <math.h># include <conio.h>

Page 8: Turbo C

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”;

Page 9: Turbo C

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);

Page 10: Turbo C

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

Page 11: Turbo C

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 \

Page 12: Turbo C

Syntax: Input (iostream.h)

cin>>variable;ex.cin>number;cin>>name;cin>>x;

Page 13: Turbo C

Syntax: Input (stdio.h)

scanf(“format string”,&variable);Ex.scanf(“%s”,&name);scanf(“%d”,&number);scanf(“%c”,&character);

Page 14: Turbo C

Syntax: Variable Declaration

datatype variablenameex.int number;float number;char choice;char name[20];Note: Variables are container of data

Page 15: Turbo C

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

Page 16: Turbo C

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

Page 17: Turbo C

Relational Operator

> - greater than>= - greater than or equal< - less than<= - less than equal== - equal!= -not equal

Page 18: Turbo C

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

Page 19: Turbo C

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”;

}

Page 20: Turbo C

Syntax: if - else statement

if (condition)statement(s)

Else statement(s)

Ex.if (gender==‘f’)

printf(“girl”);Else

printf(“boy”);

Page 21: Turbo C

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”;

Page 22: Turbo C

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”);}

Page 23: Turbo C

#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

Page 24: Turbo C

#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

Page 25: Turbo C

Loops

1. For loop2. Do while loop3. While loop

Page 26: Turbo C

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()

}

Page 27: Turbo C

Syntax: do while loop

do {statement(s)}while(condition);

Ex.do{cout<<a;a++;}while(a<10);

Page 28: Turbo C

Syntax: while loop

while(condition){Statement(s)}

Ex.while(condition){cout<<a;A++;}

Page 29: Turbo C

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

Page 30: Turbo C

Syntax: Function Declaration

Return type function name(parameter list);

Ex .void myfunction();int meter();float object();