Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

CONDITIONAL

MATA KULIAH BAHASA PEMROGRAMAN

OLEH
IKA MAULID NUR AHMAD, ST, MM
Outline
• Boolean expression
• if statement
• nested if statement
• switch case statement
• Flowchart

2
Boolean Expression

Boolean Expression
• Operators
– Comparison – Boolean
• Equal == • And &&
• Not equal != • Or ||
• Less < • Not !
• Greater >
• Less than or equal to <=
• Greater than or equal to
>=
0 and 0 = 0 0 or 0 = 0 not 0 = 1
0 and 1 = 0 0 or 1 = 1 not 1 = 0
1 and 0 = 0 1 or 0 = 1
1 and 1 = 1 1 or 1 = 1 3
Boolean Expression Example
• From the equation: X2+9X+10 = 0
– How can we check that value of X is the
answer for above equation?
((X*X +9*X +10) == 0) //true if X is the
answer

• Condition: Is value Y even number?


(Y%2 == 0) //true if Y is even
OR
(Y%2 != 1) //true if Y is even
4
Boolean Expression

Example: Boolean Expressions


double x = 4.0;

Expression Value
x < 5.0 true
___________
x > 5.0 false
___________
x <= 5.0 true
___________
5.0 == x false
___________
x != 5.0 true
___________
(3!=4)&&(7<5) false
___________
(4>4)||(5<=10) true
___________
5
Outline
Boolean expression
if statement
nested if statement
switch case statement
Flowchart

6
if statement

if statement
Execute the specific statement when
the ”condition” becomes true
Syntax:
true
if (condition) if (condition) {
true
statement; statement1;
statement2;
if (condition) {
}
statement;
}
7
if statement

if statement

if (condition)
statement; condition
False
True
if (condition) {
statement; Statement

8
if statement

if statement

if (condition) {
statement1; condition
statement2; True False
} Statement1

Statement2

9
if statement

if statement example
10

• BMI (Body Mass Index)


Weight in Kilograms
BMI =
(Height in Meters) X (Height in Meters)

BMI Weight Status


Below 18.5 Underweight
18.5 – 24.9 Normal
25.0 – 29.9 Overweight
30.0 and Above Obese (Extremely Fat)
10
if statement

if statement example
BMI Weight Status
11
Weight in Kilograms Below 18.5 Underweight
BMI = 18.5 – 24.9 Normal
(Height in Meters) X (Height in Meters) 25.0 – 29.9 Overweight
30.0 and Above Obese (Extremely Fat)
double BMI = W /(H*H);
if(BMI<18.5)
Console.WriteLine(“Underweight”);
if(BMI>=18.5 && BMI<=24.9)
Console.WriteLine(“Normal”);
if(BMI>=25.0 && BMI<=29.9)
Console.WriteLine(“Overweight”);
if(BMI>=30.0)
Console.WriteLine(“Obese”);
Menghitung BMI
#include <stdio.h>
#include <stdlib.h>

int main()
{
double BMI, W, H;
printf("Berat badan anda (Kg) : " );scanf("%lf",&W);
printf("tinggi badan anda (Meter) : "); scanf("%lf",&H);
BMI = W/(H*H);
printf("Nilai BMI = %lf \n\n",BMI);
printf("Body Mass Index anda = ");
if(BMI <18.5)
printf("Underweight \n");
if(BMI>=18.5 && BMI<=24.9)
printf("Normal \n");
if(BMI>=25.0 && BMI<=29.9)
printf("Overweight \n");
if(BMI>=30.0)
printf("Obese \n");
return 0;
}
if statement

if…else… statement
13

• If condition is true  execute statement1


• If condition is false  execute statement2
• Syntax:

if (condition) if (condition)
statement1; //true statement1; //true
else else {
statement2; //false statement2; //false
statement3; //false
}
if statement

if…else… statement

condition
True False

Statement1 Statement2

if (condition)
statement1; //true
else
statement2; //false
14
if statement

if…else… statement

condition
True False

Statement2
if (condition) Statement1
statement1; //true Statement3
else {
statement2; //false
statement3; //false
} 15
if statement
if…else… statement
example
• Write the program which check input
number.
– input : integer number
– output : message to inform that
number is odd or even.
Value in N Output
Even Number It’s even number.
Odd Number It’s odd number.

16
if statement
if…else… statement
example
Value in N Output
Even Number It’s even number.

Odd Number It’s odd number.

if(n%2 == 0)
Console.WriteLine(“It’s even number”);
else
Console.WriteLine(“It’s odd number”);

17
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num;
printf("masukan sebuah bilangan : "); scanf("%d",&num);
if ( num%2 == 0 )
printf("bilangan genap \n");
else
printf("bilangan ganjil \n");
return 0;
}
Outline
Boolean expression
if statement
nested if statement
switch case statement
FlowChart

19
Nested if statement

Nested if statement
20

int N;
N = int.Parse(Console.ReadLine());
if#1
if (N >= 0)
{ if#2
if (N==0)
Console.WriteLine(“N is zero number”);
else
Console.WriteLine(“N is positive number”);
}
else
Console.WriteLine(“N is negative number”);
Nested if statement

