Beruflich Dokumente
Kultur Dokumente
CONTROL STATEMENT
Quick
Review
Arrays
Blocks and Compound Statements
Control Flow
Conditional Statements
If Statement
Switch Statements
Loops (While, for, do-while, break,
continue, exit())
2
QUICK
REVIEW
DEFINITIONS
Review
Variable -Name/reference to a stored value (usually in
memory)
Data Type -determines the size of a variable in memory,
what values it can take on, what operations are allowed
Operator -an operation performed using 1-3 variables
Expression -combination of literal values/variables and
operators/functions
4
DATA TYPES
Review
Various sizes (char, short, long, float, double)
OPERATORS
Review
Unary, binary, ternary (1-3 arguments)
QUIZ-RI
1. long int variables hold bigger numbers, so why not
always use them instead of int variables?.
ANSWER-RI
1. A long int variable takes up more RAM than the smaller int.
Inefficient with Memory Space
2. The value assigned will have the decimal portion truncated.
3. Many compilers will allow this without signaling any
errors. The number is wrapped to fit and therefore wont be
correct. If you assign 32768 to a two-byte signed variable of
type short, it really contain the value -32768.
4. The compiler does the same wrapping as if you assigned a
number that was too big
8
5. The differences have to do with pointers and variable scope.
QUIZ-RII
1What effect do spaces and blank lines have on how a
program runs?
ANSWER-RII
1. White space (lines, spaces, tabs) makes the code listing
more readable. No effect on the executable program.
2. Using a single compound statement, the expressions are
evaluated only until the entire statement evaluates to false.
3. unary operators work with one variable, and binary
operators work with two.
ARRAYS
ARRAYS
Arrays:
An indexed group of data storage locations that have same name
Distinguished from each other by a subscript, or index (a
number following the variable name, enclosed in brackets)
An array declaration includes both the data type and the size
of the array (the number of elements in the array)
12
13
BLOCKS &
COMPOUND
STATEMENT
Multiple Statements:
temp = x+y;
z = fn(temp);
Curly Braces
Combine into compound statement/block.
15
BLOCKS
Blocks:
Block can substitute for simple statement
NESTED BLOCKS
Blocks nested inside each other
{
int temp = x+y;
z = fn(temp);
{
float temp2 = xy;
z += bar(temp2);
}
}
17
CONTROL
FLOW
CONDITIONAL
STATEMENTS
LOOPS
CONTROL CONDITIONS
in C99, bool type available (use stdbool.h)
Condition is an expression (or series of expressions)
e.g. n<3 or x<y || z<y
Expression is non-zero condition true
The if statement
The switch
statement
The if Statement
Otherwise, do nothing
21
y += (x+1)/2;
Either inner statement may be block
22
23
Nesting if statements
if (x%4 == 0)
if (x%2 == 0)
y= 2;
else
y= 1;
To which if statement does the else keyword belong?
24
Nesting if statements
25
for Loop
Initialization: i=1
Condition: i <= n
Increment: i++
30
Expressions can be empty (condition assumed to be true)
int count;
int main( void )
{
9:
10:
11:
12:
13:
14:
15:
34
Output (forstate.c)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
35
COMMA Operator
Cs comma operator is most often used in for statements
Expression by separating two sub-expressions with comma operator.
Two sub-expressions are evaluated (in left-to-right order).
The entire expression evaluates to the value of the right sub-expression
By using the comma operator, you can make each part of a for statement
perform multiple duties
Two 1,000-element arrays, a[] and b[].
Program to copy the contents of a[] to b[] in reverse order:
37
38
39
40
while Loop
43
44
Output (whilest.c)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
45
46
47
48
49
do while Loop
54
Input Output
1 - Add a Record
2 - Change a record
3 - Delete a record
4 - Quit
Enter a selection: 8
1 - Add a Record
2 - Change a record
3 - Delete a record
4 - Quit
Enter a selection: 4
You chose Menu Option 4
55
56
57
58
QUIZ-I
1. What is the index value of the first element in an array?
2. What is the difference between a for statement and a
while statement?
59
ANSWER-I
1. The first index value of an array in C is 0.
2. A for statement contains initializing and increment
expressions as parts of the command.
QUIZ-II
5. What must you remember when nesting statements?
6. Can a while statement be nested in a do...while
statement?
61
ANSWER-II
5. You cant overlap the loops. The nested loop must be
entirely inside the outer loop.
QUIZ -III
1. Write a declaration for an array that will hold 50 type long
values.
2. Show a statement that assigns the value of 123.456 to the 50th
element in the array .
3. What is the value of x when the following statement is
complete?
for (x = 0; x < 100, x++) ;
4. What is the value of ctr when the following statement is
complete?
for (ctr = 2; ctr < 10; ctr += 3) ;
5. How many Xs does the following print?
for (x = 0; x < 10; x++)
for (y = 5; y > 0; y--)
puts(X);
63
ANSWER-III
1. long array[50];
2. Notice that in the following answer, the 50th element is
indexed to 49. Remember that arrays start at 0.
array[49] = 123.456;
QUIZ -IV
6. Write a for statement to count from 1 to 100 by 3s.
65
ANSWER-IV
6. The code is as follows:
int x;
for( x = 1; x <= 100; x += 3) ;
7. The code is as follows:
int x = 1;
while( x <= 100 )
x += 3;
66
QUIZ -V
9. BUG BUSTER: What is wrong with the following code
fragment?
record = 0;
while (record < 100) {
printf( \nRecord %d , record );
printf( \nGetting next number... );
}
ANSWER- V
6. This program never ends. record is initialized to 0. The
while loop then checks to see whether record is less than
100. 0 is less than 100, so the loop executes, thus printing
the two statements. The loop then checks the condition
again. 0 is still, and always will be, less than 100, so the
loop continues. Within the brackerecord needs to be
incremented. You should add the following line after the
second printf() function call:
record++;
10. The semicolon doesnt belong at the end of the for
statement. This is a common bug.
68
INTRODUCTION TO C
Break
Continue
Infinite Loops
goto Statement
Switch Statement
Exit Statement
Exiting Loop
Early :
break
& continue
71
74
The Output
Original string: This is a test string. It contains two
sentences.
Modified string: This is a test string.
75
The break
76
77
78
79
80
81
83
84
Switch ()
85
Switch ()
Switch (expression) is any expression that evaluates
to an integer value: type long, int, or char.
The switch statement evaluates expression and
compares the value against the templates following
each case label.
Then one of the following happens switch (expression)
87
Multiple cases
Compares variable to each case in order
When match found, starts executing inner code until
break; reached
Execution falls through if break; not included
88
89
90
Output ()
Enter a number between 1 and 5:
1
You entered 1.
Enter a number between 1 and 5:
6
Out of range, try again.
91
92
Output ()
Enter a value between 1 and 10, 0 to exit:
11
Between 1 and 10, please!
Enter a value between 1 and 10, 0 to exit:
1
You entered 5 or less.
Enter a value between 1 and 10, 0 to exit:
6
You entered 6 or more.
Enter a value between 1 and 10, 0 to exit:
0
93
94
Exit ()
The exit() function terminates program execution
and returns control to the operating system.
This function takes a single type int argument that
is passed back to the operating system to indicate the
programs success or failure.
The syntax of the exit() function is
exit(status);
If status has a value of 0, it indicates that the
program terminated normally.
A value of 1 indicates that the program terminated
with some sort of error
95
Exit ()
To use the exit() function, a program must include
the header file stdlib.h.
This header file also defines two symbolic constants
for use as arguments to the exit() function:
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
Thus, to exit with a return value of 0, call
exit(EXIT_SUCCESS);
for a return value of 1,
call exit(EXIT_FAILURE).
96
97
QUIZ-I
1. When is it advisable to use the goto statement
in your programs?
2. Whats the difference between the break
statement and the continue statement?
3. What is an infinite loop, and how do you create
one?
4. What two events cause program execution to
terminate?
98
ANSWER-I
1. Never.
QUIZ-II
5. To what variable types can a switch evaluate?
6.What does the default statement do?
100
ANSWER-II
5. The expression in a switch statement can evaluate to a long,
int, or char value.
6. The default statement is a case in a switch statement. When
the expression in the switch statement evaluates to a value that
doesnt have a matching case, control goes to the default case.
7. The exit() function causes the program to end. A value can
be passed to the exit() function. This value is returned to the
operating system.
8. The system() function executes a command at the operating
system level.
101
SUMMARY
Topics covered:
Controlling program flow using conditional
Dividing a complex program into many simpler subprograms using functions and modular programming
techniques
Variable scope rules and extern, static, and register
variables
102
Thank You!