0% found this document useful (0 votes)
108 views11 pages

Clo 2P: Politeknik Sultan Idris Shah Jabatan Kejuruteraan Elektrik Dec20012 - Programming Fundamentals

The document provides instructions for students to complete practical works on selection structures in C programming. It contains 3 labs that involve writing and running programs using if-else statements, nested if-else statements, and switch-case statements. The objectives are for students to be able to construct programs using simple conditional statements. Students are tasked to write programs that demonstrate conditional logic and solve problems involving selecting output based on multiple conditions. The document guides students through examples and tasks to practice using conditional statements in C programming.

Uploaded by

Sarahmei Mei
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)
108 views11 pages

Clo 2P: Politeknik Sultan Idris Shah Jabatan Kejuruteraan Elektrik Dec20012 - Programming Fundamentals

The document provides instructions for students to complete practical works on selection structures in C programming. It contains 3 labs that involve writing and running programs using if-else statements, nested if-else statements, and switch-case statements. The objectives are for students to be able to construct programs using simple conditional statements. Students are tasked to write programs that demonstrate conditional logic and solve problems involving selecting output based on multiple conditions. The document guides students through examples and tasks to practice using conditional statements in C programming.

Uploaded by

Sarahmei Mei
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/ 11

CLO 2P

POLITEKNIK SULTAN IDRIS SHAH


JABATAN KEJURUTERAAN ELEKTRIK
DEC20012 - PROGRAMMING FUNDAMENTALS
Title Selection Structure in C
Practical
3ii
Work
Course DEP 2A / DEP 2B / DTK 2A / DTK 2B
Session June 2019

Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.

OBJECTIVES :

Upon completion of this practical, students should be able:

1. To construct programs that uses simple if, if else, nested if else and switch
statements

EQUIPMENTS :

1. Personal computer / Laptop


2. Dev-C++ software and Arduino IDE
3. Maker UNO Board

Lab 3.1 Conditional Structure (Nested if-else)

Procedures:

3.1.1 Write the following programming below and try to understand it.

// This program demonstrates the use of nested if structure


#include <stdio.h>
int main ()
{
int A, B, C;

printf("Enter any three integer number here:\n");


scanf("%d%d%d",&A,&B,&C);
if(A>B)
{
if(A>C)
printf("Number %d is largest",A);
else
printf("Number %d is largest",C);
}
else
{
if(B>C)
printf("Number %d is largest",B);
else
printf("Number %d is largest",C);
}
getch();
}

1
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

3.1.2 Write the above program and save it as Practical31. To compile, click on
Execute and choose Compile. Alternatively the program can be compiled
by using Ctrl + F9 hotkey.
3.1.3 Run the program and capture the codes, Compile progress and output.
To Run, simply click on Execute > Run. Alternatively hit the Ctrl + F10.

Lab 3.2 Conditional Structure (switch)

Procedures:

3.1.4 Write the following programming below and save it as Practical32. In your
own word discuss your understanding of switch statement.

// This program demonstrates the use of if else switch case structure


#include<stdio.h>
int main()
{
int choice;
printf("Please choose your favourite fruits: \n");
printf("1: Strawberry\n"); //Selection provided
printf("2: Apple\n"); //Selection provided
printf("3: Banana\n"); //Selection provided
printf("4: Durian\n"); //Selection provided
printf("5: Watermelon\n"); //Selection provided
scanf("%d",&choice);

switch(choice) //select the choice


{
case(1):
printf("\nYour favourite food is Strawberry\n"); break;
case(2):
printf("\nYour favourite food is Apple\n"); break;
case(3):
printf("\nYour favourite food is Banana\n"); break;
case(4):
printf("\nYour favourite food is Durian\n"); break;
case(5):
printf("\nYour favourite food is Watermelon\n"); break;
default: //not entering the correct choice
printf("\nInvalid input\n"); //output for default
}
getch();
return 0;
}

2
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Lab 3.3 Conditional Structure (switch)

Procedures:

3.1.5 Write the following programming below and save it as Practical33.

#include<stdio.h>
int main()
{
char x;
printf("Please indicate your choice: \n");
printf("M: Merah \n"); //Selection provided
printf("B: Biru \n"); //Selection provided
printf("K: Kuning \n\n"); //Selection provided
scanf("%c",&x);

switch(x)
{
case ‘M’:
case ‘m’: printf("Merah\n"); break;
case ‘B’:
case ‘b’: printf("Biru\n"); break;
case ‘K’:
case ‘k’: printf("Kuning\n"); break;
default: puts("pilihan salah");
}
system("pause");
return 0;
}

3.1.6 Discuss the importance of break in a switch case statement.

3
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Lab 3.4 Demonstrating Hardware & Software Operation (LED Chase Effect)

Procedures:

3.1.7 Open a new sketch in Arduino IDE and type the following code.

//initializing variables

