Kuliah komputer pemrograman

194
ALGORITMA DAN PEMROGRAMAN KOMPUTER Oleh: Oleh: Hendrianto Husada Hendrianto Husada

Transcript of Kuliah komputer pemrograman

Page 1: Kuliah  komputer pemrograman

ALGORITMA DAN PEMROGRAMAN

KOMPUTER

Oleh:Oleh:

Hendrianto HusadaHendrianto Husada

Page 2: Kuliah  komputer pemrograman

MATERI KULIAH

PENDAHULUANPENDAHULUAN KOMPONEN BAHASA CKOMPONEN BAHASA C OPERATOR BAHASA COPERATOR BAHASA C INPUT /OUTPUTINPUT /OUTPUT PERNYATAAN DASARPERNYATAAN DASAR FUNGSIFUNGSI LARIKLARIK STRINGSTRING POINTERPOINTER

Page 3: Kuliah  komputer pemrograman

DAFTAR PUSTAKA

SAM PUBLISHING, TEACH YOURSELF SAM PUBLISHING, TEACH YOURSELF C IN 21 DAYSC IN 21 DAYS

OXFORD UNIVERSITY COMPUTING OXFORD UNIVERSITY COMPUTING SERIES ,PROGRAMMING IN C SERIES ,PROGRAMMING IN C

BRIAN W.KERNIGHAN AND DENNIS BRIAN W.KERNIGHAN AND DENNIS M. RICHIE ,The C programming M. RICHIE ,The C programming Language, PRENTICE HALL 1988 Language, PRENTICE HALL 1988

Page 4: Kuliah  komputer pemrograman

DAFTAR PUSTAKA

ROB MILES, C PROGRAMMINGROB MILES, C PROGRAMMING

Page 5: Kuliah  komputer pemrograman

SISTEM PENILAIAN

UTSUTS UAS UAS TUGASTUGAS ABSENABSEN

Page 6: Kuliah  komputer pemrograman

PENDAHULUAN

MENGAPA MENGGUNAKAN CMENGAPA MENGGUNAKAN C SALAH SATU BAHASA PEMROGRAMAN GENERASI SALAH SATU BAHASA PEMROGRAMAN GENERASI

KETIGA YANG PALING BANYAK DIGUNAKAN KETIGA YANG PALING BANYAK DIGUNAKAN KEMAMPUANNYA DAN FLEKSIBILITASNYA MEMBUAT KEMAMPUANNYA DAN FLEKSIBILITASNYA MEMBUAT

C MASIH MENJADI PILIHAN UNTUK HAMPIR SEMUA C MASIH MENJADI PILIHAN UNTUK HAMPIR SEMUA BIDANG APLIKASI KHUSUSNYA PADA LINGKUNGAN BIDANG APLIKASI KHUSUSNYA PADA LINGKUNGAN PENGEMBANGAN PERANGKAT LUNAKPENGEMBANGAN PERANGKAT LUNAK

BANYAK APLIKASI DITULIS DALAM C ATAU C++ BANYAK APLIKASI DITULIS DALAM C ATAU C++ TERMASUK COMPILER UNTUK BAHASA TERMASUK COMPILER UNTUK BAHASA PEMROGRAMAN LAINPEMROGRAMAN LAIN

BANYAK SISTEM OPERASI DITULIS DIDALAM BAHASA BANYAK SISTEM OPERASI DITULIS DIDALAM BAHASA C TERMASUK UNIX, DOS DAN WINDOWSC TERMASUK UNIX, DOS DAN WINDOWS

Page 7: Kuliah  komputer pemrograman

MENGAPA C

BERADAPTASI DENGAN PENGGUNAAN BARU DAN YANG BERADAPTASI DENGAN PENGGUNAAN BARU DAN YANG TERAKHIR ADALAH JAVA YANG DIGUNAKAN UNTUK TERAKHIR ADALAH JAVA YANG DIGUNAKAN UNTUK PEMROGRAMAN APLIKASI INTERNE TPEMROGRAMAN APLIKASI INTERNE T

MEMPUNYAI BANYAK KEKUATAN ( KEUNTUNGAN ) MEMPUNYAI BANYAK KEKUATAN ( KEUNTUNGAN ) SEPERTI : FLEKSIBEL DAN PORTABEL ,MENGHASILKAN SEPERTI : FLEKSIBEL DAN PORTABEL ,MENGHASILKAN DENGAN CEPAT, KODENYA COMPACT, BERORIENTASI DENGAN CEPAT, KODENYA COMPACT, BERORIENTASI OBYEK UNTUK MEMBUAT DAN MEMANIPULASI STRUKTUR OBYEK UNTUK MEMBUAT DAN MEMANIPULASI STRUKTUR YANG KOMPLEKS (CLASS DALAM C++),MEMPUNYAI YANG KOMPLEKS (CLASS DALAM C++),MEMPUNYAI RUTIN-RUTIN TINGKAT RENDAH UNTUK MENGONTROL RUTIN-RUTIN TINGKAT RENDAH UNTUK MENGONTROL PERANGKAT KERAS ( CONTOH : PORT INPUT DAN OUTPUT PERANGKAT KERAS ( CONTOH : PORT INPUT DAN OUTPUT DAN SISTEM OPERASI INTERUPSI ),MEMPUNYAI STANDAR DAN SISTEM OPERASI INTERUPSI ),MEMPUNYAI STANDAR INTERNASIONAL ANSI CINTERNASIONAL ANSI C

