0% found this document useful (0 votes)
13 views14 pages

Lab1-Data Types, Making Decisions and Looping(1)

This document outlines Lab 01 for the Programming for Engineers course, focusing on data types, decision-making, and looping in C programming. It includes objectives, background information on fundamental data types, operators, and control statements, as well as pre-lab preparation and in-lab procedures. The lab is structured into five activities that guide students through input/output statements, data type definitions, operators, decision-making statements, and looping constructs.

Uploaded by

ngocthanh2821
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)
13 views14 pages

Lab1-Data Types, Making Decisions and Looping(1)

This document outlines Lab 01 for the Programming for Engineers course, focusing on data types, decision-making, and looping in C programming. It includes objectives, background information on fundamental data types, operators, and control statements, as well as pre-lab preparation and in-lab procedures. The lab is structured into five activities that guide students through input/output statements, data type definitions, operators, decision-making statements, and looping constructs.

Uploaded by

ngocthanh2821
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/ 14

INTERNATIONAL UNIVERSITY

SCHOOL OF ELECTRICAL ENGINEERING

PROGRAMMING FOR ENGINEERS LABORATORY

COURSE ID: EE058IU

LAB 01

Data Types, Making Decisions


and Looping

Full name: ……………………………………...…………...

Student ID: …………………………………...………….....

Class: …...………………… Group: ……..………………..

Date: …...…………………………………………………...

Page 1 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Table of Contents

I. OBJECTIVES ................................................................................................................ 5
II. BACKGROUND ............................................................................................................ 5
2.1. Simple input and output statements ............................................................................ 5
2.2. Fundamental data types ............................................................................................... 6
2.3. C token keywords ........................................................................................................ 6
2.4. Arithmetic operators and the precedence of arithmetic operators .............................. 7
2.4.1. Assignment operator in C Programming Language ............................................. 7
2.4.2. Arithmetic Operator in C Programming Language .............................................. 8
2.4.3. Relational Operator in C Programming ............................................................... 8
2.4.4. Logical Operator in C Programming .................................................................... 8
2.5. Simple making decision statements ............................................................................ 9
2.5.1. if...-statement .............................................................................................................. 9
2.5.2. if...else... ...................................................................................................................10
2.6. Looping .....................................................................................................................11
2.6.1. while .......................................................................................................................11
2.6.2. for...........................................................................................................................12
2.6.3. do...while .................................................................................................................13
III. PRE-LAB PREPARATION .......................................................................................13
IV. IN-LAB PROCEDURE ...............................................................................................13

Page 2 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

List of Tables

Table 1. Token types. ................................................................................................................ 6


Table 2. Token meanings. ......................................................................................................... 7
Table 3. Supported arithmetic operators. ................................................................................ 8
Table 4. Relational operators in C. .......................................................................................... 8
Table 5. Logical operators in C. .............................................................................................. 8
Table 6. Logical operator chart. .............................................................................................. 9
Table 7. for-loop component explanation. .............................................................................12

Page 3 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

List of Figures

Figure 1. Input/Output streams. ............................................................................................... 5


Figure 2. Tokens in C. .............................................................................................................. 6
Figure 3. C data types. ............................................................................................................. 7
Figure 4. if-statement ............................................................................................................... 9
Figure 5. if...else... statement .................................................................................................11
Figure 6. while-loop ...............................................................................................................12
Figure 7. for-loop. ..................................................................................................................12
Figure 8. do...while loop ........................................................................................................13

Page 4 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

I. OBJECTIVES
1. Write simple computer programs in C language, use the input/output statements,
understand the fundamental data types and learn computer memory concepts.
2. Use assignment, arithmetic, relational and logical operators, learn the precedence order
of these operators and write simple decision-making statements.
3. Use basic problem-solving techniques, develop algorithms through the process of top-
down, stepwise refinement, use the if-selection statement, the if…else…-selection statement
and switch-statement to select actions.
4. Use the while, for, do...while repetition statement to execute statements in a program
repeatedly, identify between counter-controlled and sentinel-controlled repetition, learn
structured programming, use increment, decrement, assignment operators and break-
statement.
This laboratory experiment is divided into five activities:
a) The first activity involves the simple input and output statements, the definition of constants
and variables (global and local), tokens in C.
b) The second activity involves the data type definition, memory concept and type conversion.
c) The third activity involves some basic assignment, arithmetic, relational and logical
operators.
d) The fourth activity involves making decision (if-selection, if...else...-selection or switch).
e) The last activity involves while, for, do...while looping.
II. BACKGROUND
2.1. Simple input and output statements
C programming language has several built-in library functions to perform the input and
output tasks. Two commonly used functions for I/O (Input/Output) are printf() and scanf().
The scanf() function reads formatted input from standard input (keyboard) whereas
the printf() function sends formatted output to the standard output (screen).

Figure 1. Input/Output streams.


