Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Pengantar Pemrograman Fakultas Teknik Sipil & Lingkungan Materi: Variabel, type dan konstanta Ekspresi & assignment.

Presentasi serupa


Presentasi berjudul: "Pengantar Pemrograman Fakultas Teknik Sipil & Lingkungan Materi: Variabel, type dan konstanta Ekspresi & assignment."— Transcript presentasi:

1 Pengantar Pemrograman Fakultas Teknik Sipil & Lingkungan Materi: Variabel, type dan konstanta Ekspresi & assignment

2 JENIS JENIS DATA  Beberapa jenis data dasar:  Karakter: char  Bilangan bulat, integer: int long int: bilangan bulat dengan range lebih besar dari int  Bilangan riil: float double: bilangan riil dengan presisi dan range lebih besar dari float

3 KONSTANTA  Yang paling sederhana adalah angka bilangan bulat seperti: 0, 1, 2, 123.  Konstanta dengan titik desimal atau dengan huruf e (atau keduanya) adalah konstanta bilangan riil, seperti: 3.14, 10.,.01, 123e4, 123.456e7.  Huruf e menyatakan perkalian dengan pangkat 10. Contoh:  123.456e7 adalah 123.456 * 10 pangkat 7 = 1,234,560,000. (konstanta bilangan riil berjenis dobel)

4 KONSTANTA KARAKTER DAN STRING  Konstanta karakter: memakai tanda petik tunggal 'A', '.', '%'.  Nilai numerik konstanta karakter adalah nilai karakter tersebut sesuai (dalam ASCII, contohnya, 'A' mempunyai nilai 65.)  String adalah array karakter (detail nanti). Konstatnta string memakai tanda petik ganda. "apple", "hello, world", "this is a test".  Arti karakter \ didalam konstanta karakter atau string:  \n a ``newline'' character  \b a backspace  \r a carriage return (without a line feed)  \' a single quote (e.g. in a character constant)  \" a double quote (e.g. in a string constant)  \\ a single backslash  Contoh: "he said \"hi\""

5 DEKLARASI  variabel (objek) dipakai untuk menyimpan suatu nilai  Variabel harus dideklarasikan sebelum dipakai  Deklarasi menyatakan nama dan jenis variabel char c; int i; float f; int i1, i2; /* boleh beberapa variabel dalam satu baris */  Deklarasi boleh diikuti pemberian harga awal int i = 1; int i1 = 10, i2 = 20;

6 ALASAN DEKLARASI  It makes things somewhat easier on the compiler; it knows right away what kind of storage to allocate and what code to emit to store and manipulate each variable; it doesn't have to try to intuit the programmer's intentions.  It forces a bit of useful discipline on the programmer: you cannot introduce variables willy- nilly; you must think about them enough to pick appropriate types for them. (The compiler's error messages to you, telling you that you apparently forgot to declare a variable, are as often helpful as they are a nuisance: they're helpful when they tell you that you misspelled a variable, or forgot to think about exactly how you were going to use it.)

7 NAMA VARIABEL  Nama variabel terdiri dari huruf, angka dan garis bawah.  Untuk kita, nama harus dimulai dengan huruf  Nama boleh sangat panjang  Huruf besar dan kecil dibedakan  Dodol, DoDol dan dodol adalah tiga variabel yang berbeda  Nama variabel tidak boleh sama dengan kata-kata kunci (keywords)  Int, if, for dll. Tidak boleh dipakai sebagai nama variabel

8 OPERATOR ARITMATIKA + addition - subtraction * multiplication / division % modulus (remainder) ++increment --decrement

9 OPERATOR ARITMATIKA UTAMA  operator – untuk mengurangi dua angka (a - b), atau memberi tanda negatif ( -a + b atau a + -b).  Pembagian dua bilangan bulat menghasilkan bilangan bulat dengan membuang sisa pembagian: 7 / 4 adalah 1.  Apabila salah satu atau semua operand adalah bilangan riil hasil pembagian adalah bilangan riil : 7.0 / 4 adalah 1.75.  Operator modulus % memberikan sisa dari pembagian dua bilangan: 7 % 4 adalah 3.

10 PRECEDENCE  Multiplication, division, and modulus all have higher precedence than addition and subtraction.  1 + 2 * 3 ekivalen dengan 1 + (2 * 3).  All of these operators ``group'' from left to right, which means that when two or more of them have the same precedence and participate next to each other in an expression, the evaluation conceptually proceeds from left to right.  1 - 2 - 3 is equivalent to (1 - 2) - 3  Whenever the default precedence or associativity doesn't give you the grouping you want, you can always use explicit parentheses.  if you wanted to add 1 to 2 and then multiply the result by 3, you could write (1 + 2) * 3.

11 OPERATOR ‘=‘  operator = memberi nilai kepada variabel  x = 1 sets x to 1, and a = b sets a to whatever b's value is.  i = i + 1 this expression takes i's old value, adds 1 to it, and stores it back into i.  c = a = b ekivalen dengan c = (a = b). Memberi nilai b kepada a dan c  tidak ada bedanya: Variabel diberikan harga awal pada saat dideklarasikan atau pada saat pertama kali dipakai int a = 10; Atau int a; /* lalu... */ a = 10;

12 OPERATOR ‘++‘ DAN ‘- -’  Operator ++; menambah satu ke variabel Int x = 10; X ++; /* sama artinya dengan x = x + 1; */  Operator --; megurangi satu ke variabel Int x = 10; X --; /* sama artinya dengan x = x - 1; */

