KEMBAR78
Lecture 1 Looping Statements | PDF | Control Flow | Software Development
0% found this document useful (0 votes)
17 views26 pages

Lecture 1 Looping Statements

The document outlines a course on Problem Solving with Programming, focusing on enhancing programming skills using the C language. It covers loop control structures, including while, do-while, and for loops, with examples and syntax provided. Additionally, it includes assessment questions and references for further learning.

Uploaded by

avisheshsharma50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views26 pages

Lecture 1 Looping Statements

The document outlines a course on Problem Solving with Programming, focusing on enhancing programming skills using the C language. It covers loop control structures, including while, do-while, and for loops, with examples and syntax provided. Additionally, it includes assessment questions and references for further learning.

Uploaded by

avisheshsharma50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Problem Solving with Programming
Course Code: 20CST111

Loop Control structure in C DISCOVER . LEARN . EMPOWER


1
Problem
solving
with
programmi
ng
Course Objectives
The course aims to provide exposure to problem-solving
through programming.
The course aims to raise the programming skills of
students via logic building capability.
With knowledge of C programming language, students
would be able to model real world problems.

2
Problem
solving with
programming
Course Outcome
CO Title Level
Number
CO1 Identify​situations where computational Understand
methods would be useful.

CO2 Approach the programming tasks using Remember


techniques learnt and write pseudo-code.
CO3 Choose the right data representation formats Understand
based on the requirements of the problem.
CO4 Use the comparisons and limitations of the Understand
various programming constructs and choose
the right one for the task.
3
Contents

4
Looping Statements
I/O Statements

5
Types of
I/OLooping
types statements
• Entry controlled loop or Pre-Test Loop: In the entry
controlled loop or Pre-Test loop, the condition is
checked before we start and at the beginning of each
iteration of the loop. If the condition is true, we
execute body of loop; if the control expression is false,
we terminate the loop.
• Examples: while statement and for statement

• Exit controlled loop or Post-Test Loop: In the exit


controlled loop or Post-Test loop, the condition is
checked after or at the end of each iteration of the
loop. If the condition is true, we repeat body of loop; if
the condition is false, we terminate the loop.
• Examples: do…while statement

6
Loops Definition & Syntax
Statements Definition & Syntax
It is a most basic loop in C programming. It has one control condition, and executes as long
the condition is true. The condition of the loop is tested before the body of the loop is
executed.
Syntax:
while while (condition)
{
statement(s);
Incrementation;
}
C for loops is very similar to a while loops in that it continues to process a block of code
until a statement becomes false, and everything is defined in a single line. The for loop is
also entry -controlled loop.
Syntax:
for for ( init; condition; increment )
{
statement(s);
}

C do while loops are very similar to the while loops, but it always executes the code block at
least once and furthermore as long as the condition remains true.
Syntax:
do
{
Do while statement(s);

}while( condition );
7
While loop
Working:
step1: The loop variable is initialized
with some value and then it has been
tested for the condition.
step2: If the condition returns true
then the statements inside the body
of while loop are executed else
control comes out of the loop.
step3: The value of loop variable is
incremented/decremented then it
has been tested again for the loop
condition.

8
Example
Program to print first 10 natural numbers using while loop.
#include<stdio.h>
int main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
x++;
}
Return 0;
} What will happen if we don’t put {} in while…..

9
1. Program to print first 10 natural numbers using
while loop.

2. Write a C program to print all alphabets from a to z using


while loop. How to display alphabets from a to z using while
loop in C programming.

10
Example

Infinite loop 11
Do-while loop
Working:
1. First we initialize our variables, next
it will enter into the Do While loop.
2. It will execute the group of
statements inside the loop.
3. Next we have to use Increment and
Decrement Operator inside the loop
to increment or decrements the value.
4. Now it will check for the condition.
If the condition is True, then the
statements inside the do while loop
will be executed again. It will continue
the process as long as the condition is
True.
5. If the condition is False then it will
exit from the loop. 12
he following program print numbers between 1 and 100 which are multiple of 3 using the do while loop:

Example
Program to print first 10 natural numbers using do-while loop.
#include<stdio.h>
int main( )
{
int x;
x = 1;
do
{
printf("%d\t", x);
x++;
}while(x <= 10);
return 0;
}
13
For loop Flowchart of continue statement
Working:
Step 1: First initialization happens and
the counter variable gets initialized.
Step 2: In the second step the condition
is checked, where the counter variable
is tested for the given condition, if the
condition returns true then the C
statements inside the body of for loop
gets executed, if the condition returns
false then the for loop gets terminated
and the control comes out of the loop.
Step 3: After successful execution of
statements inside the body of loop, the
counter variable is incremented or
decremented, depending on the
operation (++ or –)

14
Example
Program to print first 10 natural numbers.
#include<stdio.h>
int main( )
{
int x;
for(x=1;x<=10;x++)
{
printf("%d\t", x);
}
return 0;
}

15
Example
Program to find the sum of first n natural numbers.
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
} 16
Example
Program to print factorial of a number
#include<stdio.h>
int main()
{
int fact, i, n;
fact = 1;
printf("Enter the number\t");
scanf("%d" , &n);
for(i = 1; i <= n; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d\n", n , fact);
return 0;
} 17
Program to print factorial of a number

18
Example
Program to print Fibonacci series
#include<stdio.h>
int main()
{
int n, f, f1=-1, f2=1;
printf(" Enter The Number Of Terms:");
scanf("%d",&n);
printf(" The Fibonacci Series is:");
do
{
f=f1+f2;
f1=f2;
f2=f;
printf(" \n %d",f);
n--;
}while(n>=1);
return 0;
}
19
Summary
It continues to process a block of code until a statement
for becomes false, and everything is defined in a single line.

It has one control condition, and executes as long the condition is true.
while

It always executes the code block at least once and furthermore as long as
Do while the condition remains true.

20
FAQ
1. What is the difference between while loop and do-while loop?

21
FAQ
• Q2 Write a program to print table of a number enter by user.
• #include <stdio.h>
• void main()
• {
• int j,n;
• printf("Input the number (Table to be calculated) : ");
• scanf("%d",&n);
• printf("\n");
• for(j=1;j<=10;j++)
• {
• printf("%d X %d = %d \n",n,j,n*j);
• }
• }

22
Assessment Questions
1. Program to check Armstrong number.
2. Write a c program to check a number is prime number or not.
3. Write programs for the following :
a) program to read 10 numbers from keyboard and find their sum and average.
b) to calculate the simple interest.
4. do-while loop terminates when conditional expression returns?
A One
B ZERO
C NON-ZERO
D NONE OF THE ABOVE
5. What will be the output of following program ?
#include <stdio.h>
void main()
{
int cnt=1;
do
{ printf("%d,",cnt);
cnt+=1;
}while(cnt>=10);
printf("\nAfter loop cnt=%d",cnt);
23
printf("\n")
Discussion Forum

Program takes an integer from the user and calculates the


number of digits.
For example: If the user enters 231967, the output of the
program will be 6.

24
References
Book References:
1.http://www2.cs.uregina.ca/~hilder/cs833/Other%20Reference%20Materials/The%20C
%20Programming%20Language.pdf
2.http://www.freebookcentre.net/programming-books-download/The-Basics-of-C-Programming.html

Video Lecture:
1.https://www.studytonight.com/c/loops-in-c.php
2.https://www.youtube.com/watch?v=4gFfGzpDGFw
3.https://spocathon.page/video/lecture-20-implementation-loops-statement-contd
4.https://study.com/academy/lesson/nesting-loops-statements-in-c-programming.html

Websites:
1.https://www.programiz.com/c-programming/c-for-loop
2.https://www.tutorialspoint.com/cprogramming/c_loops.htm
3.https://beginnersbook.com/2014/01/c-loops-examples/
THANK YOU

26

You might also like