0% found this document useful (0 votes)
2 views

8.0 Control Structure

The document covers control structures in programming, focusing on algorithm development, structured programming, and various control statements including if, if-else, switch, while, do-while, and for loops. It explains how to use these structures to organize solutions to problems and provides examples of their implementation. Additionally, it discusses the use of break and continue statements within loops.

Uploaded by

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

8.0 Control Structure

The document covers control structures in programming, focusing on algorithm development, structured programming, and various control statements including if, if-else, switch, while, do-while, and for loops. It explains how to use these structures to organize solutions to problems and provides examples of their implementation. Additionally, it discusses the use of break and continue statements within loops.

Uploaded by

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

PROGRAMMING LANGUAGE

1 [EEE]
Lecture 8: Control Structure
M. Arifur Rahman
Algorithm Development
• So far, we considered very simple programs
(read, compute, print)
• Top-down Design
• Start from the big picture
• Use a process called divide-and-conquer
• Keep dividing the problem until steps are detailed enough to convert to
a program
• Refinement with Pseudo-code (English like statements) and Flowchart
(diagram, graph)

2
Pseudo-code Notation and Flowchart
Symbols

3
Structured Programming
Use simple control structures to organize the
solution to a problem

Sequence
no yes

Selection

no
Repetition
yes

4
Selection Statements
if
if else
switch

5
if statement
if(Boolean expression)
statement; /* single statement */

if(Boolean expression) {
/* more than one statement */
statement1;

statement n;
}
6
if statement - examples
• if (x > 0)
k++;
• if(x > 0) {
x = sqrt(x);
k++;
}
• if(x > 0) /* a common mistake */
x = sqrt(x);
k++;

7
if else statement
if(Boolean expression)
statement;
else
statement;

if(Boolean expression) {
statement block
} else {
statement block
}
8
The switch Statement
A cascaded if statement can be used to
compare an expression against a series of
values:
if (grade == 4)
printf("Excellent");
else if (grade == 3)
printf("Good");
else if (grade == 2)
printf("Average");
else if (grade == 1)
printf("Poor");
else if (grade == 0)
printf("Failing");
else
printf("Illegal grade"); 9
The switch Statement
The switch statement is an alternative:
switch (grade) {
case 4: printf("Excellent");
break;
case 3: printf("Good");
break;
case 2: printf("Average");
break;
case 1: printf("Poor");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
10
The switch Statement
• A switch statement may be easier to read
than a cascaded if statement.
• switch statements are often faster than if
statements.
• Most common form of the switch
statement:
switch ( expression ) {
case constant-expression : statements

case constant-expression : statements
default : statements
} 11
The switch Statement

• The word switch must be followed by an


integer expression—the controlling
expression—in parentheses.
• Characters are treated as integers in C and
thus can be tested in switch statements.
• Floating-point numbers and strings don’t
qualify, however.

12
The switch Statement
• Each case begins with a label of the form
case constant-expression :
• A constant expression is much like an
ordinary expression except that it can’t
contain variables or function calls.
5 is a constant expression, and 5 + 10 is a
constant expression, but n + 10 isn’t a constant
expression (unless n is a macro that represents a
constant).
• The constant expression in a case label
must evaluate to an integer (characters are
acceptable).
13
The switch Statement

• After each case label comes any number of


statements.
• No braces are required around the
statements.
• The last statement in each group is
normally break.

14
The switch Statement
• Duplicate case labels aren’t allowed.
• The order of the cases doesn’t matter, and the
default case doesn’t need to come last.
• Several case labels may precede a group of
statements:
switch (grade) {
case 4:
case 3:
case 2:
case 1: printf("Passing");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
15
The switch Statement
• To save space, several case labels can be put
on the same line:
switch (grade) {
case 4: case 3: case 2: case 1:
printf("Passing");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
• If the default case is missing and the
controlling expression’s value doesn’t match
any case label, control passes to the next
statement after the switch. 16
The Role of the break
Statement
• Executing a break statement causes the
program to “break” out of the switch
statement; execution continues at the next
statement after the switch.
• The switch statement is really a form of
“computed jump.”
• When the controlling expression is evaluated,
control jumps to the case label matching the
value of the switch expression.
• A case label is nothing more than a marker
indicating a position within the switch.

17
The Role of the break
Statement
• Without break (or some other jump
statement) at the end of a case, control will
flow into the next case.
• Example:
switch (grade) {
case 4: printf("Excellent");
case 3: printf("Good");
case 2: printf("Average");
case 1: printf("Poor");
case 0: printf("Failing");
default: printf("Illegal grade");
}
• If the value of grade is 3, the message
printed is 18
The Role of the break
Statement
• Omitting break is sometimes done
intentionally, but it’s usually just an
oversight.
• It’s a good idea to point out deliberate
omissions of break:
switch (grade) {
case 4: case 3: case 2: case 1:
num_passing++;
/* FALL THROUGH */
case 0: total_grades++;
break;
}
• Although the last case never needs a break
statement, including one makes it easy to
add cases in the future. 19
Loop (Repetition) Structures
Problem: Conversion table
degrees  radians
Degrees to Radians
0 0.000000
10 0.174533
20 0.349066
30 0.523599

340 5.934120
radians = degrees * PI / 180;
350 6.108653
360 6.283186
21
#include <stdio.h>
#define PI 3.141593

Sequential Solution int main(void)


{
int degrees=0;
double radians;

degrees = ??? printf("Degrees to Radians \n");


radians = degrees*PI/180;
degrees = 0;
printf("%d %f \n", degrees,
radians = degrees*PI/180;
radians); printf("%d %f \n", degrees, radians);

degrees = 10;
radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);

degrees = 20;
Not a good solution radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);

degrees = 360;
radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);
}
22
Loop Solution #include <stdio.h>
#define PI 3.141593

