Professional Documents
Culture Documents
COMMONWEALTH OF AUSTRALIA
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
Semester 1, 2013
This week
Semester 1, 2013
Simple program
1
2
3
4
5
6
7
8
exit
Semester 1, 2013
if then else
case
A simple example
Is k even?
no
Report
odd
yes
Report
even
Semester 1, 2013
Example
#include <stdio.h>
int main(void)
{
int k=101;
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;
}
Af 0
Coefficient of Friction
g Acceleration due to Gravity
AS sin( ) g
acceleration component
Semester 1, 2013
Algorithm
1.
2.
3.
4.
5.
6.
Distance traveled
at 2
Basic equation: d ut
2
In C:
Semester 1, 2013
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
Semester 1, 2013
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
Coefficient of Friction
g Acceleration due to Gravity
AS sin( ) g
Incorporating friction
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
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
Relational operators
(x
(y
(z
(x
==
!=
<=
==
3)
35)
27.8)
(y + 1))
Logical operators
and
or (two vertical bars)
not
Example:
Semester 1, 2013
12
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
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
true
cond_1_2
Statements I
false
Statements II
true
Test
cond_1_2
true
statement
false
statement
Semester 1, 2013
15
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
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
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
Semester 1, 2013
18
Switch-Case Statement
Semester 1, 2013
19
code5_5.c
Today we discussed
Semester 1, 2013
20
Whats next?
Practical
Experiment with different control flow
methods in some programs
Lecture
Repetition
Semester 1, 2013
21