CC-112L Programming Fundamentals Laboratory 03 Introduction To C & Structured Program Development - II
CC-112L Programming Fundamentals Laboratory 03 Introduction To C & Structured Program Development - II
CC-112L Programming Fundamentals Laboratory 03 Introduction To C & Structured Program Development - II
Programming Fundamentals
Laboratory 03
Version: 1.0.0
Contents:
• Learning Objectives
• Required Resources
• General Instructions
• Background and Overview
o Iteration Statement
o while Statement
o Counter Controlled Iteration
o Sentinel Controlled Iteration
o Nested Controlled Statements
o for Statement
o switch Statement
o do…while Iteration Statement
• Activities
o Pre-Lab Activity
▪ while Iteration Statement
▪ The pseudocode statements
▪ Simple Example Program while loop
▪ Exercise 1
▪ Iteration Essentials
▪ Exercise 2
▪ Counter-controlled iteration
▪ Exercise 3
▪ Example Program of Counter-controlled iteration
▪ Sentinel-controlled iteration
▪ Always Use Braces in a while Statement
▪ Task 01: Sum of Divisors in a range
▪ Task 02: Sum of Digits of number
▪ Task 03: Dry Run Program
o In-Lab Activity
▪ Nested Controlled Statement
▪ Explanation of Nested Controlled Program
▪ for Iteration Statement
▪ for Statement Header Components
▪ Control variables defined in a for header
▪ General format of a for statement
▪ Expressions in the for-statement’s header are optional ▪
for Statement Flowchart
▪ Exercise 4
▪ Exercise 5
▪ do…while Iteration Statement
▪ do…while Statement Flowchart
▪ Task 01: Printing Stars I
▪ Task 02: Printing Stars II
▪ Task 03: Printing Stars III
▪ Task 04: Floyd’s Triangle
▪ Task 05: Reverse a number
o Post-Lab Activity
▪ switch Statement
▪ Rules for switch statement in C language
Learning Objectives:
• Using while Iteration Statement
• Understanding Iteration Essentials
• Understanding Countered Controlled Iteration
• Understanding Sentinel-Controlled Iteration
• Understanding Nested Control Statements
• Using for Iteration Statement
• Using do…while Iteration Statement
• Using switch Statement
Resources Required:
• Desktop Computer or Laptop
• Microsoft ® Visual Studio 2022
General Instructions:
• In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY
discuss with your Teaching Assistants (TAs) or Lab Instructor.
• Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Teachers:
A statement which repeats a set of instructions until a condition is satisfied. A single execution of set
of instructions is called an iteration.
while Statement:
To use if or if…else statement inside another if or if…else statement is referred as Nested Control
Statement.
for Statement:
for Statement allows to repeat a set of instructions up to a specified number of
times. switch Statement:
The switch Statement allows you to execute a single block of code using alternative conditions. This
can be done using the if…else Statement as well but switch Statement is easy to read and has a simple
syntax.
do…while Iteration Statement:
do…while Statement executes at least once because first iteration runs without checking the
condition. The condition is checked after the first iteration.
Activities:
Pre-Lab Activities:
While Iteration Statement:
An iteration statement (repetition statement / loop) repeats an action while some condition remains
true.
Syntax:
The syntax of a while loop in C language:
while (condition) {
statement(s);
}
Simple Example Program:
As a while statement example, consider a program segment that finds the first power of 3 which is
larger than 100.
The integer variable product is initialized to 3. When the following code segment finishes executing,
product will contain the desired answer:
Fig
1. (While Iteration)
Output:
Exercise 1:
• Counter-controlled iteration
• Sentinel-controlled iteration.
Exercise 2:
Dry Run the following program and fill up the trace table:
Fig 4. (Exercise No 2)
Counter-controlled iteration:
Fig 7. (Exercise No 3)
Laboratory 03 – Introduction to C & Structured Program Development – II Page 8 of 23
CC-112L Programming Fundamentals SPRING 2022
Error Line # Solution
Sentinel-controlled iteration:
In sentinel-controlled iteration use a sentinel value to indicate “end of data entry”. A sentinel value
also is called a signal value, a dummy value, or a flag value.
In the Example there will be phases:
So, this program introduces the data type double to handle numbers with decimal point that is,
floating point numbers.
Here, a cast operator is used to force the averaging calculation to use floating- point numbers. These
features are explained after the program listing. Note that lines 13 and 21 both include the sentinel
value in the prompts requesting data entry. This is a good practice in a sentinel-controlled loop.
Always Use Braces in a while Statement:
Without this while loop’s braces (lines 16 and 26), only the statement on line 17 would be in the loop’s
body. The code would be incorrectly interpreted as
Task 01: Sum of Dividends in a range [Estimated 15 minutes / 10 marks] Create a C program
that:
Task 02: Sum of Digits of number [Estimated 15 minutes / 10 marks] Create a C program which:
Fig. 11
(Sum of digits of a number)
• Submit “.c” file named your “Roll No” on Google Classroom
10
In-Lab Activities:
Nested Controlled Statement:
C supports nesting of loops. Nesting of loops is the feature in C that allows the looping of statements
inside another loop. Let's observe an example of nesting loops in C. Any number of loops can be
defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting
level can be defined at n times.
Concept of nested loop:
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Example of nested loop Program:
The a single for-iteration statement handles all the details of counter-controlled iteration of the while
loop in Counter.c program from line 6-10. For readability, try to fit the for-statement’s header (line 8)
on one line. The for statement executes as follows:
• When program begins executing, control variable counter will be initializing it to 1 by for
statement.
• Next, it will check loop condition counter <= 5. At start value of counter is 1, so the condition is
true, and the for statement executes its printf statement (line 9) to display counter’s value,
namely 1.
• Next, the for statement increments the variable counter using the expression ++counter and loop
will execute so on.
• This loop continues until the control variable counter becomes 6. At this point, the loop
condition is false and iteration terminates. The program continues executing with the first
statement after the for (line 12).
Output:
Control Variables Defined in a for Header Exist Only Until the Loop Terminates:
When you define the control variable in the for header before the first semicolon (;), as in line 8 of
Counter.c the control variable exists only until the loop terminates. So, attempting to access the
control variable after the for-statement’s closing right brace (}) is a compilation error.
for (int counter = 1; counter <= 5; ++counter) {
• Initialization names the loop’s control variable and provides its initial value, •
Loop Condition determines whether the loop should continue executing,
• Increment updates the control variable value after executing the statement so that the loop
condition eventually becomes false.
Expressions in the for-Statement’s Header Are Optional:
All three expressions in a for header are optional. If you remove the loop condition, the condition is
always true, thus creating an infinite loop. You can remove the initialization expression if the program
initializes the control variable before the loop. You can also remove the increment expression if the
program calculates the increment in the loop’s body or if no increment is needed.
for Statement Flowchart:
Exercise 4:
Dry Run the following program and fill up the trace table:
Exercise 5:
Fig 20.
(Exercise No 5)
Rewrite code:
// Write code below this line:
Laboratory 03 – Introduction to C & Structured Program Development – II Page 15 of 23
CC-112L Programming Fundamentals SPRING 2022
Dowhile.c
Output:
Task 01: Printing Stars [20 minutes / 20 marks] Write a C program which: [Hint: Use Nested
for Statements]
Task 02: Printing Stars [20 minutes / 10 marks] Write a C program which: [Hint: Use Nested
for Statements]
Task 03: Printing Stars [20 minutes / 10 marks] Write a C program which: [Hint: Use Nested
for Statements]
Task 04: Floyd’s Triangle [30 minutes / 20 marks] Create a program in C to print the Floyd's
Triangle
Sample Output:
Fig. 27 (In-Lab Task)
Task 05: Reverse a number [30 minutes / 10 marks] Create a C program that:
Post-Lab Activities:
Switch Statement:
The switch statement in C is an alternate to if-else-if statement which allows us to execute multiple
operations for the different possible values of a single variable called switch variable. Here, we can
define various statements in the multiple cases for the different values of a single variable.
Switch Statement syntax:
Task 01: Multiple Choice Calculator [Estimated 45 minutes / 30 marks] Create a Console based
calculator which:
• Displays the menu on Console and takes an input from user as follows:
Submissions:
• For In-Lab Activity:
▪ Save the files on your PC.
▪ TA’s will evaluate the tasks offline.
• For Pre-Lab & Post-Lab Activity:
▪ Submit the .c file on Google Classroom and name it to your roll no.
Evaluations Metric:
• All the lab tasks will be evaluated offline by TA’s
• Division of Pre-Lab marks: [35 marks] ▪ Sum of Divisors in a range [10 marks] ▪
Sum of digit of number [10 marks] ▪ Dry Run Program [15 marks]
• Division of In-Lab marks: [50 marks] ▪ Task 01: Printing Stars I [10 marks] ▪ Task
02: Printing Stars II [10 marks] ▪ Task 03: Printing Stars III [10 marks] ▪ Task 04:
Floyd’s Triangle [10 marks] ▪ Task 05: Reverse Number [10 marks]
• Division of Post-Lab marks: [30 marks] ▪ Task01: Simple Multiple-Choice
Calculator [30 marks]
• for-loop Statement in C
https://www.programiz.com/c-programming/c-for-loop
• do-while Statement in C
https://www.programiz.com/c-programming/c-do-while-loops
• switch-case Statement in C
https://www.programiz.com/c-programming/ c-switch-case-statement
• break-continue Statement in C
https://www.programiz.com/c-programming/c-break-continue-statement