Example: Program to print an integer
#include <stdio.h>
int main(){
int number;
// printf() displays the formatted output
printf(“Enter an integer: ”);
// scanf() reads the formatted input and stores them
scanf(“%d”, &number);
// printf() displays the formatted output
printf(“You entered: %d\n”, number);
return 0;
}

Page 5 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

2.2. Fundamental data types


In C programming, variables or memory locations should be declared before it can be used.
Similarly, a function also needs to be declared before use.
Data types simply refers to the type and size of data associated with variables and functions.
int - Integer data types
Integers are whole numbers that can have both positive and negative values but no decimal
values. Example: 0, -5, 10...
Types of integer data types:
1. Integer (%d)
2. Short integer (%hd)
3. Long integer (%ld)
Each type is again classified into signed (by default) and unsigned integer (%u).
float - Floating types
Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare
a floating-point variable in C by using either float (%f) or double (%lf) keyword.
+ Difference between float and double
The size of float (single precision float data type) is 4 bytes and the size of double (double
precision float data type) is 8 bytes. Floating-point variable has a precision of 6 digits whereas
the precision of double is 14 digits.
char - Character types
Keyword char (%c) is used for declaring character type variables. For ex, char test = ‘h’;
2.3. C token keywords
In C programming, punctuation, individual words, characters etc. are called tokens. Tokens
are basic building blocks of C programming.

Figure 2. Tokens in C language.


Token example:
No Token Type Example 1 Example 2
1 Keyword do while
2 Constant number sum
3 Identifier -76 p89
4 String “HTF” “PRIT”
5 Special Symbol * @
6 Operators ++ /
Table 1. Six token types.

Page 6 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Basic building blocks and definition:


Token Meaning
Keyword A variable is a meaningful name of data storage location in
computer memory. When using a variable, you refer to memory
address of computer.
Constant Constants are expressions with a fixed value.
Identifier The term identifier is usually used for variable names.
String Sequence of characters.
Special Symbol Symbols other than the alphabets and digits and white-spaces.
Operators A symbol that represents a specific mathematical or non-
mathematical action.
Table 2. Token meanings.
C qualifiers
Qualifiers alters the meaning of base data types to yield a new data type.
Size qualifiers
Size qualifiers alters the size of a basic type. There are two size qualifiers, long and short. For
example:
long double i;
Sign qualifiers
Integers and floating-point variables can hold both negative and positive values. However, if
a variable need to hold positive value only, unsigned data types are used. For example:
// unsigned variables cannot hold negative value
unsigned int positiveInteger;
Constant qualifiers
An identifier can be declared as a constant. To do so const keyword is used.
Volatile qualifiers
A variable should be declared volatile whenever its value can be changed by some external
sources outside the program. Keyword volatile is used for creating volatile variables.

Figure 3. C data types.


2.4. Arithmetic operators and the precedence of arithmetic operators
2.4.1. Assignment operator in C Programming Language
1. Assignment operator is used to assign value to a variable.
2. Assignment operator is denoted by equal to (=) sign.

Page 7 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

3. Assignment operator is binary operator which operates on two operands.


4. Assignment operator have two values – l-value and r-value. Operator copies r-value into
l-value.
5. Assignment operator has lower precedence than all available operators but has higher
precedence than comma operator.
2.4.2. Arithmetic Operator in C Programming Language
1. C programming supports 5 arithmetic operators.
2. Arithmetic operators are used for “Arithmetic Calculation”.
Supported arithmetic operators:
Five arithmetic operators are shown in the following table. Arithmetic operators are used to
perform arithmetic operations in C programming.
Operator Meaning Example
+ Addition Operator 10 + 20 = 30
- Subtraction Operator 20 – 10 = 10
* Multiplication Operator 20 * 10 = 200
/ Division Operator 20 / 10 = 2
% Modulo Operator 20 % 6 = 2
Table 3. Supported arithmetic operators.
2.4.3. Relational Operator in C Programming
In C programming, we can compare the value stored between two variables and depending
on the result, we can follow different blocks using Relational Operator in C.
In C, we have different relational operators such as:
Operator Meaning
> Greater than
>= Greater than or equal to
<= Less than or equal to
< Less than
== Is equal to
!= Is not equal to
Table 4. Relational operators in C.
2.4.4. Logical Operator in C Programming
Whenever we use if-statement then we can use relational operator which tells us the result
of the comparison, so if we want to compare more than one condition then we need to use
logical operators. Suppose we need to execute certain block of code if and only if two
conditions are satisfied then we can use Logical Operator in C Programming.
Operator Name of the Operator
&& And Operator
|| Or Operator
! Not Operator
Table 5. Logical operators in C.
Logical Operator Chart:

Page 8 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Operator Applied Between Condition 1 Condition 2 Final Output


AND True True True
True False False
False True False
False False False
OR True True True
True False True
False True True
False False False
NOT True - False
False - True
Table 6. Logical operator chart.
2.5. Simple making decision statements
2.5.1. if...-statement
Syntax
if(expression)
statement1;

