0% found this document useful (0 votes)
19 views8 pages

Dcit 22 Lesson 04 If Else Statement

Uploaded by

jmmaandal01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Dcit 22 Lesson 04 If Else Statement

Uploaded by

jmmaandal01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LECTURE 04:

IF…ELSE STATEMENT

IF…ELSE STATEMENT
An else statement can be combined with an if statement. An else statement contains the
block of code that executes if the conditional expression in the if statement resolves to 0 or a
FALSE value.
The else statement is an optional statement and there could be at most only one else
statement following if.

SYNTAX

IF STATEMENT

if (condition):
statement

IF…ELSE STATEMENT

if (condition):
statement
else:
Statement

ELIF STATEMENT

if (condition):
statement
elif (condition):
statement
else:
Statement

NOTE:
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope or block
of codes in the program. Other programming languages often use curly-brackets for this
purpose.

Example with an ERROR


The following are example of source code with a common error when constructing a if…else
statement inf Python programming language.

1|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


The image above shows a source code that will get an error. The console will say that your
source code is having an INDENTION ERROR and expected to have an indented block after
the ‘if’ statement. To correct this error, you should put and indention after the if statement.

Figure above shows that the source code is also having an error. If you run this source code,
the console will prompt you that you are having some error and the error code is SyntaxError:
expected ‘:’. To correct this kind of error, you should put a colon ( : ) after the condition of
your program.

Example source code of the IF…ELSE

This source code will check if the value of variable ‘b’ is greater than the value of the variable
‘a’. If it was true, the console will prompt that ‘b is greater than a’.

We have a variable ‘a’ with a value of 33 and a variable of ‘b’ with a value of 200. Our condition
was to check if the value of b is greater than the value of a. If it was true the console will
prompt or display that b is greater than a. However, if it was false, the program will execute
the else statement which will display on the console that a is greater than b.

2|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


The source code above demonstrates an elif statement where we have three conditional
statement. In the example source code, we have a variable of ‘a’ having a value of 33 and a
variable ‘b’ having a value of 33. At the first condition, the programming will check if b is greater
than a. if the condition is true, the program will execute the if statement and will ignore the
rest conditional statement. However, if the condition is false, the program will execute the elif
statement. If both the if statement and elif statement is false, the program will execute the
else statement.

EXAMPLE PROBLEM INVOLVING IF…ELSE STATEMENT

1. Create a simple program that will check if the number that the user input was an ‘EVEN’
or an ‘ODD’

a. Flowchart of the problem

3|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


b. Source Code

c. Program output

d. Explanation

We declare a variable N with a data type of integer or int that why we call the int ()
function. The problem also stated that it was a user input that why we also call the
input () function. We also declare a new variable remainder to store the
remainder value if we divided by the N into 2 using the modulo (%) operator. The
first condition will check if the value of the remainder variable was equal to 0. If the
IF condition was true, the program will execute the IF statement which is ‘The
number is EVEN!’. However, if the IF condition was false, the program will execute
the ELSE statement which is ‘The number is ODD!’

2. Create a simple program that can calculate the pay of an employee. If the hours
worked by the employee was less than 40 hours, the pay of the employee was hours
x rate. However, if the hours worked was greater than the 40 hours, the employee will
get an overtime pay. The formula for the overtime pay was overtime hours x 1.5 x rate.

a. Flowchart of the problem

4|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


b. Source Code

c. Program Output

d. Explanation

With the program problem stated above, we will declare two variable which are
the hours and the rate. Both variables were integer or whole number that’s
why we will use the int () function and both variables will be used to store the
input from the user that’s why we used the input () function. Our first condition
was if the hours work was less than 40 hours and if was true, we used the
formula Pay = hours x rate. However, if it was false, the program will execute
the ELSE statement and used the formula of Pay with OT pay.

5|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


NESTED IF STATEMENT

Create a program that will guess the entered number of the user. The number to be guess
was between 0 and 5.

number = int(input("Enter number between 0 and 5: "))

check = input("is your number between 0 and 2?")


if (check == "yes"):
check = input("is your number either 0 or 1?")
if (check == "yes"):
check = input("is your number between 0 ?")
if (check == "yes"):
print("THE NUMBER IS 0")
else:
print("THE NUMBER IS 1")
else:
print("THE NUMBER IS 2")
else:
check = input("is your number either 3 or 4?")
if (check == "yes"):
check = input("is your number 3?")
if (check == "yes"):
print("THE NUMBER IS 3")
else:
print("THE NUMBER IS 4")
else:
print("THE NUMBER IS 5")

6|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


Create a program that will check if the number entered by the user was even or odd and
either divisible by 4 or 5

number = int(input("Enter number: "))

if (number%2 == 0):
print("the number is even")
if(number%4 == 0):
print("and divisible by 4")
else:
print("the number is odd")
if(number%5 == 0):
print("and divisible by 5")

7|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS


Create a program that will check if the two numbers are equal, the first number is greater
than second number, or second number is greater than first number.

num1 = int(input("Enter 1st number: "))


num2 = int(input("Enter 2nd number: "))

if (num1 != num2):
if(num1 > num2):
print("First number is greater than second number")
else:
print("Second number is greater than first number")
else:
print("Both numbers are equal")

8|Page

DCIT 22 – COMPUTE R PROGRAMMI NG 1 PREPARED BY: EDISON S. DE LOS SANTOS

You might also like