2 1 Loops
2 1 Loops
Programming in C
Today’s Topics
while(expression)
{
Output ?
EXAMPLE 1:
#include<stdio.h>
void main()
{
int sum=0;
char ch=’0’;
while(ch!=10)
{
printf("(%c, %d)\n",ch,ch);
scanf("%c",&ch);
}
}
Output ?
#include<stdio.h>
void main()
{
int i=0;
while(i<3, i=0, i<1)
{
printf("Loop ");
i++;
}
}
Interesting Evaluation
Output ? Output ?
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i=0; int n,i=0;
while(i<3, i=0, i<1) while(scanf("%d",&n)==1)
{ {
printf("Loop "); printf("END\n");
i++; if(n==0) break;
} }
} }
Infinite Loop
The break and continue Statements
break
• Causes immediate exit from a while, for, do/while or
switch structure
• Program execution continues with the first statement after
the structure
Common uses of the break statement
• Escape early from a loop
• Skip the remainder of a switch structure
The break and continue Statements
Continue
Skips the remaining statements in the body of a while,
for or do/while structure
Proceeds with the next iteration of the loop
while and do/while
Loop-continuation test is evaluated immediately after the
continue statement is executed
for
Increment expression is executed, then the loop-continuation test
is evaluated
Interesting Evaluation
Armstrong Number
#include<stdio.h>
#include<math.h>
Write a program to test a void main()
given number is {
Armstrong or not ?. int n, sum=0, temp;
scanf("%d",&n);
temp=n;
Armstrong number: Sum while(temp !=0)
of cubes of each digit of {
sum = sum + pow((temp%10),3);
the number is equal to temp = temp/10;
number itself (e.g., }
153 = 13 + 53 + 33 ) if(n == sum)
printf("Armstrong Number");
else
printf("Not Armstrong Number");
}
while loop - Example
► while loop:
Input: a12wse4gf320rd
Output: 12 + 4 + 320 =
336
do-while loop
do
{
}while(expression); //semicolon
do {
printf("Welcome to the Menu:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Quit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1':
printf("You selected Option 1.\n");
break;
case '2':
printf("You selected Option 2.\n");
break;
case '3':
printf("Goodbye!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != '3');
goto statement
goto myLabel;
........
myLabel:
statements;
#include <stdio.h>
int main() {
int i = 0;
start:
if (i < 5) {
printf("%d ", i);
i++;
goto start; // Jump back to the 'start' label
}
return 0;
}
► for loop
► nested for loop
for loop
• Sentinel-controlled repetition
– Indefinite repetition
– Used when number of repetitions not
known
– Sentinel value indicates "end of data"
© 2000 Prentice H
28
int main() {
int num, sum = 0;
return 0;
}
© 2000 Prentice H
Terminate the program in our choice
What this program will do ?
#include<stdio.h>
int main()
{
char ch; int i;
printf("Enter the sequence of characters\n");
scanf("%c",&ch);
for( ;ch!=’e’&&ch!=’E’; )
{
switch(ch<=’9’ && ch>=’0’)
{
case 1: printf("%c",ch);
scanf("%c",&ch);
break;
case 0: scanf("%c",&ch);
break;
}
}
}
Nested loops
What this program will do ?
#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=4;j++)
printf("%d",j);
printf("\n");
}
}
Visualize code
Nested while loop
#include <stdio.h>
int main() {
int i = 0;
while (i <= 3) {
int j = 0;
while (j <= 4) {
printf("%d", j);
j++;
}
printf("\n");
i++;
}
return 0;
}
• Loops can be nested multiple times.
• Any combination of loops like for, while, and do while
can be nested together.
Printing pyramid 1
#include <stdio.h>
int main() {
int rows, i, j;
return 0;
}
Printing pyramid 2
#include <stdio.h>
int main() {
int rows, i, j;
return 0;
}
Pyramid Printing – Another logic
#include<stdio.h>
void main()
{
int i,j,k,n;
printf("Enter a number: ");
scanf("%d",&n);
k=2*n-2;
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
printf(" ");
k=k-1;
for(j=0;j<=i;j++)
printf("* ");
printf("\n");
}
}
Homework
Pyramid Printing
Problems
• Write a program to read a number and find the sum of its
individual digits repeatedly till the result is a single digit.