Page 8: Kuliah  komputer pemrograman

LANGKAH-LANGKAH SUATU PROGRAM C MENJADI EXECUTABLE FILE

Page 9: Kuliah  komputer pemrograman
Page 10: Kuliah  komputer pemrograman

KOMPONEN-KOMPONEN BAHASA C

HEADER FILES ( stdio.h )HEADER FILES ( stdio.h ) PREPROCESSOR DIRECTIVE ( # include )PREPROCESSOR DIRECTIVE ( # include ) FUNGSI UTAMA ( main ( ) )FUNGSI UTAMA ( main ( ) ) KURUNG KURAWAL BUKA DAN TUTUP ( { } )KURUNG KURAWAL BUKA DAN TUTUP ( { } ) VARIABELVARIABEL PERNYATAANPERNYATAAN KOMENTAR KOMENTAR RETURNRETURN

Page 11: Kuliah  komputer pemrograman
Page 12: Kuliah  komputer pemrograman
Page 13: Kuliah  komputer pemrograman

KOMPONEN PROGRAM

Page 14: Kuliah  komputer pemrograman

PENGENAL

DIGUNAKAN SEBAGAI NAMA VARIABEL , FUNGSI , DIGUNAKAN SEBAGAI NAMA VARIABEL , FUNGSI , KOMSTANTAKOMSTANTA

SYARAT-SYARAT PENGENAL :SYARAT-SYARAT PENGENAL : KARAKTER PERTAMA HARUS HURUF ATAU GARIS KARAKTER PERTAMA HARUS HURUF ATAU GARIS

BAWAHBAWAH KARAKTER SELANJUTNYA BISA HURUF,ANGKA ATAU KARAKTER SELANJUTNYA BISA HURUF,ANGKA ATAU

GARIS BAWAHGARIS BAWAH TIDAK BOLEH MENGGUNAKAN KATA KUNCITIDAK BOLEH MENGGUNAKAN KATA KUNCI PANJANG KARAKTER 35 PANJANG KARAKTER 35 UNIK UNIK

Page 15: Kuliah  komputer pemrograman

DEKLARASI & INISIALISASI VARIABEL

Deklarasi variabel : int x,y,z;Deklarasi variabel : int x,y,z; Inisialisasi variabel : int x = 24;Inisialisasi variabel : int x = 24; int i,j; int i,j; char ch; char ch; double x,y,z,fred; double x,y,z,fred; unsigned long int Name_of_Variable; unsigned long int Name_of_Variable;

Page 16: Kuliah  komputer pemrograman

Hello World Program

The source codeThe source code

#include <stdio.h> #include <stdio.h>

int main() int main()

{ {

printf("Hello World\n"); printf("Hello World\n");

return(0); return(0);

}}

Page 17: Kuliah  komputer pemrograman

NameName DescriptionDescription Size*Size* Range*Range*

charchar Character or small Character or small integerinteger

1 byte1 byte signed: -128 to 127signed: -128 to 127unsigned: 0 to 255 unsigned: 0 to 255

short intshort int

(short)(short)

Short integerShort integer 2 bytes2 bytes signed: -32768 to 32767signed: -32768 to 32767unsigned: 0 to 65535 unsigned: 0 to 65535

intint IntegerInteger 4 bytes4 bytes signed: -2147483648 to signed: -2147483648 to 21474836472147483647unsigned: 0 to 4294967295 unsigned: 0 to 4294967295

long intlong int

(long)(long)

Long integerLong integer 4 bytes4 bytes signed: -2147483648 to signed: -2147483648 to 21474836472147483647unsigned: 0 to 4294967295unsigned: 0 to 4294967295

floatfloat Floating point Floating point numbernumber

4 bytes4 bytes 3.4e +/- 38 (7 digits) 3.4e +/- 38 (7 digits)

doubledouble Double precision Double precision floating point numberfloating point number

8 bytes8 bytes 1.7e +/- 308 (15 digits) 1.7e +/- 308 (15 digits)

long long doubledouble

Long double Long double precision floating precision floating point numberpoint number

8 bytes8 bytes 1.7e +/- 308 (15 digits) 1.7e +/- 308 (15 digits)

Data types

Page 18: Kuliah  komputer pemrograman

Local variabelLocal variabel

Local variabel digunakan didalam suatu fungsi , dan hanya Local variabel digunakan didalam suatu fungsi , dan hanya digunakan didalam fungsi tersebut .digunakan didalam fungsi tersebut .

Static variabelStatic variabel

Ditentukan dengan suatu keyword Ditentukan dengan suatu keyword staticstatic pada deklarasi variabel. pada deklarasi variabel.

Perbedaan dari non-static local variable adalah variabel static Perbedaan dari non-static local variable adalah variabel static tidak bisa dihilangkan dengan keluar dari fungsi. tidak bisa dihilangkan dengan keluar dari fungsi.

Global variabel Global variabel

Suatu deklarasi global variable kelihatannya normal, tetapi terletak Suatu deklarasi global variable kelihatannya normal, tetapi terletak diluar suatu fungsi program . Oleh karena itu ia bisa di akses oleh diluar suatu fungsi program . Oleh karena itu ia bisa di akses oleh semua fungsi .semua fungsi .

Variable types

Page 19: Kuliah  komputer pemrograman

OPERATOR-OPERATOR C

ARITMATIKAARITMATIKA INCREMENT/ DECREMENTINCREMENT/ DECREMENT RELASIRELASI LOGIKALOGIKA BITWISEBITWISE PENUGASANPENUGASAN LAINNYALAINNYA

Page 20: Kuliah  komputer pemrograman

OPERATOR ARITMATIK

Page 21: Kuliah  komputer pemrograman

OPERATOR PENUGASAN

Page 22: Kuliah  komputer pemrograman

Increment and Decrement Operators

awkwardawkward easyeasy easiesteasiest

x = x+1;x = x+1; x += 1x += 1 x++x++

x = x-1;x = x-1; x -= 1x -= 1 x--x--

Page 23: Kuliah  komputer pemrograman

INC/DEC

PRE INCREMENT ( ++ X)PRE INCREMENT ( ++ X) POST INCREMENT ( X++)POST INCREMENT ( X++) PRE DECREMENT ( --X )PRE DECREMENT ( --X ) POST DECREMENT ( X--)POST DECREMENT ( X--)

Page 24: Kuliah  komputer pemrograman

Example

Arithmetic operatorsArithmetic operatorsint i = 10;int i = 10;int j = 15;int j = 15;int add = i + j;int add = i + j; //25 //25int diff = j – i;int diff = j – i; //5 //5int product = i * j;int product = i * j; // 150 // 150int quotient = j / i;int quotient = j / i; // 1 // 1iint residual = j % nt residual = j % i; // 5i; // 5i++;i++; //Increase by 1//Increase by 1i--;i--; //Decrease by 1//Decrease by 1

Page 25: Kuliah  komputer pemrograman

Comparing themComparing them

int i = 10;int i = 10;

int j = 15;int j = 15;

float k = 15.0;float k = 15.0;

j / i = ?j / i = ?

j % i = ?j % i = ?

k / i = ?k / i = ?

k % i = ? k % i = ?

Page 26: Kuliah  komputer pemrograman

The AnswerThe Answer

j /j / i i = 1; = 1;

j % i = 5;j % i = 5;

k / i k / i = 1.5;= 1.5;

k % i It is k % i It is illegalillegal..

Note: For %, the operands can only be integers.Note: For %, the operands can only be integers.

Page 27: Kuliah  komputer pemrograman
Page 28: Kuliah  komputer pemrograman
Page 29: Kuliah  komputer pemrograman

The AnswerThe Answer

j /j / i i = 1; = 1;

j % i = 5;j % i = 5;

k / i k / i = 1.5;= 1.5;

k % i It is k % i It is illegalillegal..

Note: For %, the operands can only be integers.Note: For %, the operands can only be integers.

Page 30: Kuliah  komputer pemrograman

OPERATOR LOGIKA & RELASI What is “true” and “false” in CWhat is “true” and “false” in C

In C, there is no specific data type to represent “true” and “false”. C In C, there is no specific data type to represent “true” and “false”. C uses value “0” to represent “false”, and uses non-zero value to stand uses value “0” to represent “false”, and uses non-zero value to stand for “true”. for “true”.

Logical OperatorsLogical Operators

A && BA && B =>=> A and BA and B

A || BA || B => => A or BA or B

A == BA == B =>=> Is A equal to B?Is A equal to B?

A != BA != B => Is A not equal to B?=> Is A not equal to B?

Page 31: Kuliah  komputer pemrograman
Page 32: Kuliah  komputer pemrograman

A > BA > B =>=> Is A greater than B?Is A greater than B?

A >= B A >= B => Is A greater than or equal to B?=> Is A greater than or equal to B?

A < BA < B =>=> Is A less than B?Is A less than B?

A <= B A <= B => Is A less than or equal to B?=> Is A less than or equal to B?

Don’t be confusedDon’t be confused

&& and || have different meanings from & and |.&& and || have different meanings from & and |.

& and | are & and | are bitwisebitwise operators. operators.

Page 33: Kuliah  komputer pemrograman

OPERATOR RELASI

Page 34: Kuliah  komputer pemrograman
Page 35: Kuliah  komputer pemrograman
Page 36: Kuliah  komputer pemrograman
Page 37: Kuliah  komputer pemrograman

int i = 10; int j = 15; int k = 15; int m = 0;int i = 10; int j = 15; int k = 15; int m = 0;

if( i < j && j < k) =>if( i < j && j < k) =>

if( i != j || k < j) =>if( i != j || k < j) =>

if( j<= k || i > k) =>if( j<= k || i > k) =>

if( j == k && m) =>if( j == k && m) =>

if(i)if(i) => =>

if(m || j && i )if(m || j && i ) => =>

Page 38: Kuliah  komputer pemrograman

int i = 10; int j = 15; int k = 15; int m = 0;int i = 10; int j = 15; int k = 15; int m = 0;

if( i < j && j < k) => if( i < j && j < k) => falsefalse

if( i != j || k < j) => if( i != j || k < j) => truetrue

if( j<= k || i > k) => if( j<= k || i > k) => truetrue

if( j == k && m) => if( j == k && m) => falsefalse

if(i)if(i) => => truetrue

if(m || j && i )if(m || j && i ) => => truetrue

Did you get the correct answers? Did you get the correct answers?

Page 39: Kuliah  komputer pemrograman

OPERATOR BITWISE

Page 40: Kuliah  komputer pemrograman

OPERATOR BITWISE

GESER KANANGESER KANAN GESER KIRIGESER KIRI OPERATOR KOMPLEMEN (~)OPERATOR KOMPLEMEN (~)

Page 41: Kuliah  komputer pemrograman
Page 42: Kuliah  komputer pemrograman
Page 43: Kuliah  komputer pemrograman
Page 44: Kuliah  komputer pemrograman
Page 45: Kuliah  komputer pemrograman

INPUT/OUTPUT

PRINTFPRINTF SCANFSCANF PUTSPUTS

Page 46: Kuliah  komputer pemrograman

printf()

Fungsi printf() dapat diperintahkan untuk mencetak Fungsi printf() dapat diperintahkan untuk mencetak integers, floats and string . integers, floats and string .

syntax yang umum adalah syntax yang umum adalah printfprintf( “format”, variabel);( “format”, variabel);

Contoh :Contoh :intint stud_id = 5200; stud_id = 5200;char * name = “Mike”;char * name = “Mike”;printfprintf(“(“%s%s ‘s ID is ‘s ID is %d%d \n”, name, stud_id); \n”, name, stud_id);

Page 47: Kuliah  komputer pemrograman
Page 48: Kuliah  komputer pemrograman
Page 49: Kuliah  komputer pemrograman
Page 50: Kuliah  komputer pemrograman

Why “\n”Why “\n”

It introduces a new line on the terminal screen.It introduces a new line on the terminal screen.

\a\a alert (bell) character alert (bell) character \\\\ backslash backslash

\b\b backspace backspace \?\? question mark question mark

\f\f formfeedformfeed \’\’ single quote single quote

\n\n newlinenewline \”\” double quote double quote

\r\r carriage returncarriage return \000\000 octal number octal number

\t\t horizontal tab horizontal tab \xhh\xhh hexadecimal number hexadecimal number

\v\v vertical tab vertical tab

escape sequence

Page 51: Kuliah  komputer pemrograman

Format IdentifiersFormat Identifiers

%d %d decimal integersdecimal integers

%x%x hex integerhex integer

%c %c charactercharacter

%f%f float and double numberfloat and double number

%s%s stringstring

%p%p pointerpointer

How to specify display space for a variableHow to specify display space for a variable??

printf(“The student id is %printf(“The student id is %55d \n”, stud_id); d \n”, stud_id);

The value of stud_id will occupy The value of stud_id will occupy 55 characters space in the characters space in the print-out.print-out.

Page 52: Kuliah  komputer pemrograman

FUNGSI PUTS ( )

Page 53: Kuliah  komputer pemrograman

FUNGSI PUT ( )

Page 54: Kuliah  komputer pemrograman
Page 55: Kuliah  komputer pemrograman

PERNYATAAN DASAR

PERNYATAAN BERSYARATPERNYATAAN BERSYARAT PERNYATAAN PENGULANGANPERNYATAAN PENGULANGAN PERNYATAAN BREAK DAN PERNYATAAN BREAK DAN

CONTINUECONTINUE PERNYATAAN NOLPERNYATAAN NOL PERNYATAAN MAJEMUKPERNYATAAN MAJEMUK

Page 56: Kuliah  komputer pemrograman

PERNYATAAN BERSYARAT

PERNYATAAN IFPERNYATAAN IF PERNYATAAN IF ELSEPERNYATAAN IF ELSE PERNYATAAN SWITCHPERNYATAAN SWITCH PERNYATAAN IF /IF ELSE PERNYATAAN IF /IF ELSE

BERSARANGBERSARANG

Page 57: Kuliah  komputer pemrograman

PERNYATAAN BERSYARAT

Page 58: Kuliah  komputer pemrograman

PERNYATAAN IF & IF - ELSE

IFIFifif ( (expressionexpression){ ){

statement …statement …}}

IF ELSEIF ELSEifif ( (expressionexpression) {) {

statement …statement … }}elseelse{ {

statement …statement … } }

Page 59: Kuliah  komputer pemrograman

IF

Page 60: Kuliah  komputer pemrograman

IF ELSE

Page 61: Kuliah  komputer pemrograman

CONTOH

Page 62: Kuliah  komputer pemrograman

PERNYATAAN IF ELSE BERSARANG

if (if (expressionexpression) {) {

statement…statement…

}} else ifelse if ( (expressionexpression) {) {

statement…statement…

}} else{else{

statement…statement…

}}

Page 63: Kuliah  komputer pemrograman

An exampleAn example

if(score >= 90){if(score >= 90){

a_cnt ++;a_cnt ++;

}else if(score >= 80){}else if(score >= 80){

b_cnt++;b_cnt++;

}else if(score >= 70){}else if(score >= 70){

c_cnt++;c_cnt++;

}else if (score>= 60){}else if (score>= 60){

d_cnt++d_cnt++

}else{}else{

f_cnt++f_cnt++

}}

Page 64: Kuliah  komputer pemrograman

IF/IF ELSE BERSARANG

Page 65: Kuliah  komputer pemrograman

PERNYATAAN SWITCH The switch statementThe switch statement

switch (switch (expressionexpression) ) { {

case case item1item1: : statementstatement; ; break; break;

case case item2item2: : statementstatement; ; break; break;

default: default: statementstatement; ; break; break;

} }

Page 66: Kuliah  komputer pemrograman

CONTOH

Page 67: Kuliah  komputer pemrograman

NESTED SWITCH

Page 68: Kuliah  komputer pemrograman

PERNYATAAN PENGULANGAN PERNYATAAN FORPERNYATAAN FOR PERNYATAAN WHILEPERNYATAAN WHILE PERNYATAAN DO WHILEPERNYATAAN DO WHILE PERNYATAAN PENGULANGAN PERNYATAAN PENGULANGAN

BERSARANG ( NESTED LOOP )BERSARANG ( NESTED LOOP ) PERNYATAAN PENGULANGAN TAK PERNYATAAN PENGULANGAN TAK

HENTIHENTI

Page 69: Kuliah  komputer pemrograman
Page 70: Kuliah  komputer pemrograman

PERNYATAAN PENGULANGAN FOR for statementfor statement

for (for (expression1expression1; ; expression2expression2; ; expression3expression3)){{

statement…statement…

}}

expression1expression1 initializes; initializes;

expression2expression2 is the terminate test; is the terminate test;

expression3expression3 is the modifier is the modifier;;

Page 71: Kuliah  komputer pemrograman

DIAGRAM ALUR PERNYATAAN FOR

Page 72: Kuliah  komputer pemrograman

CONTOH

Page 73: Kuliah  komputer pemrograman
Page 74: Kuliah  komputer pemrograman

An exampleAn example

int x;int x;

for (x=0; x<3; x++) for (x=0; x<3; x++)

{ {

printf("x=%dprintf("x=%d\\n",x); n",x);

} }

First time: First time: x = 0;x = 0;

Second time:Second time: x = 1;x = 1;

Third time: Third time: x = 2;x = 2;

Fourth time:Fourth time: x = 3; (donx = 3; (don’’t execute the body)t execute the body)

Page 75: Kuliah  komputer pemrograman
Page 76: Kuliah  komputer pemrograman

PERNYATAAN WHILE

Page 77: Kuliah  komputer pemrograman
Page 78: Kuliah  komputer pemrograman
Page 79: Kuliah  komputer pemrograman
Page 80: Kuliah  komputer pemrograman
Page 81: Kuliah  komputer pemrograman
Page 82: Kuliah  komputer pemrograman

The while statementThe while statementwhile (while (expressionexpression) {) {

statementstatement … …}}while loop exits only when the expression is while loop exits only when the expression is false. false.

An exampleAn exampleint x = 3; int x = 3; while (x>0) { while (x>0) {

printf("x=%d n",x); printf("x=%d n",x); x--; x--;

} }

Page 83: Kuliah  komputer pemrograman
Page 84: Kuliah  komputer pemrograman
Page 85: Kuliah  komputer pemrograman
Page 86: Kuliah  komputer pemrograman
Page 87: Kuliah  komputer pemrograman

for <==> while

for (for (expression1expression1; ; expression2expression2; ; expression3expression3)){{

statement…statement…

}}

expression1;expression1;

while (expression2)while (expression2)

{{

statementstatement……;;

expression3;expression3;

}}

equals

Page 88: Kuliah  komputer pemrograman

DO WHILE

Page 89: Kuliah  komputer pemrograman
Page 90: Kuliah  komputer pemrograman

Do while

Page 91: Kuliah  komputer pemrograman
Page 92: Kuliah  komputer pemrograman
Page 93: Kuliah  komputer pemrograman
Page 94: Kuliah  komputer pemrograman

PERNYATAAN PENGULANGAN TAK HENTI 1.1. for ( ; ; ) for ( ; ; ) {{ Pernyataan ;Pernyataan ; } }

2.2. while ( 1 )while ( 1 ) {{ Pernyataan;Pernyataan; }}

Page 95: Kuliah  komputer pemrograman

33 dodo {{ Pernyataan;Pernyataan; } while (1 ) ;} while (1 ) ;

Page 96: Kuliah  komputer pemrograman

PERNYATAAN PENGULANGAN BERSARANG

Page 97: Kuliah  komputer pemrograman
Page 98: Kuliah  komputer pemrograman
Page 99: Kuliah  komputer pemrograman

NESTED FOR

Page 100: Kuliah  komputer pemrograman

PERNYATAAN FOR BERSARANG

Page 101: Kuliah  komputer pemrograman

PERNYATAAN BREAK

PERNYATAN YANG DIGUNAKAN PERNYATAN YANG DIGUNAKAN UNTUK KELUAR DARI SUATU UNTUK KELUAR DARI SUATU PERNYATAAN PENGULANGAN PERNYATAAN PENGULANGAN

PERNYATAAN YANG DIGUNAKAN PERNYATAAN YANG DIGUNAKAN DIDALAM SWITCH UNTUK DIDALAM SWITCH UNTUK MENGAKHIRI SUATU PILIHANMENGAKHIRI SUATU PILIHAN

Page 102: Kuliah  komputer pemrograman
Page 103: Kuliah  komputer pemrograman

PERNYATAAN BREAK

Page 104: Kuliah  komputer pemrograman

PERNYATAAN BREAK

Page 105: Kuliah  komputer pemrograman

PERNYATAAN BREAK

Page 106: Kuliah  komputer pemrograman
Page 107: Kuliah  komputer pemrograman

PERNYATAAN BREAK

Page 108: Kuliah  komputer pemrograman

PERNYATAN CONTINUE

PERNYATAAN YANG DIGUNAKAN PERNYATAAN YANG DIGUNAKAN UNTUK MELEWATKAN ( SKIP ) UNTUK MELEWATKAN ( SKIP ) SUATU ITERASI DAN SUATU ITERASI DAN MELANJUTKAN KE ITERASI MELANJUTKAN KE ITERASI SELANJUTNYASELANJUTNYA

Page 109: Kuliah  komputer pemrograman
Page 110: Kuliah  komputer pemrograman

PERNYATAAN CONTINUE

Page 111: Kuliah  komputer pemrograman

PERNYATAAN CONTINUE

Page 112: Kuliah  komputer pemrograman
Page 113: Kuliah  komputer pemrograman

PERNYATAN GOTO

PERNYATAAN UNTUK MELOMPAT PERNYATAAN UNTUK MELOMPAT KE SUATU SUB PROGRAM TETAPI KE SUATU SUB PROGRAM TETAPI TIDAK DIREKOMEN UNTUK TIDAK DIREKOMEN UNTUK MENGGUNAKAN GOTO MENGGUNAKAN GOTO

Page 114: Kuliah  komputer pemrograman
Page 115: Kuliah  komputer pemrograman
Page 116: Kuliah  komputer pemrograman
Page 117: Kuliah  komputer pemrograman
Page 118: Kuliah  komputer pemrograman

PERNYATAAN NULL

Jika anda meletakkan tanda titik koma pada Jika anda meletakkan tanda titik koma pada suatu baris , anda membuat suatu suatu baris , anda membuat suatu pernyataan null pernyataan null

yaitu suatu pernyataan yang tidak yaitu suatu pernyataan yang tidak melakukan suatu aksi apapun. melakukan suatu aksi apapun.

SyntaxSyntax :: ;;

Page 119: Kuliah  komputer pemrograman

PERNYATAAN MAJEMUK

Suatu pernyataan majemuk disebut juga Suatu pernyataan majemuk disebut juga block, block, block adalah sekelompok dua atau block adalah sekelompok dua atau lebih lebih

pernyataan diantara dua kurung pernyataan diantara dua kurung kurawal.kurawal.

ContohContoh ::

Page 120: Kuliah  komputer pemrograman

CONTOH PROGRAM #include <stdio.h>#include <stdio.h> /* print Fahrenheit-Celsius table *//* print Fahrenheit-Celsius table */ main()main() {{ int fahr;int fahr; for (fahr = 0; fahr <= 300; fahr = fahr + 20)for (fahr = 0; fahr <= 300; fahr = fahr + 20) { printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));{ printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); }} Return 0 ;Return 0 ; }}

Page 121: Kuliah  komputer pemrograman

if (n > 0)if (n > 0) for (i = 0; i < n; i++)for (i = 0; i < n; i++) if (s[i] > 0) {if (s[i] > 0) { printf("...");printf("..."); return i;return i; }} else /* WRONG */else /* WRONG */ printf("error -- n is negative\n");printf("error -- n is negative\n");

Page 122: Kuliah  komputer pemrograman

for (x = 0; x < 100, x++) ;for (x = 0; x < 100, x++) ; for (ctr = 2; ctr < 10; ctr += 3) ;for (ctr = 2; ctr < 10; ctr += 3) ; for (x = 0; x < 10; x++)for (x = 0; x < 10; x++)

for (y = 5; y > 0; y--)for (y = 5; y > 0; y--)

puts("X");puts("X");

Page 123: Kuliah  komputer pemrograman

record = 0;record = 0; while (record < 100)while (record < 100) {{ printf( "\nRecord %d ", record );printf( "\nRecord %d ", record ); printf( "\nGetting next number..." );printf( "\nGetting next number..." ); }}

Page 124: Kuliah  komputer pemrograman

CONTOH

record = 0;record = 0;

while (record < 100)while (record < 100)

{{

printf( "\nRecord %d ", record );printf( "\nRecord %d ", record );

printf( "\nGetting next number..." );printf( "\nGetting next number..." );

}}

Page 125: Kuliah  komputer pemrograman

CONTOH

Page 126: Kuliah  komputer pemrograman

KATA KUNCI \ KEY WORD

Page 127: Kuliah  komputer pemrograman

FUNGSI

FUNGSI ADALAH SEKELOMPOK FUNGSI ADALAH SEKELOMPOK PERNYATAAN PERNYATAAN YANG PERNYATAAN PERNYATAAN YANG BERSAMA-SAMA MELAKUKAN TUGASBERSAMA-SAMA MELAKUKAN TUGAS

FUNGSI DISEBUT JUGA SUBRUTIN FUNGSI DISEBUT JUGA SUBRUTIN /SUBPROGRAM/SUBPROGRAM

CIRI-CIRI FUNGSI:MEMPUNYAI NAMA, CIRI-CIRI FUNGSI:MEMPUNYAI NAMA, INDEPENDEN,MELAKUKAN SUATU TUGAS INDEPENDEN,MELAKUKAN SUATU TUGAS KHUSUS,DAPAT MENGEMBALIKAN SUATU KHUSUS,DAPAT MENGEMBALIKAN SUATU NILAI KE PROGRAM PEMANGGILNYANILAI KE PROGRAM PEMANGGILNYA

Page 128: Kuliah  komputer pemrograman
Page 129: Kuliah  komputer pemrograman

FUNGSI

BUILT IN FUNCTIONBUILT IN FUNCTION USER DEFINED FUNCTIONUSER DEFINED FUNCTION

Page 130: Kuliah  komputer pemrograman

BUILT IN FUNCTION

PRINTF , SCANFPRINTF , SCANF COS,SIN ,SQRT : MATH.HCOS,SIN ,SQRT : MATH.H

Page 131: Kuliah  komputer pemrograman

USER DEFINED FUNCTION

FUNGSI LUAS LINGKARANFUNGSI LUAS LINGKARAN FUNGSI VOLUME BOLA FUNGSI VOLUME BOLA FUNGSI KONVERSI SUHU FUNGSI KONVERSI SUHU

Page 132: Kuliah  komputer pemrograman

LANGKAH-LANGKAH PENGGUNAAN FUNGSI DEKLARASI FUNGSIDEKLARASI FUNGSI PEMANGGILAN FUNGSI PEMANGGILAN FUNGSI DEFINISI FUNGSIDEFINISI FUNGSI

Page 133: Kuliah  komputer pemrograman

DEKLARASI FUNGSI

DISEBUT JUGA FUNGSI PROTOTIPEDISEBUT JUGA FUNGSI PROTOTIPE SYNTAKS:SYNTAKS:

tipe_return nama_fungsi ( daftar tipe_return nama_fungsi ( daftar parameter)parameter)

contoh:contoh:

int kubus(s);int kubus(s);

Page 134: Kuliah  komputer pemrograman
Page 135: Kuliah  komputer pemrograman

DEFINISI FUNGSI

Page 136: Kuliah  komputer pemrograman

PEMANGGILAN FUNGSI

Page 137: Kuliah  komputer pemrograman
Page 138: Kuliah  komputer pemrograman
Page 139: Kuliah  komputer pemrograman
Page 140: Kuliah  komputer pemrograman
Page 141: Kuliah  komputer pemrograman
Page 142: Kuliah  komputer pemrograman
Page 143: Kuliah  komputer pemrograman
Page 144: Kuliah  komputer pemrograman

ARRAY/LARIK

LARIK\ARRAY ADALAH SEKUMPULAN LARIK\ARRAY ADALAH SEKUMPULAN VARIABEL YANG MEMPUNYAI NAMA SAMA VARIABEL YANG MEMPUNYAI NAMA SAMA TETAPI INDEKSNYA BERBEDATETAPI INDEKSNYA BERBEDA

SETIAP VARIABEL YANG TERDAPAT DIDALAM SETIAP VARIABEL YANG TERDAPAT DIDALAM SUATU LARIK\ARRAY DISEBUT ELEMEN DARI SUATU LARIK\ARRAY DISEBUT ELEMEN DARI LARIK TERSEBUTLARIK TERSEBUT

JENIS LARIK\ARRAY DIDALAM C TERBAGI JENIS LARIK\ARRAY DIDALAM C TERBAGI ATAS : DIMENSI SATU, DIMENSI DUA , DIMENSI ATAS : DIMENSI SATU, DIMENSI DUA , DIMENSI TIGATIGA

Page 145: Kuliah  komputer pemrograman

DEKLARASI LARIK

Page 146: Kuliah  komputer pemrograman

INISIALISASI LARIK\ARRAY

Page 147: Kuliah  komputer pemrograman

MENGAKSES ELEMEN SUATU LARIK\ARRAY SUATU ELEMEN LARIK DAPAT SUATU ELEMEN LARIK DAPAT

DIAKSES DENGAN MEMBERIKAN DIAKSES DENGAN MEMBERIKAN INDEKS ELEMEN SETELAH NAMA INDEKS ELEMEN SETELAH NAMA LARIK\ARRAYLARIK\ARRAY

double salary = balance [ 9 ] ;double salary = balance [ 9 ] ;

Page 148: Kuliah  komputer pemrograman

Contoh

Page 149: Kuliah  komputer pemrograman

Output :

Page 150: Kuliah  komputer pemrograman
Page 151: Kuliah  komputer pemrograman

OUTPUT

Page 152: Kuliah  komputer pemrograman
Page 153: Kuliah  komputer pemrograman
Page 154: Kuliah  komputer pemrograman

LARIK BERDIMENSI DUA

Page 155: Kuliah  komputer pemrograman

INISIALISASI LARIK BERDIMENSI DUA

Page 156: Kuliah  komputer pemrograman
Page 157: Kuliah  komputer pemrograman
Page 158: Kuliah  komputer pemrograman

OUTPUT

Page 159: Kuliah  komputer pemrograman
Page 160: Kuliah  komputer pemrograman
Page 161: Kuliah  komputer pemrograman
Page 162: Kuliah  komputer pemrograman

POINTER

POINTER ADALAH SUATU VARIABEL POINTER ADALAH SUATU VARIABEL YANG MENYIMPAN ALAMAT YANG MENYIMPAN ALAMAT VARIABEL LAINVARIABEL LAIN

Page 163: Kuliah  komputer pemrograman

DEKLARASI POINTER

Page 164: Kuliah  komputer pemrograman
Page 165: Kuliah  komputer pemrograman
Page 166: Kuliah  komputer pemrograman

OPERASI-OPERASI DALAM MENGGUNAKAN POINTER MENDEFINISIKAN VARIABEL POINTERMENDEFINISIKAN VARIABEL POINTER MEMBERIKAN ALAMAT SUATU MEMBERIKAN ALAMAT SUATU

VARIABEL PADA POINTERVARIABEL PADA POINTER MENGAKSES NILAI DARI ALAMAT MENGAKSES NILAI DARI ALAMAT

VARIABEL YANG TERDAPAT PADA VARIABEL YANG TERDAPAT PADA VARIABEL POINTERVARIABEL POINTER

DILAKSANAKAN DENGAN OPERATOR *DILAKSANAKAN DENGAN OPERATOR *

Page 167: Kuliah  komputer pemrograman

INISIALISASI POINTER

Page 168: Kuliah  komputer pemrograman
Page 169: Kuliah  komputer pemrograman

CONTOH

Page 170: Kuliah  komputer pemrograman

OUTPUT

Page 171: Kuliah  komputer pemrograman

CONTOH

Page 172: Kuliah  komputer pemrograman

POINTER DAN LARIK

Page 173: Kuliah  komputer pemrograman

POINTER ARITMATIK

++++ ---- ++ --

Page 174: Kuliah  komputer pemrograman
Page 175: Kuliah  komputer pemrograman
Page 176: Kuliah  komputer pemrograman

STRING

STRING ADALAH SEKUMPULAN STRING ADALAH SEKUMPULAN KARAKTER YANG DIAKHIRI KARAKTER YANG DIAKHIRI DENGAN KARAKTER NULLDENGAN KARAKTER NULL

Page 177: Kuliah  komputer pemrograman

DEKLARASI DAN INISIALISASI STRING

Page 178: Kuliah  komputer pemrograman

CONTOH

Page 179: Kuliah  komputer pemrograman

MEMANIPULASI STRING

MENENTUKAN PANJANG SUATU MENENTUKAN PANJANG SUATU STRINGSTRING

MENGCOPY DAN MENGGABUNGKAN MENGCOPY DAN MENGGABUNGKAN STRINGSTRING

MEMBANDINGKAN STRINGMEMBANDINGKAN STRING MENCARI STRINGMENCARI STRING MENGUBAH STRINGMENGUBAH STRING MENGUJI KARAKTERMENGUJI KARAKTER

Page 180: Kuliah  komputer pemrograman
Page 181: Kuliah  komputer pemrograman
Page 182: Kuliah  komputer pemrograman
Page 183: Kuliah  komputer pemrograman
Page 184: Kuliah  komputer pemrograman
Page 185: Kuliah  komputer pemrograman
Page 186: Kuliah  komputer pemrograman
Page 187: Kuliah  komputer pemrograman
Page 188: Kuliah  komputer pemrograman
Page 189: Kuliah  komputer pemrograman
Page 190: Kuliah  komputer pemrograman
Page 191: Kuliah  komputer pemrograman
Page 192: Kuliah  komputer pemrograman

Books recommended

The C Programming LanguageThe C Programming Language, Brian Kernighan , Brian Kernighan and Dennis Ritchie. Second edition. Prentice-Hall, and Dennis Ritchie. Second edition. Prentice-Hall, 1988. 1988. (C Bible)(C Bible)

The C++ Programming LanguageThe C++ Programming Language, Bjarne , Bjarne Stroustrup. Third edition. Addison-Wesley, 1997. Stroustrup. Third edition. Addison-Wesley, 1997. (C++ Bible)(C++ Bible)

Advanced Programming in the UNIX Advanced Programming in the UNIX EnvironmentEnvironment,, W. Richard StevensW. Richard Stevens,, Addison- Addison-Wesley, 1992Wesley, 1992.. (APUE)(APUE)

Page 193: Kuliah  komputer pemrograman

DAFTAR PUSTAKA

Page 194: Kuliah  komputer pemrograman

TERIMA KASIH