Nested if statement
21

int N;
N = int.Parse(Console.ReadLine());

if (N > 0)
Console.WriteLine(“N is positive number”);
if#1
else if#2
if (N==0)
Console.WriteLine(“N is zero number”);
else
Console.WriteLine(“N is negative number”);
N 0
3
Nested if statement
-5
-5
0
3 N is positive
zero number
number
N is negative number
22

int N;
N = int.Parse(Console.ReadLine());

if (N >= 0) 30-5>=
>=00
{  False
True


if (N==0) False True
Console.WriteLine(“N is zero number”);
else
Console.WriteLine(“N is positive number”);
}
else
Console.WriteLine(“N is negative number”);
Nested if statement
N -4
0
3
Nested if statement
0
3
-4 N is positive
zero number
number
N is negative number
23

int N;
N = int.Parse(Console.ReadLine());

if (N > 0) -4
0>
3 > 00 False
True
Console.WriteLine(“N is positive number”);

else
if (N==0) True
False
Console.WriteLine(“N is zero number”);
else
Console.WriteLine(“N is negative number”);
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num;
printf("masukan sebuah bilangan : ");scanf("%d",&num);
if ( num >= 0 )
{ if (num == 0)
printf("bilangan nol \n");
else printf("bilangan positif \n");
}
else
printf("bilangan negatif \n");

return 0;
}
Outline
Boolean expression
if statement
nested if statement
switch case statement
Flowchart

25
switch...case statement

switch…case statement
26

• For selecting a statement where its label


corresponds to the value of the switch
expression.
switch (<expression>)
{
case <constant-expression> :
<expression> <statements>;
must be int, break;
char, string
[default:
<statements>;
break;]
}
switch...case statement

Example: switch-case (1)


27
int day_num;
int day name Console.Write("Input the day");
day_num;
day_num = 302
1
1 Sunday int.Parse(Console.ReadLine());

2 Monday switch(day_num) <expression>


{case 1:
3 Tuesday Console.Write ("Today is Sunday");
break;
4 Wednesday <constant-expression>
case 2:
5 Thursday
Console.Write("Today is Monday");
6 Friday break;
:
7 Saturday default :
Console.Write ("I don’t know");
break;
} 27
case 6 :
#include <stdio.h>
printf("Hari Jumat");
#include <stdlib.h>
break;
case 7 :
int main()
printf( "Hari Sabtu");
{
break;
int day;
default :
printf("hari ke : ");scanf("%d",&day);
printf("Anda salah memasukkan hari ");
switch (day)
break;
{
}
case 1 :
printf("Hari Minggu");
printf("\n");
break;
return 0;
case 2 :
}
printf("Hari Senin");
break;
case 3 :
printf("Hari Selasa");
break;
case 4 :
printf("Hari Rabu");
break;
case 5 :
printf( "Hari Kamis");
break;
switch...case statement

Convert switch-case to if else


29
switch version with default if else version
int a;
a= if (a == 1 || a == 2)
int.Parse(Console.ReadLine()); Console.WriteLine("Hi");

switch (a) { else if (a == 3 || a == 4)


case 1 : Console.WriteLine("Hello");
case 2 :
else
Console.WriteLine("Hi"); Console.WriteLine("Bye");
break;
case 3 :
case 4 : switch version without default
Console.WriteLine("Hello");
switch (a) {
break;
default : case 1 :
Console.WriteLine("Bye"); case 2 :
break; Console.WriteLine("Hi");
} break;
}
Outline
Boolean expression
if statement
nested if statement
switch case statement
Flowchart

30
Flowchart

Flowchart Symbols Overview


31

• Graphical representation
Terminator
Process

Input/output

Condition

Connector
Flow line
Flowchart

Program Flowchart Example


32

Start
Start

Statement1
Statement1

Statement2
Statement2

Statement3
Statement3

Statement4
Statement4

End
End
Flowchart

if statement flowchart
33 Start
Start

statement1;
Statement1
Statement1
if (condition)
statement2; //true
else { Condition
Condition
true false
statement3; //false
} Statement2
Statement2 Statement3
Statement3
statement4;

Statement4
Statement4

End
End
if statement flowchart
Start
Start

n= n=int.Parse(Console.ReadLine());
int.Parse(Console.ReadLine());
if (n>0)
n= 2*n+5; n>0
n>0
true false
else {
Console.
Console.Write(“Go”); n=2*n+5;
n=2*n+5; Write(“Go”);
n = n%4;
} n=n%4;
n=n%4;

End
End 34
LATIHAN SOAL
Soal 1
•Buat
  Program untuk menghitung nilai anda dengan
ketentuan sebagai berikut :
Nilai Total =
Nilai :
A = Nilai Total ≥ 90
B = Nilai Total ≥ 80
C = Nilai Total ≥ 65
D = Nilai Total ≥ 50
E = Nilai Total < 50
Soal 2
Buat program untuk menentukan bilangan
terbesar dari 3 buah bilangan

You might also like