0% found this document useful (0 votes)
4 views19 pages

Lecture 3

Uploaded by

soumendhar393
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)
4 views19 pages

Lecture 3

Uploaded by

soumendhar393
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/ 19

Structured Programming Language

Mumtahina Ahmed

Department of Computer Science


School of Science, Engineering & Technology

Lecture 03
C Flow Control
Program in CValues and VariablesExpressionsStandard Input/Output

C break and continue

C break
The break statement ends the loop immediately when it is encountered. Its syntax is:

The break statement is almost always used with if...else statement inside the loop.
Program in CValues and VariablesExpressionsStandard Input/Output

C break and continue

How break statement works?


Program in CValues and VariablesExpressionsStandard Input/Output

Example 1: break statement


// If the user enters a negative number, the loop terminates
#include <stdio.h>
int main() {
int i; double number, sum = 0.0;
for (i = 1; i <= 10; i++) {
printf("Enter n%d: ", i);
scanf("%lf", &number); // if the user enters a negative number, break the loop
if (number < 0.0) {
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0; }
Program in CValues and VariablesExpressionsStandard Input/Output

Output

This program calculates the sum of a maximum of 10


numbers. Why a maximum of 10 numbers? It's because if the
user enters a negative number, the break statement is
executed. This will end the for loop, and the sum is displayed.
Program in CValues and VariablesExpressionsStandard Input/Output

C break and continue

C continue
The continue statement skips the current iteration of the loop and continues with the
next iteration. Its syntax is:

The continue statement is almost always used with the if...else statement.
Program in CValues and VariablesExpressionsStandard Input/Output

C break and continue


How continue statement works?
Program in CValues and VariablesExpressionsStandard Input/Output

Example 2: continue statement


// If the user enters a negative number, the loop terminates
#include <stdio.h>
int main() {
int i; double number, sum = 0.0;
for (i = 1; i <= 10; i++) {
printf("Enter n%d: ", i);
scanf("%lf", &number); // if the user enters a negative number, break the loop
if (number < 0.0) {
continue;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0; }
Program in CValues and VariablesExpressionsStandard Input/Output

Output

In this program, when the user enters a positive number, the


sum is calculated using sum += number; statement.
When the user enters a negative number,
the continue statement is executed and it skips the negative
number from the calculation.
Program in CValues and VariablesExpressionsStandard Input/Output

C goto Statement

C goto statement
The goto statement allows us to transfer control of the program to the specified label.

The label is an identifier. When the goto statement is encountered, the control of the
program jumps to label: and starts executing the code.
Program in CValues and VariablesExpressionsStandard Input/Output

Example: goto Statement


// Calculate the sum and average of positive numbers
// If the user enters a negative number, the sum and average are displayed.
#include <stdio.h>
int main()
{
const int maxInput = 10; int i;
double number, average, sum = 0.0;
for (i = 1; i <= maxInput; ++i)
{
printf("%d. Enter a number: ", i);
scanf("%lf", &number); // go to jump if the user enters a negative number
if (number < 0.0)
{
goto jump;
}
Program in CValues and VariablesExpressionsStandard Input/Output

Example: goto Statement


sum += number;
}
jump: average = sum / (i - 1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}
Program in CValues and VariablesExpressionsStandard Input/Output

C switch Statement

The switch statement allows us to execute one code block among many alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of
the switch statement is much easier to read and write.
Program in CValues and VariablesExpressionsStandard Input/Output

How does the switch statement work?


1. The expression is evaluated once and compared with the values of
each case label.
2. If there is a match, the corresponding statements after the matching label
are executed.
3. For example, if the value of the expression is equal to constant2,
statements after case constant2: are executed until break is encountered.
4. If there is no match, the default statements are executed.

Notes:

1. If we do not use the break statement, all statements after the matching
label are also executed.
2. The default clause inside the switch statement is optional.
Program in CValues and VariablesExpressionsStandard Input/Output

switch Statement Flowchart


1. The expression is evaluated once and compared with the values of
each case label.
2. If there is a match, the corresponding statements after the matching label
are executed.
3. For example, if the value of the expression is equal to constant2,
statements after case constant2: are executed until break is encountered.
4. If there is no match, the default statements are executed.

Notes:

1. If we do not use the break statement, all statements after the matching
label are also executed.
2. The default clause inside the switch statement is optional.
Program in CValues and VariablesExpressionsStandard Input/Output

switch Statement Flowchart


Program in CValues and VariablesExpressionsStandard Input/Output

Example: Simple Calculator


// Program to create a simple calculator
#include <stdio.h>
int main()
{
char operation;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
Program in CValues and VariablesExpressionsStandard Input/Output

Example: Simple Calculator


case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break; // operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}
return 0;
}
Program in CValues and VariablesExpressionsStandard Input/Output

Thank You!

You might also like