Figure 4. if-statement
Explanation:
 Expression is Boolean expression
 It may have True or False value
if-statement:
if(conditional)
{
Statement No 1
Statement No 2
Statement No 3
.

Page 9 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

.
.
Statement No N
}
Note:
1. More than one condition can be written inside if-statement.
2. Opening and closing braces are required only when “code” after if-statement occupies
multiple lines.
if(conditional)
Statement No 1
Statement No 2
Statement No 3
In the above example, only Statement 1 is a part of if-statement.
void main(){
int a=5, b=6, c;
c = a + b;

if (c==11)
printf("Execute me 1");
printf("Execute me 2");
}
Output:
Execute me 1
Execute me 2
1. Code will be executed if condition statement is True.
2. Non-zero number inside if means “TRUE Condition”
if(100)
printf("True Condition");
2.5.2. if...else...
Syntax
if(expression){
Statement1;
Statement2;
}else{
Statement3;
Statement4;
}

next_statement;

Page 10 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Figure 5. if...else... statement


Explanation:
 If expression is True then Statement1 and Statement2 are executed
 Otherwise, Statement3 and Statement4 are executed.
Sample program on if-else statement:
void main(){
int marks=50;
if (marks>=40){
printf("Student is pass.");
}else{
printf("Student is fail.");
}
}
Output:
Student is pass.

2.6. Looping
2.6.1. while
Syntax
initialization;
while(condition){
Statement 1;
Statement 2;
incrementation;
}

Page 11 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Figure 6. while-loop
Note:
 For single line of code – opening and closing braces are not needed.
 while(1) is used for infinite loop.
 Initialization, incrementation and condition steps are on different lines.
 while-loop is also known as “Entry Controlled Loop” [i.e. conditions are checked if
found true then and then only code is executed].
2.6.2. for
Syntax
for(i=0; i<5; i++){
printf("\nC Programming for loop");
}
In the above program, we have printed the string C Programming for loop 5 times.
for(initial expression; test expression; update expression){
body of loop;
}
Part of Loop Explanation
initial expression It is initial expression used to initialize subscript/loop variable
test expression Expression decides whether to go inside the loop or not
update expression Updating the loop variable for next iteration of loop
Table 7. for-loop component explanation.

Figure 7. for-loop.

Page 12 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Explanation of for-loop:
1. Firstly, the for-loop executes the initialize statement in which the subscript variable will
be initialized with the initial value.
2. After the initialize statement, the condition part of the loop will be executed if the
condition becomes true then body of the loop will be executed otherwise the loop will
be terminated.
3. If the loop condition becomes true then body of the loop will be executed. After the
execution of the for-loop body the control goes to the third part of the loop statement
i.e. expression updation
4. After updating subscript variable control again goes to execute condition statement.
2.6.3. do...while
Syntax

initialization;
do{
Statement 1;
Statement 2;
incrementation;
}while(condition);

Figure 8. do...while loop


Note:
 It is exit controlled loop.
 Initialization, incrementation and condition steps are on different line.
 It is also called “Bottom Tested” [i.e. condition is tested at bottom and the body must
execute at least once].

III. PRE-LAB PREPARATION


Read the theory carefully at home before coming to the class.
IV. IN-LAB PROCEDURE
Exercise 1
Write a C program that reads 12 integers. The program should count the number of positive
and negative values among 12 inputs. Print them on the screen.
Output:

Page 13 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING

Enter number 1: 16 Enter number 5: -21 Enter number 9: 42


Enter number 2: -7 Enter number 6: -15 Enter number 10: -37
Enter number 3: 23 Enter number 7: -33 Enter number 11: -20
Enter number 4: 49 Enter number 8: 54 Enter number 12: 18
Number of positive: 6
Number of negative: 6
Exercise 2
Write a C program that reads two integers a and b. Check if a ൑ b or not. If yes, prints all
the even numbers from a to b and their sum. Otherwise, prints “As a is greater than b so
that we cannot calculate the sum.”
Exercise 3
Write a C program that reads a character. The input process continues when the character
prompted is not a vowel (a,e,i,o,u). The program should also count the number of valid
inputs until a vowel is entered. Otherwise, prints “No character was entered.”
Output:
Enter a character: a Enter a character: y
No character was entered. Enter a character: k
Enter a character: j
Enter a character: h Enter a character: i
Enter a character: t Number of inputs: 5
Exercise 4
Write a C program that reads a student grade which is a character. The program should
print the classification based on the grade as:
Grade Classification
A Excellent
B Good
C Fair
D Average
F Weak
Other characters Invalid
At the end, the program should ask the user to continue to read another grade or not. If yes,
continues, otherwise, prints “Exit program...”.
Output:
Enter a grade: B Invalid. Enter a grade: D
Good! Average!
Do you want to continue? y Do you want to continue? n
Enter a grade: Y Exit program...
Exercise 5
Write a C program that ...

Page 14 of 14

You might also like