You are on page 1of 21

Curtin University of Technology

Engineering Programming 100

COMMONWEALTH OF AUSTRALIA

Copyright Regulation 1969

WARNING
This material has been copied and communicated to
you by or on behalf of Curtin University pursuant
to Part VB of the Copyright Act 1968 (the Act)
The material in this communication may be subject
to copyright under the Act. Any further copying or
communication of this material by you may be the
subject of copyright protection under the Act.
Do not remove this notice

Engineering Programming 100

Lecture 5: Control Flow

Semester 1, 2013

Curtin University of Technology

Engineering Programming 100

Last week we discussed

Program design methods


Implementing the design
A case study - speeder.c

This week

Semester 1, 2013

Explore different ways to control code


execution
Explore the development of a program
for a typical engineering program using
some control flow statements

Curtin University of Technology

Engineering Programming 100

Simple program

List of statements executed in order:


int main(void)
{
int x;
int y;
int z;
x = 5;
y = 7;
z = x + y;
printf("z: %d\n",z);
}

1
2
3
4
5
6
7
8
exit

Need to be able to control flow

Choose whether to execute code or


not
Depends on the value of an expression
Two basic methods:
1.
2.

Semester 1, 2013

if then else
case

Curtin University of Technology

Engineering Programming 100

A simple example

Program to check if an integer value is


even or odd
Input Value
k

Is k even?

no

Report
odd

yes
Report
even

Using modulo operation

Semester 1, 2013

Introducing modulo operation modulo


returns the remainder of a division of
two integer values
In C, modulo operation is represented
by % symbol. E.g. 5%3 returns 2.

Curtin University of Technology

Engineering Programming 100

Example

Back to the problem. Implement in C.

#include <stdio.h>
int main(void)
{
int k=101;

// set the value of k here

if((k%2) == 0)

// modulo by 2 returns 0 if
//even and 1 if odd
printf("the number is even\n");
else
printf("the number is odd\n");
return 0;
}

Sliding block example


0

Af 0

Coefficient of Friction
g Acceleration due to Gravity

AS sin( ) g
acceleration component

The program simulates a frictionless block sliding down a


finite length inclined plane. The block begins sliding at time
0. The program prompts the user for the angle of the plane
and the elapsed time. It reports the distance traveled by
the block along the plane from the starting point

Semester 1, 2013

Curtin University of Technology

Engineering Programming 100

Algorithm
1.

2.

3.

4.

5.

6.

Write to the screen a prompt for the angle of


incline
Read from the keyboard a value and store it
in theta
Write to the screen a prompt for the elapsed
time
Read from the keyboard a value and store it
in time
Compute distance using theta and
time
Write to the screen distance in an
understandable form

Distance traveled

at 2
Basic equation: d ut
2

Add in angle of the plane,


gt 2
gravity, and assume start from rest: d
sin

In C:

distance = 0.5 * time * time * g * sin(theta);

Semester 1, 2013

Curtin University of Technology

Engineering Programming 100

C uses radians but we prefer degrees!

Decide to input angle theta in degrees


Convert to radians:

radians

2
degrees
360

In C (alternatives):
theta_2 = 2 * 3.141592 * theta / 360.0;
theta_2 = 3.141592 * theta / 180.0;
theta_2 = PI * theta / 180.0;
theta_2 = conversion * theta;
(Precompute:
conversion = 3.141592 / 180.0;)

Basic code
int main(void)
{
float distance, theta, theta_2, time, mu, conversion;
mu = 0.5;
printf("input the angle of decline in degrees ");
scanf("%f", &theta);
printf("input the time the block has been sliding for in seconds ");
scanf("%f", &time);
conversion = 3.141592 / 180.0;
theta_2 = conversion * theta;
distance = 0.5 * time * time * 9.81 * sin (theta_2);
printf("distance traveled is %f metres\n",distance);
}

code5_1.c

Semester 1, 2013

Curtin University of Technology

Engineering Programming 100

What if negative time is entered?


Need an if statement to control whether to compute
distance

if (time <= 0.0)


{
distance=0.0;
printf("warning, time <= 0.0\n");
}
else
{
conversion = 3.141592 / 180.0;
theta_2 = conversion * theta;
distance = 0.5 * time * time * 9.81 * sin
(theta_2);
}
code5_2.c