byte ledPin[] = {2,3,4,5,6,7,8,9,10,11,12,13};


int ledDelay(65);
int direction = 1;
int currentLED;
unsigned long changeTime;

void setup() {
for(int x = 0;x<10; x++){
pinMode(ledPin[x], OUTPUT);
}
changeTime=millis();
}

void changeLED(){
for(int x = 0;x<10;x++){
digitalWrite(ledPin[x],LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if(currentLED==9){
direction=-1;
}
if(currentLED==0){
direction=1;
}
}
void loop() {
if((millis()-changeTime)>ledDelay){
changeLED();
changeTime=millis();
}
}

3.1.8 Compile the sketch. If the coding is error free proceed with program
upload by pressing the Upload button.
3.1.9 It should immediately start running the code and your light should be
racing from one to the other.

4
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Lab 3.5 Now you try!

Procedures:

3.1.10 By taking Lab 3.1 until Lab3.3 into consideration, solve the following task.
3.1.11 Write your codes by using Dev-C++ software and check for any errors.
3.1.12 Rewrite your codes in the spaces provided.

Task A (if statement)

Create a program that ask for the user’s nationality and age. If the user is
a Malaysian with the age of 21 and above. The program will pass a remarks
“eligible to vote”.

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

5
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Task B (if statement)

Transfer the following pseudocode to a complete C program

START
1. Prompt for and get doYouHaveJob
2. if(doYouHaveJob ==‘Y’|| doYouHaveJob ==‘y’)
2.1 Prompt for and get doYouSmoke
2.2 if (doYouSmoke==‘N’||doYouSmoke==‘n’)
2.2.1 Print "you can marry my daughter"
2.4 else
2.4.1 Print "I’m sorry, you’re not good enough"
END

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

6
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Task C (Conditional Structures: Switch Case & Break)

Determine the outputs of the following programming code.

#include<stdio.h>
int main()
{
int x;
printf("Please indicate your choice: \n");
printf("1 \n"); //Selection provided
printf("2 \n"); //Selection provided
printf("3 \n\n"); //Selection provided
scanf("%d",&x);

switch(x)
{
case 1: puts("satu"); break;
case 2: puts("dua");
case 3: puts("tiga");
default: puts("pilihan salah");
}
system("pause");
return 0;
}

Answer:

Input: 1 Input: 2 Input: 3 Input: other than


1, 2 @ 3
Output: Output: Output: Output:

Discuss the needs and function of break statement.

Task D (Conditional Structures: Nested if)

Change the following pseudocode to a complete C program

7
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

START
1. Read status
2. if status is married
2.1 Print “Enter number of children”
2.2 Read numOfChildren
2.3 if numOfChildren is more than 5
2.3.1 Print “ you have a big happy family”
2.4 else if numOfChildren is less than or equal 5
2.4.1 Print “ you have a nice family”
3. else if Status is single
4. print “ you will be married, insyaallah”
END

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

8
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Task E (Conditional Structures: Switch Case & Break)

Change the following code snippet by applying the switch case selection
statement.

if (grade ==’A’)
printf (“ Excellent\n”);
else if (grade ==’B’)
printf (“ Good\n”);
else if (grade ==’C’)
printf (“Average\n”);
else if (grade ==’D’)
printf (“Poor\n”);
else if (grade ==’E’)
printf (“Very Weak\n”);
else if (grade ==’F’)
printf (“ Fail\n”);

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

[80 Marks]

9
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

Questions

1. What is required to avoid falling through from one case to the next? (1 marks)
A. End; B. break;
C. Stop; D. A semicolon.

2. What is the result of the following code? (1 marks)

int x=0;
switch(x)
{
case 1: printf("One");break;
case 0: printf("Zero"); break;
case 2: printf(“Hello World");break;
}
A. One B. Zero
C. Hello World D. ZeroHello World

3. a) Fill in the blank (6 marks)

#include <________________>
int main ()
{

int _______________;

printf(“\n\n How many years of service?”);


scanf (“______”, &years);

if(years>20)
{
printf(“Give a gold watch”);
}
__________
{
if(years>10)
{
printf(“Give a paperweight\n”);
}
___________
{
printf(“Give a pat on the back\n”);
}
}

_______________;
}

10
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P

4. Fill in the blank (5 marks)

a) Trace the output : ( 1 Mark)

#include<stdio.h>

________________
{
int a = 5;

switch(______)
{

case 1:
printf("First");
break;
case 2:
printf("Second");
__________________;

______:
printf("Third");
break;

_____________:
printf("Final");
break;

return 0;
}

Output:

5. What is the difference between switch and nested if-else. (2 Marks)

Switch Nested if

Prepared by: Checked by: Approved by:

MOHD ZEID BIN ABU BAKAR


PENSYARAH
JABATAN KEJURUTERAAN ELEKTRIK
POLITEKNIK SULTAN IDRIS SHAH
11
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii

You might also like