Lecture 4 - Conditional Statement
Lecture 4 - Conditional Statement
PROGRAMMING
LECTURE 4 – CONDITIONAL STATEMENTS
TOPICS
Instead of nesting
many levels of if…
else, we could use if…
else if ladder
IF…ELSE IF LADDER
Instead of nesting
many levels of if…else,
we could use if…else if
ladder
Exercise: Write
program according to
the flowchart in
previous slide
ARITHMETIC COMPOUND ASSIGNMENT
OPERATORS
INCREMENT AND DECREMENT OPERATORS
INCREMENT AND DECREMENT OPERATORS
GENERAL FORM OF SWITCH STATEMENT
GENERAL FORM OF SWITCH STATEMENT
switch(expression){
case item1:
//one or more statements;
break;
case item2:
//one or more statements;
break;
case itemn:
//one or more statements;
break;
default:
//one or more statements;
break;
}
GENERAL FORM OF SWITCH STATEMENT
ACTIVITY: ARITHMETIC OPERATORS
Make a program to
o Ask user to input two numbers
o Ask user to input one of these arithmetic operators represented by character
(‘+’, ‘-’, ‘*’, ‘/’)
o Display “invalid operator” if user inputs the wrong character
o Else, display the corresponding result
17
HOMEWORK: VOWELS
Make a program to
o Ask user to input a character
o Display an error message if user inputs a non alphabet character (check with
ASCII e.g., ‘a’ < c <‘z’ for lower case characters)
o If the character is alphabet, check if it is a vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) or a
consonant
o Display the result
18