Note the use of { and } to show block of code


belonging to the if and else parts.

What if reach the bottom of the


slope?

Assume a variable defining distance


from start to bottom: length
In C:
if (distance > length)
distance = length;

Semester 1, 2013

Curtin University of Technology

Engineering Programming 100

Modified code
int main(void)
{
float distance, theta, theta_2, time, mu, conversion,
length;
mu = 0.5;
length = 50.0;
printf(input the angle of decline in degrees ");
scanf("%f", &theta);
printf("input the time the block has been sliding for
in seconds ");
scanf("%f", &time);

Modified code
if (time < 0.0)
{
distance = 0.0;
printf("warning, time < 0.0\n");
}
else
{
conversion = 3.141592 / 180.0;
theta_2 = conversion * theta;
distance = 0.5 * time * time * 9.81 * sin (theta_2);
if (distance > length)
distance = length;
}
printf("distance traveled is %f\n",distance);
}
code5_3.c

Semester 1, 2013

Curtin University of Technology

Engineering Programming 100

What about friction?


Af cos( ) g

Coefficient of Friction
g Acceleration due to Gravity

AS sin( ) g

The program simulates a block sliding down a finite length


inclined plane. The block begins sliding at time 0. The program
prompts the user for the angle of the plane and the elapsed
time. It reports the distance traveled by the block along the
inclined plane from the starting point given that there is friction
between the block and the inclined plane.

Incorporating friction

Friction retards or prevents movement


Friction is acting up the ramp with
acceleration Af:
A f g cos( )

Modify acceleration:
At As A f g (sin( ) cos( ))

In C:
distance = 0.5 * time * time * 9.81 *
(sin (theta_2) - mu * cos(theta_2));

Semester 1, 2013

10

Curtin University of Technology

Engineering Programming 100

Final code
int main(void)
{
float distance, theta, theta_2, time, mu,
length;
mu = 0.5;
length = 50.0;
printf("input the angle of decline in degrees
");
scanf("%f", &theta);
printf("input the time the block has been
sliding for in seconds ");
scanf("%f", &time);

Final code
if (time < 0.0)
{
distance = 0.0;
printf("warning, time < 0.0\n");
}
else
{
conversion = 3.141592 / 180.0;
theta_2 = conversion * theta;
distance = 0.5 * time * time * 9.81 *
(sin (theta_2) - mu * cos(theta_2));
if (distance > length)
distance = length;
}
printf("distance traveled is %f\n",distance);
}

code5_4.c

Semester 1, 2013

11

Curtin University of Technology

Engineering Programming 100

Relational operators

Use for comparing variables in conditions


==
equal
!=
not equal
> greater than
< less than
>= greater than or equal
<= less than or equal

Example conditions in if statements


if
if
if
if

(x
(y
(z
(x

==
!=
<=
==

3)
35)
27.8)
(y + 1))

Logical operators

Used to combine conditions


Three operators:
&&
||
!

and
or (two vertical bars)
not

Example:

if ((x >= 0.0) && (x <= 1.0)


&& (y >= 0.0) && (y <= 1.0)
&& (z >= 0.0) && (z <= 1.0))
{
printf(the value (x,y,z) is inside the unit
cube\n);
}

Semester 1, 2013

12

Curtin University of Technology

Engineering Programming 100

Types of if statements

Single branch:

if (condition true)
{
/* execute these statements */
}

Test
condition

true

statement

Types of if statements

Dual branch:

if (condition true)
{
/* execute statements I if true */
}
else
{
/* execute statements II if false */
}
Test
condition

true

Statements I

false

Statements II

Semester 1, 2013

13

Curtin University of Technology

Engineering Programming 100

Combinations of if statements
Nested:
if (cond_1 true)
{
if (cond_1_2 true)
{
/* execute these statements */
}
}
true

Test
cond_1

true

Test
cond_1_2

Combinations of if statements
Which if does the else statement belongs to?
if (cond_1 true)
if (cond_1_2 true)
/* execute these statements */
else
/* execute these statements */

Semester 1, 2013

14

Curtin University of Technology

Engineering Programming 100

Combinations of if statements (3)


Use of braces to define the blocks:
if (cond_1 true)
{
if (cond_1_2 true)
{
/* execute statements I*/
}
else
{
/* execute statements II*/
}
Test
Test
}
true
cond_1

true

cond_1_2

Statements I

false

Statements II

Combinations of if statements (3)


if (cond_1 true)
{
if (cond_1_2 true)
{
/* execute statements I*/
}
}
else
{
/* execute statements II */
}
Test
cond_1

true

Test
cond_1_2

true

statement

false

statement

Semester 1, 2013

15

Curtin University of Technology

Engineering Programming 100

Combinations of if statements
Chained:
if (condition_1 true)
{
/* execute statements I*/
}
else if (condition_2 true)
{
/* execute statements II*/
}
else if (condition_3 true)
{
/* execute statements III*/
}
else if

else
{
/* execute statements N*/
}
Note the final else is optional

Test
condition_1

true

Statements I

false
Test
condition_2

true

Statements II

false
Test
condition_3

true

Statements III

false

Test last
condition

true

Statements

false
Statements N

Another example

Problem:
Convert a student mark between 0 and 100
to a grade as below:

Semester 1, 2013

16

Curtin University of Technology

Engineering Programming 100

Use if statements
Example 1
if ((mark >= 0) && (mark <50))
{
printf("Fail\n");
}
else if ((mark >= 50) && (mark <60))
{
printf("Pass\n");
}
else if ((mark >= 60) && (mark < 70))
{
printf("Credit\n");
}
else if ((mark >= 70) && (mark < 80))
{
printf("Distinction\n");
}
else if ((mark >= 80) && (mark <= 100))
{
printf("High Distinction\n");
}

Use if statements
Example 2
if ((mark >= 0) && (mark <50))
printf("Fail\n" );
else if ((mark >= 50) && (mark <60))
printf("Pass\n");
else if ((mark >= 60) && (mark < 70))
printf("Credit\n");
else if ((mark >= 70) && (mark < 80))
printf("Distinction\n");
else if ((mark >= 80) && (mark <= 100))
printf("High Distinction\n");

Semester 1, 2013

17

Curtin University of Technology

Engineering Programming 100

Use if statements
Example 3
int mark_2;
mark_2 = mark / 10;
if ((mark_2 >= 0) && (mark_2 < 5)
printf("Fail\n");
else if (mark_2 == 5)
printf("Pass\n");
else if (mark_2 == 6)
printf("Credit\n");
else if (mark_2 == 7)
printf("Distinction\n");
else if ((mark_2 >= 8) && (mark_2 <= 10))
printf("High Distinction\n");

Switch-Case Statement

The switch statement is used to conditionally


perform statements based on an integer
expression (selector)
switch (selector){
case int-value1: statements1; break;
case int-value2: statements2; break;
case int-value3: statements3; break;
default: statements4;
}

The exact behaviour of the switch statement is


controlled by the case and default commands
Default is executed if no other match is found

Semester 1, 2013

18

Curtin University of Technology

Engineering Programming 100

Switch-Case Statement

Using a switch-case statement in


previous example
int main(void)
{
int mark, mark_2;
printf("Enter a mark between 0 and 100 ");
scanf("%d",&mark);
mark_2 = mark /10;
switch (mark_2)
{
case 0: case 1: case 2: case 3: case 4:
printf("Fail\n");
break;
case 5:
printf("Pass\n");
break;
See http://www.cprogramming.com/tutorial/lesson5.html
http://www.geocities.com/learnprogramming123/Clesson7Beginner.htm

Semester 1, 2013

19

Curtin University of Technology

Engineering Programming 100

Using a switch-case statement in


previous example
case 6:
printf("Credit\n");
break;
case 7:
printf("Distinction\n");
break;
case 8: case 9: case 10:
printf("High Distinction\n");
break;
default:
printf (Invalid mark entered\n");
}
return 0;
}
See http://www.cprogramming.com/tutorial/lesson5.html
http://www.geocities.com/learnprogramming123/Clesson7Beginner.htm

code5_5.c

Today we discussed

Semester 1, 2013

Control flow in a program


Explored different ways to do it
Constructed code using some of the
methods

20

Curtin University of Technology

Engineering Programming 100

Whats next?

Practical
Experiment with different control flow
methods in some programs

Lecture
Repetition

Semester 1, 2013

21

You might also like