**** 1

2. Control,
Functions
2012.09.07
Jihye Jeon
[email protected]
2
introduction
 Control




structure
if-else
for
while
switch
 Function


function
arguments by reference
Control Structures
 Bifurcation,
repeat code, decisions
 Compound-statement or block

{statement1; statement2; statement3;}
 Conditional
structure
 Iteration structure
 Jump
 Selective structure
Conditional structure: if - else
-The if keyword will execute a statement or block only if a condition
is fulfilled
if(condition1) statement1;
if(condition1) statement1;
else statement2;
if(condition1) statement1;
else if (condition2) statemet2;
else statement3;
Selection: switch and if-else
-The switch statement is similar to a
series of IF statements
-The switch statement executes line
by line until the first time it sees a
break statement
-The case expression may be any
expression that evaluates to
constants
Switch and if-else
-Both of the code fragments have the same behavior.
Iteration: while
-while loops are the simplest type of loop
-while loops execute the nested statement(s) repeatedly, as long
as the while expression evaluates to TRUE
while(expression) statements
Iteration: do-while
- The first iteration of a do-while loop is guaranteed to run.
do statement;
while(expression);
Iteration: for
-For loops are the most complex loops
for(initialization; condition; increase) statement;
initialization
condition
increase
Jump statement: break
-Leave a loop even if the condition for its end is not fulfilled.
11
Example
 Generate
random number from 1 t0 9
with no duplicate using control structure
Functions
-we can structure our programs in a more modular way using functions
-function is a group of statements that is executed when it is called from some point
of the program
The identifier by which it will be possible to call the function
Function body
type name (parm1, param2,…) {statements }
Data type specifier of the data returned by the function
Use void if functions have not return value
Parameters: data type specifier, variable name
acts as regular local variable
Functions
functions need be
defined before they
are referenced.
Functions with no type
Arguments passing
 Arguments

Copy their values but never the variables
themselves
 Arguments

passed by value
passed by reference
Not pass a copy of its value but pass the
variable itself
Arguments by value,
reference
-Not pass a copy of its value but pass the variable itself
Arguments by reference
Example
 Generate
18
random number from 1 t0 9
with no duplicate using functions and
arguments by reference
Homework1
 Make


a star pyramid based on user input
Use control structures and functions
Input number is an odd number
 Example
Please type an odd number :
*
***
*****
***
*
5
7
8
4
3
1
2
6
9
no strike no ball
5
6
2
7
no strike one ball
7
3
4
5
two strike one ball
7 8
Use srand() and rand() function
4
6
three strike no ball
Homework2

Make a baseball-game


Generate random number from 1234 t0 9876 with NO
duplicate (8765(o) / 9987(x),8777(x), 321(x))
Don’t use number ‘0(zero)’ (1457(o), 3498(o) /
2087(x))
Don’t use array



Example