0% found this document useful (0 votes)
15 views23 pages

Loops

The document outlines the course 'Problem Solving Using Computers' for B.Tech first-year students, detailing the course structure, assessment methods, and key programming concepts such as loop control structures. It explains different types of loops (while, do while, for) and their applications in programming, particularly in computing repetitive tasks like triangular numbers. Additionally, it provides examples and code snippets to illustrate the implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

Loops

The document outlines the course 'Problem Solving Using Computers' for B.Tech first-year students, detailing the course structure, assessment methods, and key programming concepts such as loop control structures. It explains different types of loops (while, do while, for) and their applications in programming, particularly in computing repetitive tasks like triangular numbers. Additionally, it provides examples and code snippets to illustrate the implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

B.

TECH FIRST YEAR


ACADEMIC YEAR: 2020-2021

Course Name: Problem Solving Using Computers

Course code : CS 1001


Lecture series no : 01
Credits : 3
Mode of delivery : online (Power point presentation)
Faculty : Krati Dubey
Email-id : [email protected]
“Learning the
Session outcome implementation of
looping”
Assignment
quiz
Mid term examination – II Assessment criteria’s
End term Examination
PROGRAM
OUTCOMES
MAPPING WITH
CO3
[PO.1]. Engineering knowledge: Apply the knowledge of
mathematics, science, engineering fundamentals, and an engineering
specialization to the solution of complex engineering problems

[PSO 2]. Will be able to apply knowledge of AI, Machine Learning and
Data Mining in analyzing big data for extracting useful information from
it and for performing predictive analysis.
Loop Control Structures
Controlling the program flow
• Forms of controlling the program
flow:
– Executing a sequence of statements
– Using a test to decide between
alternative sequences (branching) Statement1
Statement2
– Repeating a sequence of statements Statement3
(until some condition is met) Statement4
(looping) Statement5
Statement6
Statement7
Statement8

03/10/2025 CSE 1001 Department of CSE 6


Program Looping
• A set of statements that executes repetitively for a number of
times.
• Simple example: displaying a message 100 times:

printf(hello !\n”);
printf(hello !\n”)
printf(hello !\n”)

Repeat 100 times
printf(hello !\n”)
printf(hello !\n”)
printf(hello !\n”)

Program looping: enables you to develop concise programs containing


repetitive processes that could otherwise require many lines of code !

03/10/2025 CSE 1001 Department of CSE 7


The need for program looping

Example problem: computing triangular numbers.


(The n-th triangular number is the sum of the integers from 1 through
n)
#include <stdio.h>
int main (void)
{
int triangularNumber;
triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;
printf(“The eighth triangular number is
%d“,triangularNumber);
return 0;
}

What if we have to compute the 200-th (1000-th, etc) triangular number ?

We have 3 different statements for looping.


03/10/2025 CSE 1001 Department of CSE 8
Iterative (loop) control structures
 Three kinds of loop control structures:
while
do while
for

03/10/2025 CSE 1001 Department of CSE 9


Iterative (loop) control structures
 Each loop control structure will have
 Program loop: body of loop.
 control statement  tests certain conditions & then directs repeated
execution of statements within the body of loop.

 Two types: Based on position of control statement.

1) Entry controlled loop: control is tested before the start of the loop. If
false, body will not be executed.

2) Exit controlled loop: test is performed at the end of the body. i.e. body
of loop executed at least once.

03/10/2025 CSE 1001 Department of CSE 10


Entry Controlled & Exit controlled loops
Entry
Entry

Test False
Body of
Condition
The loop

True

Body of
True
The loop Test
Condition

False

03/10/2025 CSE 1001 Department of CSE 11


Example – 200th triangular number
Statement before triangularNumber = 0
loop

init_expression n=1

no
loop_condition n<=200

yes
triangularNumber =
Statement(s) triangularNumber + n

loop_expression n=n+1

Statement after loop Print triangularNumber

03/10/2025 CSE 1001 Department of CSE 12


The ‘for’ loop
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}

1 init_expression

no
5 2 loop_condition

yes

3 Program statement

4 Loop expression

Next Statement

03/10/2025 CSE 1001 Department of CSE 13


