Professional Documents
Culture Documents
19/11/2021
Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.
OBJECTIVES :
1. To construct programs that uses simple if, if else, nested if else and switch
statements
EQUIPMENTS :
Procedures:
3.1.1 Write the following programming below and try to understand it.
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.
Coding
Compile Progress
Output
Procedures:
3.1.4 Write the following programming below and save it as Practical32. In your
own word discuss your understanding of switch statement.
Coding
Compile Progress
Output1
Output2
Procedures:
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;
}
Coding
Compile Progress
Output 1
Output 2
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.
Circuit
Coding
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.
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:
Coding
Compile Progress
Output Valid
Output Invalid
Coding
Compile Progress
Output 1
Output 2
#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:
The purpose the break statement is to break out of a loop early. For example if the
following code asks a use input a integer number x. If x is divisible by 5, the break
statement is executed and this causes the exit from the loop.
Answer:
Coding
Compile progress
Output Single
Output Married 1
Output Married 2
Change the following code snippet by applying the switch case selection
statement.
Answer:
Coding
[80 Marks]
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.
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
#include <stdio.h>
int main ()
{
Int years;
if(years>20)
{
printf(“Give a gold watch”);
}
else
{
if(years>10)
{
printf(“Give a paperweight\n”);
}
else
{
printf(“Give a pat on the back\n”);
}
}
return 0;
}
Int main___
{
int a = 5;
switch(a)
{
case 1:
printf("First");
break;
case 2:
printf("Second");
break;
case 3:
printf("Third");
break;
case 4:
printf("Final");
break;
return 0;
}
Output: