24
Penyeleksian Kondisi if if else if else if switch

Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

  • Upload
    others

  • View
    24

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Penyeleksian Kondisi

• if

• if else

• if else if

• switch

Page 2: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if

if (EXPRESSION)

Statement;

Next_statement;

OROR

if (EXPRESSION)

{

Statement;

}

Next_statement;

Page 3: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if flowchart

Page 4: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if (sample)

#include <stdio.h>

#include <conio.h>

main()

{

float nilai = 74.01;float nilai = 74.01;

if (nilai >= 65)

{

printf("lulus");

}

getch();

}

Page 5: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if else

if (EXPRESSION)

{

Statement1;

}else{}else{

Statement2;

}

Next_statement;

Page 6: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if else flowchart

Page 7: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if else (sample)

#include <stdio.h>

#include <conio.h>

main()

{

float nilai = 74.01;

if (nilai >= 65)if (nilai >= 65)

{

printf("lulus");

}else{

printf(“tidak lulus");

}

getch();

}

Page 8: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Conditional Operator

• ?

• Condition ? Expression1 : Expression2

• Sample :

X = y > 7 ? 25 : 50X = y > 7 ? 25 : 50

Page 9: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Conditional Operator (cont)

X = y > 7 ? 25 : 50

Equals

if (y > 7)

{

x = 25;

}else{

x = 50;

}

Page 10: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if elseif else

if(EXPRESSION _1)

{

Statement1;

}elseif(EXPRESSION _2){}elseif(EXPRESSION _2){

Statement2;

}else{

Statement3;

}

Next_statement;

Page 11: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if elseif else Flowchart

Page 12: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

if else if (sample) #include <stdio.h>

#include <conio.h>

main()

{

float nilai;

printf(“Masukkan nilai : ”);

scanf(“%f”,&nilai);

if (nilai >= 65)if (nilai >= 65)

{

printf(“A");

}else if(nilai == 0){

printf(“Pasti E");

}else{

printf(“Bisa B,C atau D");

}

getch();

}

Page 13: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Nested If

if(expression1)

{

StatementA;

if(expression2)

{

StatementB; StatementB;

}else{

StatementC;

}

}else{

StatementD;

}

Statement E;

Page 14: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Flowchart

Page 15: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Exercise (1)

int a = 1;

int b = 2;

int c;

if(a < b){if(a < b){

c = a + b;

}else{

c = a - b;

}

c ?

Page 16: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Exercise (2)

int a = 1;

int b = 2;

int c;

c = a > b ? a+b : a-bc = a > b ? a+b : a-b

c ?

Page 17: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Exercise (3)int a = 1;

int b = 2;

int c = 1;

int d;

if(a==c){

if(b==c){

d = a + b + c;

}else{

d = a - b + c;

}

}else{

if(a != c){

d = a - b - c;

}else{

d = 0;

}

}

d ?

Page 18: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Exercise (4)

int a = 1;

int b = 2;

int c;

if(a < b){

c = a + b;

}else{

c = a - b;

}

c++;

c ?

Page 19: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Exercise (5)int a = 1;int b = 2;int c = 1;int d;

if(a==c){

if(b==c){

d = a + b + c;

}else{

d = a - b + c;

}

d++;

}else{}else{

if(a != c){

d = a - b - c;

}else{

d = 0;

}

d--;

}

d++;

d ?

Page 20: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

switch

switch(number)

{

case 1 :

statement1;

break;

case 2 :

statement2;statement2;

break;

case 3 :

statement3;

break;

default :

statement4;

break;

}

Statement 5;

Page 21: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Flowchart

Page 22: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Sample#include <stdio.h>

#include <conio.h>

main()

{

int pilih;

printf("masukan pilihan [1/0] :"); scanf("%d",&pilih);

switch(pilih)

{

case 1:case 1:

printf("Anda memilih %d",pilih);

break;

case 0:

printf("Anda memilih %d",pilih);

break;

default :

printf(“Input tidak valid“);

break;

}

getch();

}

Page 23: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Exercise (6)

• Buat flowchart dari contoh if else if diatas

• Buat flowchart dari contoh switch diatas

• Buat flowchart dari latihan 1 – 5 diatas

• Buat flowchart proses autentikasi pengisian• Buat flowchart proses autentikasi pengisian

KRS

Page 24: Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt€¦ · Microsoft PowerPoint - Teori Materi 4 ALGORITMA DAN PEMROGRAMAN II.ppt [Compatibility Mode] Author: User Created Date: 3/27/2008

Question ?Question ?