Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

DJM20032 C PROGRAMMING

PRACTICAL TASK : PT 3
TITLE : CONTROL STATEMENT (SELECTION STATEMENT)

1.0 Objectives

At the end of this lesson, students will be able to:

i. Use selection statements (if, if..else, nested if, switch..case)

2.0 Introduction

Control statements control the flow of execution of the statements of a program. The various types of
control statements in C language are as under: -
a. Sequential
b. Conditional
c. Iteration

In conditional control, the execution of statements depends upon the condition-test. If the condition
evaluates to true, then a set of statements is executed otherwise another set of statements is followed.
This control is also called Decision Control because it helps in making decision about which set of
statements is to be executed. Decision control structure in C can be implemented by using: -
a. If statement
b. If-else statement
c. Nested if else statement
d. else-if ladder
e. case control structure
f. conditional operator
DJM20032 C PROGRAMMING

3.0 Apparatus

a) Computer
3.1.1 Software Code- Block

4.0 Procedures

4.1 Understand the task below and answer all the question:

Task 1

Create a program that can add HPNM value if the HPNM> = 3.7, the output will display "Excellent,
keep it up" and if otherwise the output will display "you have to try again"

Task 2

Write a C program to input marks of five subjects mathematic, Bahasa Melayu, English language,
Thermodynamic and Science Computer, calculate percentage and grade according to given
conditions:
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Grade F
DJM20032 C PROGRAMMING

Task 3

Write a C program to create menu driven calculator that performs basic arithmetic operations (add,
subtract, multiply and divide) using switch case and functions. The calculator should input two
numbers and an operator from user. It should perform operation according to the operator entered
and must take input in given format.

Task 4

Write a program according to the flowchart


DJM20032 C PROGRAMMING

5.0 Results/Data
State the Source code and paste the output for Task 1, Task 2, Task 3 and Task 4 on the section of
result and data.
C Code Output
Task 1

Task 2

Task 3
DJM20032 C PROGRAMMING

Task 4

6.0 Discussion

A) “if… else… if” and “switch …case” both programming constructs has ability to take decision based
on conditions. Both are almost similar in nature. However, there is always a debate among
beginners which to use and when to use what?? Explain your understanding when is suitable to
use “if… else… if” and “switch …case”?

Both "if...else...if" and "switch...case" are conditional statements that allow


programmers to execute different code based on different conditions.
However, there are some differences between them that can affect which
one you should use in a given situation. The "if...else...if" statement is used
when there are multiple conditions that need to be evaluated. It is also
useful when the conditions are complex and cannot be easily expressed in
a single expression. Each "if" statement in the series is evaluated in
sequence until a condition is met and its corresponding code block is
executed. If none of the conditions are met, the code in the "else" block is
executed. The "switch...case" statement is used when there is a single
variable or expression that needs to be tested against multiple possible
values. It is often used when the conditions are simple and can be
expressed in a single expression. The code in the matching case statement
is executed when the expression matches the value in the case. If there is
no match, the code in the default statement is executed. In general,
"if...else...if" is more flexible and can handle more complex conditions, but
can be less efficient than "switch...case" for simple conditions with a large
DJM20032 C PROGRAMMING

7.0 Conclusion and Recommendation


Deduce a conclusion from your observation.

In conclusion, selection statements such as "if", "if...else", "nested if", and


"switch...case" are essential constructs in programming that allow for decision-
making based on different conditions. Each statement has its strengths and
weaknesses and is suitable for specific situations. Use "if" statements when you
have a simple condition that needs to be evaluated, and the condition can be
expressed in a single expression. Use "if...else" statements when you have two
conditions to evaluate, and you want to execute different code based on each
condition. Use "nested if" statements when you have multiple conditions to
evaluate, and each condition depends on the previous one. Use "switch...case"
statements when you have a single variable or expression that needs to be tested
against multiple possible values, and the conditions are simple and can be
expressed in a single expression. It is essential to understand the requirements of
your program and choose the selection statement that best fits the situation. It is
also important to write clear and readable code to make it easy for other
programmers to understand and maintain your code.
DJM20032 C PROGRAMMING

LABORATORY REPORT FORMAT

1.0 TITLE:
o A brief, concise, yet description title
o An example of a title would be: "Effects of Ultraviolet Light on Borax Crystal Growth Rate".

2.0 EXPERIMENT OUTCOME


o State clearly the experiment outcome.

3.0 THEORY
o Any theory related to experiment must be explained clearly with diagram, figures and so on.
o Include graph, formulas and figures (if any) related to experiment theory.

4.0 APPARATUS/EQUIPMENTS
o Brief explanation of equipment.
o Draw and label the equipment.

5.0 SAFETY PRECAUTIONS


o List down and explain any safety precautions that apply during experiment.

6.0 PROCEDURES
o Write in passive sentences about all the steps taken during the experiment.
o Draw and label any related figures if necessary.

7.0 RESULT/DATA
o Data tabulation with units
o Data processing (plotting graph, calculation to obtain the results of the experiments.

8.0 DISCUSSION
o Discuss or answers given questions.
o Discuss comparison between theory and data collection

9.0 CONCLUSIONS AND RECOMMENDATION


o Conclusion based on experiment data / output.
o If there is anything about your experiment that you think could be improved upon, provide
suggestions for doing so.

10.0 REFERENCES
o State all the references that used

You might also like