Lab1-Data Types, Making Decisions and Looping(1)
Lab1-Data Types, Making Decisions and Looping(1)
LAB 01
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
Page 3 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
List of Figures
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).
Page 5 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
Page 6 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
Page 7 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
Page 8 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
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
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);
Page 13 of 14
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING
Page 14 of 14