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

COMPUTER PROGRAMMING LABORATORY

(ECE128)
EXPERIMENT 2
PART A
1. Listing 2-1

INPUT

OUTPUT
2. Listing 2-2

INPUT

OUTPUT
3. Listing 2-3

INPUT

OUTPUT
4. Listing 2-4

INPUT

OUTPUT
PART B
1. Figure 2.2
INPUT

OUTPUT
2. Figure 2.4

INPUT

OUTPUT
EXERCISE
1. INPUT

OUTPUT
2. INPUT
OUTPUT
3. INPUT

OUTPUT
DISCUSSION

In this report, there are bunch of statements that is applied in programming.


For example, ‘if’ and ‘else’ statement is used to perform the operations based on
some specific condition. The operations specified in ‘if’ block are executed if
and only if the given condition is true. Then there are ‘switch’ statement that
allows to execute one code block among many alternatives. It also act the same
as ‘if’ and ‘else’ statement but is much easier to read and write.
For Part A, which conclude Listing 2-1, Listing 2-2, Listing 2-3, and Listing
2-4. In this part, ‘if’ and ‘else’ statement is used. For the first part, Listing 2-1,
the program is about inserting correct or non-correct password. When the
correct password where the condition is met, is being insert, ‘if’ statement is
used to display that, that is the correct password, which ‘printf’ is used to
display that statement. However, ‘else’ statement is used to display the wrong
statement because the specified condition is not met. Therefore, the password
that is given is a ‘failed attempt’, where ‘printf’ display this statement. Next, in
Listing 2-2, two variables is an integer which has been program as main. This
two variables is used to calculate integers. Hence, ‘ans’ type is used to program
as a calculation. Then, ‘if’ and ‘else’ statement is used to display whether the
integer is a positive or a negative number. Furthermore, in Listing 2-3 is a
comparison program where an integer is compared to determine whether which
is a greater or lesser of an integer/number. ‘char’ data type is used to program
the comparison where ‘char’ act as a character or sentence to display the
sentence “greater/lesser than”. The variables which are x and y are integer. The
‘if’ and ‘else’ statement is used to compare those two variables which are x and
y. Hence, ‘printf’ is used to display the variables which is an integer and show
the comparison between two numbers. In Listing 2-4, ‘switch’ statement is
used. This statement is the same as ‘if’ or ‘else’ statement but this statement
allows to execute one code block among many alternatives. In this case,
‘switch’ statement is used with ‘(alph)’ where it evaluated once and compared
with the character which is “a/b/c” of each ‘case’ label. If there is a match, the
corresponding statements after the matching label are executed. For example, if
the character of the ‘alph’ is equal to ‘a/b/c’, statements after “case ‘a/b/c’” are
executed until ‘break’ is encountered. If there is no match, the ‘default’
statement is executed where in this case it prints out as “Your entered other than
a/b/c”.
For Part B, it’s the same concept as Part A. For the first section, Figure 2.2,
this program is about a comparison between two variables. The statement that is
used are the ‘if’ and ‘else’ statement where an integer is act as main. There are
two variables which are x and y, that is used to compare whether which of the
integer are much higher value or much lower value. Therefore, ‘if’ statement is
used to determine the first variable as a higher value which is printed out as
‘greater than’ and vice versa for ‘else if’ statement which is printed out as
‘lesser than’ while there are also ‘equal’ variable that is used. Next section, is
Figure 2.4. This program used ‘switch’ statement. In this program, ‘alph’ is
being program as a character type. The ‘alph’ is use as the expression of the
‘case’. The characters are A, B, and C. The program were asked to put between
this 3 character and will display a sentence that starts with that character/letter
according to the character that is put. There are also ‘default’ statement which
are executed if there are no match in the program.

For the Exercise part, there are also ‘if’, ‘else’ and ‘switch’ statements. For
the first exercise, the program used ‘if’ and ‘else’ statement. The ‘int’ type is
used as main and were act as value. In this case, ‘if’ statement is used to
determine the value as an even number whereas ‘else’ statement is used to
determine an odd number value. For the second exercise, this program
performs a calculation where rational operator is being asked to be put which
are “+,-,*,/” , so the program can execute a calculation based on the operation
given. In this case, there are ‘int’ type and ‘char’ type where the ‘int’ display the
numbers/values and the ‘char’ is used to put the “arithmetic operation”. ‘if’ and
‘else’ statement are used to execute the operation and display the calculation
based on the “arithmetic operations”. Lastly for the third exercise, this program
is the same as the second exercise but used the ‘switch’ and ‘case’ statement to
write and read easier. In this case, the expression ‘s’ is used in the ‘switch’
statement to act as the “arithmetic operation” whilst the ‘int’ type is use as a
value to perform the calculation which is use in the ‘ans’. Then, ‘default’
statement is used to display the wrong/unknown operation if it does not match
the program.
CONCLUSION
In conclusion, student are able to use the if selection statement and the if…
else selection statement to select actions. Moreover, student is also able to
understand and use the equality and relational operators in the conditional
statements. Lastly, student understand the multiple selection using the switch
selection statement.

You might also like