KEMBAR78
Control Structures - III | PDF | Control Flow | Programming Paradigms
0% found this document useful (0 votes)
5 views48 pages

Control Structures - III

Uploaded by

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

Control Structures - III

Uploaded by

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

Programming

Fundamentals
Repetition Statements

2
Repetition Structures

3
Loops in C
➢ There are two types of loops in C programming.
1. Entry control loops
▪ The test condition is checked before entering the
main body of the loop
2. Exit control loops
▪ The test condition is evaluated at the end of the
loop body.
Loops

Entry controlled Exit controlled

For Do-
While
while
Types of loops
For loop :
• First initializes, then condition check then executes the body
and at last , the update is done.
While loop :
• First initializes, then condition check then executes the body
and updating can be inside the body.
Do-while :
• First executes the body and then the condition check is
done.
For loop
For loop
➢A repetition control structure that allows programmers to
write a loop that will be executed a specific number of times.

➢Enables to perform n number of steps together in a single


line.
For loop - Syntax

for(initialize expression; test expression; update expression)


{
// body of the loop
}
Initialize expression :
• Assign a loop variable / counter (starting point)
Test expression :
• Test conditions are performed if the condition evaluates
to true then the loop body will be executed and then
update of the loop variable is done.
• If the test expression becomes false then the control will
exit from the loop. (Ending point)
Update expression :
• After execution of the loop body loop variable is
updated by some value whether incrementing ,
decrementing , multiplying or dividing any value.
Example

for(int count=1; count<=10; count++)


{
// body of the loop
}
Example
Print “Hello World” ten times.
Example - Code
#include<stdio.h>
int main()
{
for(int count=1;count<=10;count++)
{
printf("Hello World\n");
}
return 0;

}
Exercise
Write a C program to print numbers from 1-10.
Exercise -Code
#include<stdio.h>
int main()
{

for(int count=1;count<=10;count++)
{
printf("%d\n",count);
}
return 0;

}
Exercise 2
Write a C program to print numbers from 20-1.
Exercise 2 - Code
#include<stdio.h>
int main()
{

for(int count=20;count>=1;count--)
{
printf("%d\n",count);
}
return 0;

}
Exercise 3
Write a C program to accept 10 numbers from the user and
calculate the total and average.
Exercise 3 - Code
#include<stdio.h>
int main()
{
int num,tot=0;
float avg;
for(int count=1;count<=10;count++)
{
printf("Enter a number :");
scanf("%d",&num);
tot=tot+num;
}
avg=(float)tot/10;
printf("The total is %d\n",tot);
printf("The average is %.2f\n",avg);
}
Exercise 4
Write a C program to accept 20 numbers from the user and
display the count of odd numbers and even numbers entered
by the user.
#include<stdio.h>
Exercise 4 -Code
int main()
{
int num,o_count=0,e_count=0;
for(int count=1;count<=20;count++)
{
printf("Enter a number:");
scanf("%d",&num);

if(num%2==0)
{
e_count=e_count+1;
}
else
{
o_count=o_count+1;
}
}
printf("the total of even numbers is %d\n",e_count);
printf("the total of odd numbers is %d\n",o_count);
return 0;
}
While loop
While loop
➢An entry-controlled loop.

➢Used to iterate a part of a code while the given condition


remains true.
While loop – Functional illustration
While loop
start

False While loop


Condition
ends

True

Execute loop
body
While loop

Syntax :
while ( test expression ) //ending point
{
//body of the loop
}
Example

Print “I like coding” statement five


times.
Example
#include<stdio.h>
int main()
{
int count=1;
while(count<=5)
{
printf("Hello World\n");
count++;
}
return 0;
}
Exercise
Write a C program to print numbers from 1-10 using while
loop.
Exercise -Code
#include<stdio.h>
int main()
{
int count=1;
while(count<=10)
{
printf("%d\n",count);
count++;
}
return 0;
}
Exercise
Write a C program to display the total of the numbers from
50-70 using while loop.
Exercise
#include<stdio.h>
int main()
{
int count=50,tot=0;
while(count<=70)
{
printf("%d\n",count);
tot=tot+count;
count++;
}
printf("The total is %d\n",tot);
}
Exercise 3
Write a C program to accept 10 numbers from the user and
display the count of positive numbers and negative numbers
entered by the user.
Exercise 3
#include<stdio.h>
int main()
{
int count=1,num,p_count=0,n_count=0;
while(count<=20)
{
printf("Enter a number :");
scanf("%d",&num);
if(num>0)
{
p_count=p_count+1;
}
else
{
n_count=n_count+1;
}
count++;
}
printf("The positive count is %d\n",p_count);
printf("The negative count is %d\n",n_count);
return 0;
}
Exercise 4
Write a C program to accept integers from the user until user
enters -1.
Exercise 4 - Code
#include<stdio.h>
int main()
{
int num;
while(num!=-1)
{
printf("Enter a number :");
scanf("%d",&num);
}
return 0;
}
Exercise 5
Write a C program to accept integers from the user until user
enters zero (0).Calculate the total of the entered integers and
display the total.
Exercise 5
#include<stdio.h>
int main()
{
int num,tot=0;
while(num!=0)
{
printf("Enter a number :");
scanf("%d",&num);
tot=tot+num;
}
printf("The total is %d\n",tot);
return 0;
}
do-while loop
do-while loop
➢Used to repeat some part of the code till the given condition
is fulfilled.

➢An exit-controlled loop.


do-while loop – Functional illustration
Do-While
loop start

Execute loop False Do-While


body loop ends

True

Condition
do-while loop

Syntax :
do
{
//body of the loop
}
while(test condition);
Example

Write a C program to print numbers


from 20 to 50 using a do-while loop.
Example
#include<stdio.h>
int main()
{
int count=20;
do
{
printf("%d\n",count);
count++;
}while(count<=50);
return 0;
}
Exercise 1
Write a C program to print numbers from 50-100 in
descending order.
Exercise 1 - Code
#include<stdio.h>
int main()
{
int count=100;
do
{
printf("%d\n",count);
count--;
}while(count>=50);
return 0;
}
Exercise 2
Write a C program to accept integers from the user until user
enters -1.Calculate the total of the entered integers and
display the total.
Exercise 2- Code
#include<stdio.h>
int main()
{
int num,tot=0;
do
{
printf("Enter a number :");
scanf("%d",&num);
tot=tot+num;
}while(num!=-1);
printf("The total is %d\n",tot);
return 0;
}
Repetition Structures
Any questions before finish?
Are you sure?
Thank you for listening.

48

You might also like