1/29
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
comments
/* */
Preprocessor directive - tells the computer to load contents of a certain file
#include <stdio.h>
entry point
int main ()
prints the string of characters
printf(““)
what ends a statement?
semicolon (;)
escape character
\
newline character
\n
how do you exit a function?
return 0;
Statements executed one after the other in the order written
Sequential execution
– When the next statement executed is not the next one in
sequence
– Overuse of goto statements led to many problems
Transfer of control
– All programs written in terms of 3 control structures
• Sequence structures: Built into C. Programs executed
sequentially by default
• Selection structures: C has three types: if, if/else, and
switch
• Repetition structures: C has three types: while, do/while
and for
Bohm and Jacopini
– Graphical representation of an algorithm
– Drawn using certain special-purpose symbols connected by
arrows called flowlines
– Rectangle symbol (action symbol):
• Indicates any type of action
– Oval symbol:
• Indicates the beginning or end of a program or a section of code
Flowchart
Connect exit point of one control structure to entry point of
the next (control-structure stacking)
– Makes programs easy to build
Single-entry/single-exit control structures
– Used to choose among alternative courses of action
Selection structure
– Indicates decision is to be made
– Contains an expression that can be true or false
– Test the condition, follow appropriate path
Diamond symbol (decision symbol)
– Takes three arguments (condition, value if true, value if
false)
– Our pseudocode could be written:
printf( "%s\n", grade >= 60 ? "Passed" :
"Failed" );
– Or it could have been written:
grade >= 60 ? printf( “Passed\n” ) :
printf( “Failed\n” );
Ternary conditional operator (?:)
– Compound statements with declarations
Block
– Caught by compiler
Syntax errors
– Caught by compiler
Logic errors:
– Have their effect at execution time
– Non-fatal: program runs, but has incorrect output
– Fatal: program exits prematurely
– Loop repeated until counter reaches a certain value
– Definite repetition: number of repetitions is known
– Example: A class of ten students took a quiz. The grades
(integers in the range 0 to 100) for this quiz are available to
you. Determine the class average on the quiz
Counter-controlled repetition
Many programs have three phases:
– Initialization: initializes the program variables
– Processing: inputs data values and adjusts program variables
accordingly
– Termination: calculates and prints the final results
Can be used instead of c+=1
Increment operator (++)
Can be used instead of c-=1
Decrement operator (--)
Variable is changed before the expression it is in is evaluated
Preincrement (++c or —c)
Expression executes before the variable is changed
Postincrement(c++ or c--)
Definite repetition: know how many times loop will execute
– Control variable used to count repetitions
Counter-controlled repetition
– Indefinite repetition
– Used when number of repetitions not known
– Sentinel value indicates "end of data"
Sentinel-controlled repetition
Format when using for loops
for ( initialization; loopContinuationTest; increment ){
statement}
Format when using while loops
while ( loopContinuationTest ) {
statement;
increment;
}
Format when using do-while loops
do {
statement;
} while ( condition );