4. if Statement
Pseudo Code:
if <condition is true> start
step 1
step 2
…
step k
end_if
if Statement:
if (<condition>) {
statement 1
statement 2
…
statement k
}
5. if Statement
Example:
int num1, num2, min;
printf(“Key-in 2 numbers: “);
scanf(“%d%d”, &num1, &num2);
min = num1;
if (num1 > num2)
min = num2;
printf(“Smallest: %dn”, min);
Key-in 2 numbers: _
num2 ?
num1 ?
min ?
Key-in 2 numbers: 20 15
_
20
15
20
15
Key-in 2 numbers: 20 15
Smallest: 15
_
20 > 15?
6. if Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 65?
7. if Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 35?
8. if - else Statement
Syntax:
if (expression)
statement1;
else
statement2;
or
if (expression) {
statement1;
statement2;
} else
statement3;
9. if - else Statement
or
if (expression) {
statement1;
statement2;
} else {
statement3;
statement4;
}
10. if - else Statement
Example:
if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %dn”, min);
num2 15
num1 10
min ?
10 < 15?
_
10
Smallest: 10
_
11. if - else Statement
Example:
if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %dn”, min);
num2 15
num1 20
min ?
20 < 15?
_
15
Smallest: 15
_
12. if - else Statement
Example:
if (num1 < num2) {
min = num1;
max = num2;
}
else {
min = num2;
max = num1;
}
printf(“Min = %d, Max = %dn”, min, max);
num2 125
num1 700
min ??
700 < 125?
_
max ??
Min = 125, Max = 700
_
125
700
13. if – else Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 21?
What will the output
be if the mark is 74?
14. if – else Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 74?
What will the output
be if the mark is 14?
15. if – else Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else {
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
}
What will the output
be if the mark is 14?
What will the output
be if the mark is 70?
16. Take a break … (Learn styles)
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
17. Take a break … (Learn styles)
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
Difficult to read!!!
Don’t you think so??
19. if – else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
if-else-if statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;
if-else-if statement
20. if – else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else (expression)
statement;
if-else-if statement
Be careful…common
mistake made by
students !!
21. Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 1 is
true, so …
step m, step …. will be
executed
22. Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 1 is
false, so …
• step m, step …. will be
skipped, and
• condition 2 will be tested
23. Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 2 is
true, so …
step n, step …. will be
executed
24. Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 2 is also
false, so …
• step n, step …. will be
skipped, and
• step x will be executed
25. Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;
scanf(“%c”, &letter);
if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower casen”);
else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper casen”);
else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digitn”);
else
printf(“Special charactern”);
}
Is the letter a lower case?
Is the letter an upper case?
Is the letter a digit?
26. Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;
scanf(“%c”, &letter);
if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower casen”);
else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper casen”);
else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digitn”);
else
printf(“Special charactern”);
}
(the letter is a lower case) true
(the letter is a lower case) false
(the letter is an upper case) true
(the letter is a lower case) false
(the letter is an upper case) false
(the letter is a digit) true
(the letter is a lower case) false
(the letter is an upper case) false
(the letter is a digit) false
27. Exercise
Develop a program for the following
problem.
Given a mark, determine its grade based on
the table below:
75 < mark < 100 grade = A
65 < mark < 74 grade = B
55 < mark < 64 grade = C
40 < mark < 54 grade = D
0 < mark < 39 grade = F
others error message
28. Pseudo Code
Start
Input student’s mark
If student’s mark is greater than or equal to 75
Print “Grade = A”
else If student’s mark is greater than or equal to 65 and lower than
75
Print “Grade = B”
else If student’s mark is greater than or equal to 55 and lower than
65 Print “Grade = C”
else If student’s mark is greater than or equal to 40 and lower than
55 Print “Grade = D”
else If student’s mark is greater than or equal to 0 and lower than
40 Print “Grade = F”
else
Print “Input error”
End
29. A
n
s
w
e
r
1
int mark;
printf(“Key-in the mark: “);
scanf(“%d”,&mark);
if ((mark >= 75) && (mark <= 100))
printf("Grade = A”);
else if ((mark >= 65) && (mark <= 74))
printf(" Grade = B”);
else if ((mark >= 55) && (mark <= 64))
printf(" Grade = C”);
else if ((mark >= 40) && (mark <= 54))
printf(" Grade = D”);
else if ((mark >= 0) && (mark <= 39))
printf(" Grade = E”);
else
printf(“Input errorn”);
30. A
n
s
w
e
r
2
int mark;
char grade ;
printf(“Key-in the mark : “);
scanf(“%d”,&mark);
if ((mark >= 75) && (mark <= 100))
grade =‘A’;
else if ((mark >= 65) && (mark <= 74))
grade =‘B’;
else if ((mark >= 55) && (mark <= 64))
grade =‘C’;
else if ((mark >= 40) && (mark <= 54))
grade =‘D’;
else if ((mark >= 0) && (mark <= 39))
grade =‘E’;
else
printf(“Input errorn”);
printf(“Your grade is %c”, grade );
31. A
n
s
w
e
r
3
int mark;
char grade;
printf(“Key-in the mark: “);
scanf(“%d”,&mark);
if ((mark >= 75) && (mark <= 100))
grade=‘A’;
else if ((mark >= 65) && (mark <= 74))
grade=‘B’;
else if ((mark >= 55) && (mark <= 64))
grade=‘C’;
else if ((mark >= 40) && (mark <= 54))
grade=‘D’;
else if ((mark >= 0) && (mark <= 39))
grade=‘E’;
if ((mark > 100) || (mark < 0))
printf(“Input errorn”);
else
printf(“Your grade is %c”, grade);
33. Nested ifs
Example:
if (age > 18) {
if (age > 55)
price = 2.50; /* Price for senior citizens */
else
price = 5.00; /* Price for adults */
}
else {
if (age < 1)
price = 0.0; /* Price for infants */
else
price = 1.50; /* for children & teenagers*/
}
This price is valid for
people: age > 55
This price is valid for
people: 18 < age < 55
This price is valid for
people: age < 1
This price is valid for
people: 1 < age < 18
34. Nested ifs - Problem
Example:
if (age > 18)
if (age > 55)
price = 2.50; /* Price for senior citizens */
else
price = 5.00; /* Price for adults */
Which if does this else
belong to?
35. Nested ifs - Problem
Try to understand the consequences….
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
if (age > 18) {
if (age > 55)
price = 2.50;
}
else
price = 5.00;
36. Nested ifs - Problem
By default, else will be attached to the
nearest if
if (age > 18)
if (age > 55)
price = 2.50;
else
price = 5.00;
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
37. switch and break
Syntax:
switch (expression) {
case expression1:
statement1;
break;
case expression2:
statement2;
break;
…
default:
expressionX;
break;
}
Syntax:
switch (expression) {
case expression1:
statement1;
break;
case expression2:
statement2;
break;
…
default:
expressionX;
break;
}
Don’t forget the
brackets !!
Don’t forget the
curly brackets !!
Don’t forget the
colons !!
38. switch and break
Important Rule !!
Syntax:
switch (expression) {
case expression1:
statement1;
break;
case expression2:
statement2;
break;
…
default:
expressionX;
break;
}
Must be
INTEGER or
CHAR !
39. switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
break;
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
Assume month = 1,
so …
…this step will be
executed. Later …
…case is terminated
here. Jump to …
January
_
January
End _
40. switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
break;
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
…this step will be
executed. Later …
March
_
March
End _
Assume month = 3,
so …
…case is terminated
here. Jump to …
41. switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
break;
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
Now…what will
happen if this break
is taken out from the
program?
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
No more !!
42. switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
…this step will be
executed. Later …
February
_
February
March
_
Assume month = 2,
so …
…case is terminated
here. Jump to …
February
March
End _
…execution continues. Thus,
this step is executed . So …
43. switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
Now…what will
happen if these
breaks are taken out
from the program?
And …
if month = 1 ?
And …
if month = 34 ?
44. switch and break
Example:
char jantina;
printf (“Masukkan Jantina Anda: (L/P)n”);
scanf (“%c”,&jantina);
switch (jantina) {
case ‘P’:
printf(“nJantina adalah PEREMPUANn”);
break;
case ‘L’:
printf(“nJantina adalah LELAKI”);
break;
default:
printf(“nTIDAK DAPAT DIPASTIKAN!”);
break;
}
Masukkan Jantina Anda: (L/P)
L
Masukkan Jantina Anda: (L/P)
L
Jantina adalah LELAKI
jantina ?
L