0% found this document useful (0 votes)
16 views

Lecture 4 Control Structures Part II

Uploaded by

janithadilsham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 4 Control Structures Part II

Uploaded by

janithadilsham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

CSC 1113

Programming Techniques

Mrs.T.D.Gilmini
Senior Lecturer
Department of Computer Science
Outline
• Repetition control structures
– for loop
– While loop
– Do while loop
Learning outcomes
• Define the concept of repetition structure.
• Specify the repetition of a group of program
statements using the for, while and do-while
and a counter.
• Design loops using algorithm for solving
problems.
Repetition control structures
• A Repetition control structure will execute a
block of code a number of times based on
some condition.
• Commonly refer to repetition control
structures as loops.
• Why Repetition in Programs?
– Some computer programs have to
perform recurring actions.
– Enable reusability of the program code.
Repetition control structures
• Repeat the execution of compound statements of
the program either specified number of times or
until a particular condition is being satisfied
• Consists of two types of statements:
– Loop body (sequence of statements)
– Control Statement (conditions)
• Types of Loops
– Can categorize based on condition check
– Can categorize based on number of repetitions
The loop structures in C
• Three Types
1. while loops
2. do-while loops
3. for loops
Based on condition check
• Entry controlled loop/Pre checking loop
– Condition is checked before executing the body of
loop
– E.g.: While loop, For loop
• Exit controlled loop/Post checking loop
– Condition is checked after executing the body of
loop
– E.g.: Do-While Loop
Based on number of repetitions
• Counter controlled loops (definite repetition loop)
– the number of iterations is known before the loop begins to
execute
– has the following components:
• a control variable.
• the increment (or decrement)value
• the loop terminating condition
– Eg: for Loop
• Sentinel controlled loops (indefinite repetition loop )
– the number of iterations is not known before the loop starts
executing
– a special value called sentinel value is used to change the loop
control expression from true to false
– E.g.: processing of data from a text file of unknown size
// The following program does the work of finding a length of a string using sentinel
controlled loop
#include <stdio.h>

// Function to find the length of the string


void lengthOfString(char* string)
{
int count = 0, i = 0;
char temp;

// Pointer to string
temp = string[0];

// Iterate till temp points to NULL


while (temp != '\0') {
count++;
i++;
temp = string[i];
}

// Print the length of the string


printf("The length of string is %d",count);
}

// Driver Code
int main()
{
// Given String
char string[] = "GeeksForGeeks";

// Function Call
lengthOfString(string);
for loop
• a more efficient loop structure in ‘C’ programming
– The initial value of the for loop is performed only once.
– The condition is a Boolean expression that tests and
compares the counter to a fixed value after each iteration,
stopping the for loop when false is returned.
– The incrementation/decrementation increases (or
decreases) the counter by a set value.

Loops in C: For, While, Do While looping Statements [Examples]


(guru99.com)
for loop : Example
#include <stdio.h>

int main() {
int i, j;

// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times

// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}

return 0;
}
Increment and Decrement
i++ Vs i--
Compound assignment (i+=x)
• In this program, value of i is incremented not
by 1, but by 2 instead !
Try?
• Write C program to get following output using
nested for loop.
**********
**********
**********
**********
**********
While loop
• Write a program to display all odd numbers
below 10 using while loop.
Example : While loop
• A program that displays all odd numbers
below 10
• Do while loop

• Syntax
– Bottom tested conditional loop statement.
– The loop statement repeats when the expression is TRUE.
– The body or action of the loop statement is executed one
or more times.
– The loop statement exits when the expression is FALSE.
Example: Do while loop
• A program that displays even numbers
between 1 ~ 10
Exercise
• Write a program to find prime numbers in
between 3 ~ 100
– Clue : You might combine both While and for
looping techniques

You might also like