0% found this document useful (0 votes)
11 views58 pages

C Programming Control Flow: Loops & Examples

The document provides an overview of control flow in C programming, focusing on looping structures such as while, for, and do-while loops. It includes examples of using break and continue statements, as well as problems and solutions related to loops and their execution. Additionally, it discusses the use of goto statements and their implications in code readability and maintenance.

Uploaded by

Pranav Gupta
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)
11 views58 pages

C Programming Control Flow: Loops & Examples

The document provides an overview of control flow in C programming, focusing on looping structures such as while, for, and do-while loops. It includes examples of using break and continue statements, as well as problems and solutions related to loops and their execution. Additionally, it discusses the use of goto statements and their implications in code readability and maintenance.

Uploaded by

Pranav Gupta
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

Control Flow:

Looping

1 01/19/26
2 01/19/26
3 01/19/26
4 01/19/26
5 01/19/26
6 01/19/26
7 01/19/26
8 01/19/26
9 01/19/26
10 01/19/26
11 01/19/26
12 01/19/26
int main()
{
int TR = 3;
int row=1;
while(row<=3){
printf(“* * * * *\n”);
row++;
}
13 01/19/26
}
14 01/19/26
15 01/19/26
do
{
printf(“ press 1 for addition\n”);
printf(“ press 2 for subtraction\n”);

printf(“ press 9 to repeat the menu\n”);


printf(“enter ur choice”);
scanf(“%d”, &ch);
if (ch == 1)
{ addtion}
}while(ch == 9);
16 01/19/26
17 01/19/26
18 01/19/26
19 01/19/26
20 01/19/26
Problem-1
#include <stdio.h>
void main()
{
int k = 0;
for (k < 3; k++)
printf("Hello");
}
a)Compile time error
b) Hello is printed thrice
c) Nothing
d) Varies
21 01/19/26
Problem-2:How many times this
loop will execute?
Output: Finite times
#include<stdio.h>
int main(){
Explanation:
char c=125; If we will increment the
do char variable c it will
increment as:
printf("%d ",c);
while(c++); 125, 126,127,-128,-127,126 .
return 0; . . . , 3, 2, 1, 0
}
When variable c = 0 then
loop will terminate.

22 01/19/26
Entry controlled loop :In Entry controlled loop
the test condition is checked first and if that
condition is true than the block of statement in the
loop body will be executed
Ex:while and for loops

Exit controlled loop :exit controlled loop the


body of loop will be executed first and at the end
the test condition is checked,if condition is
satisfied than body of loop will be executed again.
In Exit controlled loop at least once the statement
will be executed.
Ex:do-while

23 01/19/26
24 01/19/26
25 01/19/26
26 01/19/26
27 01/19/26
Problem 1 Solution
void main(){
int n,sum=0;
for (n=1001; n<=2000; n=n+2) {
sum=sum+n;
}
printf("\n %d“,sum);

28 01/19/26
Problem 2 Solution
void main(){
int n,sum=0;
for (n=1000; n<=10000; n=n+1) {
if(n%17==0)
sum=sum+n;
}
printf("\n %d“,sum);

29 01/19/26
Problem 3 Solution
#include<stdio.h>
void main(){
int row,col;
for (row=1; row<=5; row++) {
for (col=1; col<=5; col++) {
if(row==1 || row==5 || col==1 || col==5)
printf("*");
else
printf(" ");

}
printf("\n");
}
}

30 01/19/26
31 01/19/26
32 01/19/26
33 01/19/26
34 01/19/26
35 01/19/26
The break Statement
(Cont…)

36 01/19/26
The break Statement
Example
#include <stdio.h>
int main () {
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
if( a > 15) /* terminate the loop using break
statement */
break;
}
return 0;
}

37 01/19/26
38 01/19/26
The continue Statement
(cont…)

39 01/19/26
The continue Statement
(cont…)
//program to demonstrate the working of continue statement in C
programming
# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i)
{
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0) continue; /*In this program, when num equals to zero, it
skips the statement product*=num and
continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}
40 01/19/26
41 01/19/26
42 01/19/26
43 01/19/26
Example:Decimal to Binary
Convesion
void main()
{
int dec,rem,i=1;
long int bin=0;
printf("Enter the decimal number : ");
scanf("%d",&dec);
while(dec>0){
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The binary number is %ld",bin);

}
44 01/19/26
C program to add two numbers
repeatedly
#include<stdio.h>
void main() {
int a, b, c;
char ch;
while(1) {
printf("Enter values of a and b\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("a + b = %d\n", c);
printf("Do you wish to add more numbers(y/n)\n");
scanf(" %c",&ch);
if ( ch == 'y' || ch == 'Y' )
continue;
else
break;
` }
}
45 01/19/26
compute x to the power n
using while loop in C
Programming
void main(){
int count, n;
float x, y;
printf("Enter the values of x and n : ");
scanf("%f %d", &x, &n);
y = 1.0;
count = 1; /* Initialisation */
/* LOOP BEGINs */
while ( count <= n){ /* Testing */
y = y*x;
count++; /* Incrementing */
} /* END OF LOOP */

printf("\nx = %f; n = %d; x to power n = %f\


n",x,n,y);
}
46 01/19/26
compute x to the power n
using for loop in C
Programming
void main(){
int count, n;
float x, y;
printf("Enter the values of x and n : ");
scanf("%f %d", &x, &n);
for (y = 1.0, count = 1; count <= n;
count++){
y = y*x;
}

printf("\nx = %f; n = %d; x to power n =


%f\n",x,n,y);
}
47 01/19/26
48 01/19/26
49 01/19/26
50 01/19/26
51 01/19/26
Check given number is prime
number or not
prime number : An integer greater than one is
called a prime number if its only positive divisors
(factors) are one and itself.
For example: 5;its divisors are 1 and 5.
Note: 2 is only even prime number.
void main(){
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
52 01/19/26
}
Print prime numbers between
1 to 100
int main(){
int num,i,count;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ,",num);
}

return 0;
}

53 01/19/26
Goto Statements
Goto changes the flow of execution without checking any
condition

54 01/19/26
Use of Goto Statements
 Nevertheless, there are a few situations where gotos may find a place.
 The most common use is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once.
 The break statement cannot be used directly since it only exits from the innermost loop. Thus:
for ( ... )
for ( ... ) {
...
if (disaster)
goto error;
}
...
error: // goto will transfer the control to this statement.

55 01/19/26
Some facts about goto
A label has the same form as a variable name,
and is followed by a colon.
It can be attached to any statement in the same
function as the goto.
The scope of a label is the entire function.
With a few exceptions like previous example,
code that uses goto statements is generally
harder to understand and its very difficult to
maintain as compare to the code without gotos.
Due to above reason goto is rarely used in
programming.

56 01/19/26
Problem- 1 What would be the output from the
following C code segment?
x=10; o/p: 10
while(x>5){ 9
printf("%d\n",x); 8
x--; 7
6
}
2. What would be printed from the following C
code segment?
for(x=1;x<=5;x++){
o/p: 1
for(y=1;y<=x;y++)
2 2
printf("%d\t",x);
3 3 3
printf("\n");
4 4 4 4
} 5 5 5 5 5
57 01/19/26
Problem-1: What will be the
output?
int main(){
int i,j=2;
for(i=0;j>=0,i<=5;i++)
{
printf("%d ",i+j);
j--;
}
return 0;
}

O/p: 2 2 2 2 2 2

58 01/19/26

You might also like