13 OPERATOR RINGKAS  Operator += X += 10;/* sama artinya dengan x = x + 10; */  Operator -= X -= 10;/* sama artinya dengan x = x - 10; */  Operator *= X *= 10;/* sama artinya dengan x = x * 10; */  Operator /= X /= 10;/* sama artinya dengan x = x / 10; */ Apa artinya?:a *= b + c;

14 OPERATOR RELASIONAL Operator Meaning == equal to != not equal < less than <= less than or equal to > greater than >= greater than or equal to Return 1 (TRUE) atau 0 (FALSE) 1 4 is 0, 5 == 5 is 1, and 6 != 6 is 0.

15 OPERATOR BOOLEAN &&and ||or !not (takes one operand; ``unary'')

16 PEMANGGILAN FUNGSI  Fungsi akan dipelajari lebih mendalam pada kuliah selanjutnya. Disini akan ditunjukkan bagaimana memakai fungsi-fungsi yang sudah disediakan oleh compiler.  Fungsi dipanggil dengan menyebut namanya diikuti oleh sepasang tanda kurung. Kalau fungsi mengambil argumen, kita letakkan argumen didalam kurung. printf("Hello, world!\n"); getchar(); printf("sum = %d\n", sum); printf("sum = %d\n", a + b + c);  Banyak fungsi mengembalikan suatu nilai. Nilai yang dikembalikan bisa disimpan variabel atau bisa langsung dipakai dalam suatu operasi c = sqrt(a * a + b * b); x = r * cos(theta);

17 CONTOH ASSIGNMENT #include main() { int sum; float money; char letter; double pi; sum = 10; /* assign integer value */ money = 2.21; /* assign float value */ Letter = 'A'; /* assign character value */ pi = 2.01E6; /* assign a double value */ printf("value of sum = %d\n", sum ); printf("value of money = %f\n", money ); printf("value of letter = %c\n", letter ); printf("value of pi = %e\n", pi ); }

18 CONTOH OPERATOR /*Demo Operators*/ #include main() { int a, b, c, d = 3; float e, f, g; c = 10; a = c + d; b = c * d; e = c/d; printf("%d, %d, %d, %d, %d, %f\n", a, b, c/d, c%d, d%c, e); f = 1.0; g = 2.0; e = f/g; printf("%f, %f, %f\n", f/g, f*g, e); a = 2147483648; b = 4294967295; printf("%d, %d\n", a, b); return 0; }

19 PREPROCESSOR STATEMENTS  Statemen define dipergunakan untuk membuat program lebih mudah dibaca /* Tanpa titik-koma ‘;’ */ /* karakter pertama dalam baris harus # */ #define TRUE 1 #define FALSE 0 #define NULL 0 #define AND & #define OR | #define EQUALS ==

20 INPUT DAN OUTPUT KE LAYAR #include main() /* program which introduces keyboard input */ { int number; printf("Type in a number \n"); scanf("%d", &number); printf("The number you typed was %d\n", number); } Contoh Run Type in a number 23 The number you typed was 23

21 KARAKTER KHUSUS Untuk FORMAT  \bbackspace  \fform feed  \nnew line  \rcarriage return  \thorizontal tab  \vvertical tab  \\backslash  \“double quote  \‘single quote  \ line continuation  \nnnnnn = octal character value  \0xnnnn = hexadecimal value (some compilers only)  printf("\007Attention, that was a beep!\n");

22 KODE JENIS VARIABEL UNTUK SCANF DAN PRINTF (sesudah %) d decimal integer o octal value x hexadecimal value h short integer l long integer f float value e double value c single character s sequence of characters, stop reading when an enter key or whitespace character [tab or space]

23 Contoh MEMBACA DAN MENCETAK #include main() { int sum; char letter; float money; printf("Please enter an integer value "); scanf("%d", &sum ); printf("Please enter a character "); /* the leading space before the %c ignores space characters in the input */ scanf(" %c", &letter ); printf("Please enter a float variable "); scanf("%f", &money ); printf("\nThe variables you entered were\n"); printf("value of sum = %d\n", sum ); printf("value of letter = %c\n", letter ); printf("value of money = %f\n", money ); }

24 GAYA MENULIS PROGRAM (1) #include main() { int sum,loop,kettle,job; char Whoknows; sum=9; loop=7; whoKnows='A'; printf("Whoknows=%c,kettle=%d\n",whoknows,kettle); }

25 GAYA MENULIS PROGRAM (2) #include main() { int sum, loop, kettle = 0, job; char whoknows; sum = 9; loop = 7; whoknows = 'A'; printf( "Whoknows = %c, kettle = %d\n", whoknows, kettle ); }

26 GAYA MENULIS PROGRAM(3)  the { and } braces directly line up underneath each other This allows us to check ident levels and ensure that statements belong to the correct block of code. This becomes vital as programs become more complex  spaces are inserted for readability We as humans write sentences using spaces between words. This helps our comprehension of what we read (if you dont believe me, try reading the following sentence. wishihadadollarforeverytimeimadeamistake. The insertion of spaces will also help us identify mistakes quicker.  good indentation Indent levels (tab stops) are clearly used to block statements, here we clearly see and identify functions, and the statements which belong to each { } program body.  initialization of variables The first example prints out the value of kettle, a variable that has no initial value. This is corrected in the second example.


Download ppt "Pengantar Pemrograman Fakultas Teknik Sipil & Lingkungan Materi: Variabel, type dan konstanta Ekspresi & assignment."

Presentasi serupa


Iklan oleh Google