# Programming for Problem Solving (3110003)

BE | Semester-1   Winter-2019 | 07-01-2020

## Q2) (a)

#### Break Statment

• Sometimes it is required to quit the loop as soon as certain conditions occur.
• A break statement is used to jump out of a loop. It provides you to early exit from for, while, do..while loop constructs.
Example: Read and sum numbers until -1 is entered.
#include <stdio.h>
void main()
{
int i, sum=0;
printf("Enter the number -1 to end\n");
while(1) {
scanf("%d", &i);
if(i == -1)
break;
sum += i;
}
printf("Sum = %d\n", sum);
}

Output:
Enter the number -1 to end
1
2
3
-1
Sum = 6

#### Continue Statment

• The continue statement can be used to skip the rest of the body of an iterative loop.
• The continue statement tells the compiler, “SKIP THE FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT ITERATION”.
Example:
#include <stdio.h>
void main()
{
int i=1, num, sum=0;
printf("Enter 5 numbers: \n");
for (i = 0; i < 5; i++) {
scanf("%d", &num);
if(num < 0)
continue;
sum += num;
}
printf("Sum = %d\n", sum);
}

Output:
Enter 5 numbers
1
2
3
4
-1
Sum = 10