How for works
• The execution of a for statement proceeds as follows:
1. The initial expression is evaluated first. This expression usually sets a
variable that will be used inside the loop, generally referred to as an
index variable, to some initial value.
2. The looping condition is evaluated. If the condition is not satisfied (the
expression is false – has value 0), the loop is immediately terminated.
Execution continues with the program statement that immediately
follows the loop.
3. The program statement that constitutes the body of the loop is executed.
4. The looping expression is evaluated. This expression is generally used to
change the value of the index variable
5. Return to step 2.

03/10/2025 CSE 1001 Department of CSE 14


The for statement
sum = 0;

no

1 2 5 4
for ( n = 1; nyes
<= 200; n = n + 1 )
{ sum = sum + n; }

Next Statement
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}

03/10/2025 CSE 1001 Department of CSE 15


Finding sum of natural numbers up to 100

#include <stdio.h>
int main()
{
int n;
int sum;
sum=0; //initialize sum

for(n = 1; n <=100; n=n + 1)


{
sum=sum + n;
}
printf(“%d”,sum);
return 0;
}

03/10/2025 CSE 1001 Department of CSE 16


Infinite loops
• It’s the task of the programmer to design correctly the algorithms so
that loops end at some moment !
// Program to count 1+2+3+4+5
#include <stdio.h>
int main()
{
What is wrong here ?
int i, n = 5, sum =0; Does the loop end?
for ( i = 1; i <= n; n = n + 1 )
{
sum = sum + i;
printf(“%d”,sum);
}
return 0;
}

03/10/2025 CSE 1001 Department of CSE 17


Example – for with a body of 2 statements

// Program to generate a table of triangular numbers

#include <stdio.h>
int main()
{
int n, triangularNumber=0;

printf(“TABLE OF TRIANGULAR NUMBERS\n\n“);


printf(“Sum from 1 to n\n“);

for ( n = 1; n <= 10; n++ )


{
triangularNumber += n;
printf(“The %d th triangular number is %d\
n”,n,triangularNumber);
}
return 0;
}

03/10/2025 CSE 1001 Department of CSE 18


for loop variants
• Multiple expressions (comma between…)
for(i=0 , j=10 ; i<j ; i++ , j--)

• Omitting fields (semicolon have to be still…)


i=0;
for( ; i<10 ; i++ )

• Declaring variables
for(int i=0 ; i=10 ; i++ )

03/10/2025 CSE 1001 Department of CSE 19


while-statement
General format:

while (test expression)


{
Note: braces optional if
body of the loop only one statement.
}

Entry controlled loop statement.


Test condition is evaluated & if it is true, then body of the loop is executed.
This is repeated until the test condition becomes false, & control transferred
out of the loop.
Body of loop is not executed if the condition is false at the very first attempt.
While loop can be nested.

03/10/2025 CSE 1001 Department of CSE 20


#include <stdio.h>
int main()
{ int i;
for (i = 1; i < 11; ++i)
{ printf("%d ", i); }
return 0; }
#include <stdio.h>
int main()
{ int i = 1;
while (i <= 10) {
printf("%d\n", i);
++i; }
return 0; }
10/03/2025 CSE 1001 Problem Solving using Computers (PSUC) - 2018 21
Finding sum of natural numbers up to 100

#include <stdio.h> #include <stdio.h>


int main() int main()
{
{ int n;
int n; int sum;
int sum; sum=0; //initialize
sum=0; //initialize sum sum
n=1;
while (n < 100)
for(n = 1; n < 100; n=n + 1) {
{ sum= sum + n;
sum=sum + n; n = n + 1;
} }
printf(“%d”,sum);
printf(“%d”,sum); return 0;
return 0; }
}

03/10/2025 CSE 1001 Department of CSE 22


Program to reverse the digits of a number
#include <stdio.h>
int main()
{
int number, rev=0, right_digit;

printf(“Enter your number.\n“);


scanf(“%d”,&number);

while ( number != 0 )
{
right_digit = number % 10;
rev=rev*10 + right_digit;
number = number / 10;
}
printf(“The reversed number is %d“, rev);
return 0;
}
03/10/2025 CSE 1001 Department of CSE 23

You might also like