2012年10月13日 星期六

C program flow control - a key technique to plan solution approach

IERG1001 Information Engineering in Society
Blog Assigment 2 

Until week 5 , 5 chapters had been taught in the CSCI1110.Let me summarize what I have learnt from chapter 4 to 5.

Part 1

1.1 Contents of the lectures

In chapter 4 and 5, I have learnt some fundamental concepts about C program flow control. What is C program flow control? In order to write a program to solve a particular problem, it is important for us to have a thorough understanding of the problem and how to solve the problem. C program flow control is a key technique for programmer to plan solution approach in a systematic way. Therefore, it is very important for a good programmer to acquire a good skill of using flow control in C program. In chapter 4 and 5, the techniques of C program flow control I had learnt include (1) operators, (2) selection statement, (3) repetition statement.

(1)   Operators :

A.      Relational Expressions :
<    >    <=    >=

A relational expression is an expression that involves the use of one or more relational operators.
Relational expressions yield either true or false.
"True" is represented by integer 1.
"False" is represented by integer 0.
All relational operators are binary operators, taking two operands.



B.      Equality Operators :
== (Equal-to)           != (Not-Equal-to)

The equality operators are binary operators, taking two operands.
An equality expression yields either 1 (true) or 0 (false).
Examples,
9 == 10
c == 'A'
'A' != 'B'
k != -2
area != (length * length)

C.      Logical Operators:
! (Not)    && (And)   || (Or)

Operator ! is unary, taking only one operand.
Operators && and || are binary, taking two operands.
Logical expressions yield either 1 (true) or 0 (false).

If the operand expression has the value zero, it will be regarded as false when evaluating the logical expression.
If the operand expression has a non-zero value, it will be regarded as true when evaluating the logical expression.

In summary,
Relational operators
less than <
greater than >
less than or equal to <=
greater than or equal to >=
Equality operators
equal to      ==
not equal to   !=
Logical operators

(unary) negation !
logical and    &&
logical or      ||

(2)   Selection statement

A.      The if statement:

if (expr)
statement;
next_statement;

If expr is true (nonzero), then statement is executed.
Otherwise statement is skipped, and control is passed to next_statement.

Example :

Input:
score = 80;
if (score >= 50)
printf ("Congratulations!\n");
printf ("Your score is %d.\n", score);

Output:
Congratulations!
Your score is 80.

B.      The if-else Statement

if (expr)
statement_1;
else
statement_2;
next_statement;


If expr is true (nonzero), then statement_1 is executed.
Otherwise statement_1 is skipped and statement_2 is executed.
In both cases, control is then passed to next_statement.

Example :

Input:
if (score >= 50) {
grade = 'P';
printf ("Congratulations!\n");
} else
grade = 'F';
printf ("Your score is %d.\n", score);
printf ("Your grade is %c.\n", grade);

Output:
When score is 80:
Congratulations!
Your score is 80.
Your grade is P.

When score is 40:
Your score is 40.
Your grade is F.

C.      The Conditional Operator ?:

expr1 ? expr2 : expr3

First, expr1 is evaluated.
If true, then expr2 is evaluated and the value of expr2 is taken as the value of the whole expression.
Else, expr3 is evaluated and the value of expr3 is taken as the value of the whole expression.
Similar to the if-else statement.

Example:

printf("min=%d", (x < y) ? (min = x) : (min = y));
is equal to
if (x < y) min = x;
else min = y;
printf("min=%d", min);

(3)   Repetition statement :

A.      The while Statement :

while (expr)
statement;
next_statement;


As long as (i.e. while) expr is true (non-zero), repeatedly execute statement.
When expr evaluates to false (zero), execute next_statement.

Example: calculate 1 + 2 + … + 10
Input:
int i = 1, sum = 0;
while (i <= 10) {  /* count i from 1 to 10 */
sum += i;
i++; }
printf("%d\n", sum);

output:
55

B.      The for Statement

for (expr1; expr2; expr3)
statement;
next_statement;

Execution Steps:
1.expr1 is evaluated.
2.expr2 is evaluated.
   if true (non-zero),
(a) statement is executed.
(b) expr3 is executed.
(c) goto step (2) again.
if false (zero), next_statement is executed.

 Video about for statement:



1.2 Learning Process:

In order to acquire the techniques of using (1) operators, (2) selection statement, (3) repetition statement, I use different methods to let myself to understanding the meaning and uses of the techniques.

(1)   operators:

When I learnt about the operators in C program, I feel quite confused about what are the various operators stand for and how to use them. However, I remembered that I actually had learnt different operators in Mathematic in primary and secondary school. Therefore, I made a table which compares the operators in C program with the operators in Mathematics.

C program
Mathematics
==
==
!=
> 
> 
< 
< 
>=
<=
!
&&
||

By this method, I found that I can understand the operators in C program easily now.
C program is actually quite similar to Mathematic in the aspect of logic, it is an effective method for us to learn C program together with our Mathematic knowledges.

(2)   selection statement

A conditional statement lets us choose which statement will be executed next.
It is a decision-making statement. To understand the logic of using selection statement. I think the most suitable method is to draw a logic-flow diagram.

If statement :


If-else statement :


By this method, I found that I can understand how the selection statement is used in C program easily now. I think logic flow diagram is a very useful tool for us to learn how to write C program. If anyone meets a difficult in the process of program design, I would suggest he/she to draw a logic flow diagram.


(3)   Repetition statement

Similar to selection statement , I used logic flow diagram to understand the use of repetition statement.

While statement :


For statement :


In short, during the learning process, I have used my Mathematics knowledge and logic-flow diagram to learn C program flow control. I found that they are very useful for me to understand how different techniques can be used.

Part 2

2.1 A interesting topic in the lesson –selection statement

A brief description about the topics :

As mentioned before, A selection statement lets us choose which statement will be executed next.It is a decision-making statement.
The below code are the if and if-else statement examples at the lecture note:
If:

If-else :


2.2 My opinion, commends, ideas, and further information about selection statement :

After seeing the above example at the lecture note,I have a problems in my mind.
If statement is a single-selection statement and if-else statement is a double-selection statement. But is we are going to handle a multiple-selection decision making.What statement should we use?

After searching, I find that C program provides the switch multiple-selection statement to handle a multiple-selection decision making.

By this statement, we can set up a series of condition in which a variable or expression is tested separately for each of the constant integral values it may assume and different actions are taken.


More about switch statement :


From this example,I understand that knowledge we learnt from the lesson is limited since the course has only limited lesson time for teaching. In order to acquire a good skill of using  C program, we have to learn more by ourselves from different extra materials so that we can acquire deeper skills in writing C program.


In the coming lecture ,I will learn more advanced knowledge about C program.I will continue to make effort on writing C program and hope that I can acquire a good skill of writing C program at the end of this semester.

沒有留言:

張貼留言