int main(void)
{
int degrees=0;
degrees = ??? double radians;
radians = degrees*PI/180;
printf(“%d %f \n", degrees, printf("Degrees to Radians \n");
radians);
while (degrees <= 360) {

radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);
degrees += 10;
}
}

23
Loop (Repetition) Structures
• while statement
• do while statement
• for statement
• Two new statements used with loops
• break and continue

24
while statement
while(expression)
statement;

while(expression) {
statement;
statement;
.
}

25
The while Control Structure

Example:
x = 1; i = 2;
while (i <= 9) {
x = x * i;
i = i + 1;
} i <= 9
true
x = x * i; i = i + 1;

false

26
Example
#include <stdio.h>
#define PI 3.141593

int main(void)
{
int degrees=0;
double radians;

printf("Degrees to Radians \n");


while (degrees <= 360)
{
radians = degrees*PI/180;
printf("%6i %9.6f \n", degrees,
radians);
degrees += 10;
}

return 0 ;
27
}
do while
do
statement;
while(expression);

do {
statement1;
statement2;
.
} while(expression);

#note - the expression is tested after the statement(s)


are executed, so statements are executed at least once
28
The do…while Repetition Statement

Flowchart of the do…while


repetition statement
action(s)

true
condition

false

29
Example
#include <stdio.h>
#define PI 3.141593

int main(void)
{
int degrees=0;
double radians;

printf("Degrees to Radians \n");


do
{
radians = degrees*PI/180;
printf("%6i %9.6f \n",degrees,radians);
degrees += 10;
} while (degrees <= 360);
return 0;
}

30
for statement
for(initialization; test; increment or decrement)
statement;

for(initialization; test; increment or decrement)


{
statement;
statement;
.
}
31
Example
#include <stdio.h>
#define PI 3.141593

int main(void)
{
int degrees;
double radians;

printf("Degrees to Radians \n");


for (degrees=0; degrees<=360; degrees+=10)
{
radians = degrees*PI/180;
printf("%6i %9.6f \n", degrees, radians);
}
return 0;
} 32
The for Repetition Statement

33
The for Repetition Statement
• For loops can usually be rewritten as while loops:
initialization;
while ( loopContinuationTest ) {
statement;
increment;
}
• Initialization and increment
• Can be more than one, comma-separated lists of statements
• Can even add the counter variable declaration in initialization
• Example:
for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );

34
Example
• What will be the output of the following program, also show how
values of variables change in the memory?
int sum1, sum2, k;
sum1 = 0; 0 2 6 sum1
sum2 = 0;
for( k = 1; k < 5; k++) { 0 1 4 sum2

if( k % 2 == 0) 1 2 3 4 5 k
sum1 =sum1 + k;
else
sum2 = sum2 + k; sum1 is 6
} sum2 is 4
printf(“sum1 is %d\n”, sum1);
printf(“sum2 is %d\n”, sum2);
35
break statement
break;
terminates loop
execution continues with the first statement following the loop

sum = 0;
for (k=1; k<=5; k++) {
scanf(“%lf”,&x);
if (x > 10.0)
break;
sum +=x;
}
printf(“Sum = %f \n”,sum);

36
continue statement
continue;
forces next iteration of the loop, skipping any remaining statements in the
loop

sum = 0;
for (k=1; k<=5; k++) {
scanf(“%lf”,&x);
if (x > 10.0)
continue;
sum +=x;
}
printf(“Sum = %f \n”,sum);
37
Exercise
What is the output of the following
program?
for (i=1; i<=5; i++) { Output

for (j=1; j<=4; j++){ ****


printf(“*”); ****
****
} ****
printf(“\n”); ****
}

38
End of
Lecture

39

You might also like