+ All Categories
Home > Documents > Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment...

Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment...

Date post: 18-Jan-2016
Category:
Upload: ezra-atkinson
View: 249 times
Download: 2 times
Share this document with a friend
34
Topics to be covered Program Structure Constants Variables Assignment Statements Standard Output Standard Input Math Functions Character Functions System Limitations Algorithm Development Conditional Expressions Selection Statements Loop Structures
Transcript
Page 1: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Topics to be coveredTopics to be covered

Program Structure Constants Variables Assignment Statements Standard Output Standard Input Math Functions Character Functions System Limitations Algorithm Development Conditional Expressions Selection Statements Loop Structures

Page 2: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Simple Program StructureSimple Program Structure

/* ANSI-compliant comments */// Comment to end of line - common but not ANSI-compliant

#include <stdio.h> // printf() Standard header File#include “myinclude.h” // custom header file

// Constant declaration - text replacement to end of line#define PI (3.14159)

int main(void) // default declaration for main(){ // Variable Declarations int m,n,p=12; double x, y, z = 9.2;

// executable code here

return(0); // main() declared as type int - must return int.}

Page 3: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

ConstantsConstants

Literal constants

Values literally appearing in the code. Format determines how compiler interprets them. Interpretted as an integer value:

42 - Interpreted as an integer value. 0x4F - Hexadecimal (0x prefix) - interpretted as an integer. 042 - Octal (0 prefix - watch out!) - interpretted as an integer. 84293L - Long integer - prevents compiler from truncating to int.

Interpretted as a floating point value: 42. - Decimal point is sufficient. 42.0 - But easy to miss - tacking on a zero is better. 1e9 - Scientific notation

#define statements

Convenient way to define symbolic constants. Surrounding values with parentheses is good practice.

Page 4: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

VariablesVariables

A name associated with a memory location where a value can be stored. Can consist of letters, digits, underscore characters - that’s it! Leading character cannot be a digit. Case sensitive - myvar is not the same as MYVAR or MyVar. Only first 32 characters are significant (more in newest ANSI Standard)

Variable types Size and range system dependent - sizes below for Borland Turbo C/C++ V4.5 integer types (can be prefixed with ‘unsigned’ to double positive range)

char - always one byte (but not always eight bits). One ASCII character. int - two bytes (-32768 to +32767) long (or long int) - four bytes (-2147483649 to +2147483648)

floating point types:

float - four bytes - ~5 sig figs, exponent +/- double - eight bytes - ~15 sig figs, exponent +/- long double - ten bytes - ~19 sig figs, exponent +/-

Page 5: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

OperatorsOperators

Arithmetic OperatorsArithmetic Operators Relational OperatorsRelational Operators Logical OperatorsLogical Operators Assignment OperatorsAssignment Operators PrecedencePrecedence AssociativityAssociativity Type CastsType Casts

Page 6: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Arithmetic OperatorsArithmetic Operators

* (Multiplication)* (Multiplication) / (Division)/ (Division)

Integer Division if both operands are integers.Integer Division if both operands are integers. 17 / 5 = 3, not 3.4 - common, common error!17 / 5 = 3, not 3.4 - common, common error!

% (Modulo)% (Modulo) Remainder after Integer Division (17%5 = 2)Remainder after Integer Division (17%5 = 2)

+ (Addition)+ (Addition) - (Subtraction)- (Subtraction)

Page 7: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Relational OperatorsRelational Operators

Evaluate to 0 if FALSE and 1 if TRUEEvaluate to 0 if FALSE and 1 if TRUE == == (equality) (not = common, common mistake)(equality) (not = common, common mistake) != != (inequality)(inequality) > > (greater than)(greater than) < < (less than)(less than) >= >= (greater than or equal to)(greater than or equal to) <= <= (less than or equal to)(less than or equal to)

Complementary Pairs (always opposite result)Complementary Pairs (always opposite result) (==, !=), (>, <=), (<, >=)(==, !=), (>, <=), (<, >=)

Page 8: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Logical OperatorsLogical Operators

Evaluate to 0 if FALSE and 1 if TRUEEvaluate to 0 if FALSE and 1 if TRUE ! ! (negation)(negation) (unary operator - operand to its right)(unary operator - operand to its right) && && (logical AND)(logical AND) (TRUE only if both operands are TRUE) (TRUE only if both operands are TRUE) |||| (logical OR)(logical OR) (TRUE if either operand is TRUE)(TRUE if either operand is TRUE)

Logical Evaluation of OperandsLogical Evaluation of Operands Operand is FALSE if it is exactly equal to zero.Operand is FALSE if it is exactly equal to zero. Operand is TRUE if it is ANYTHING other than zero. Operand is TRUE if it is ANYTHING other than zero.

Guaranteed Short Circuiting If result is known after evaluating left operand, right will not be evaluated.If result is known after evaluating left operand, right will not be evaluated.

Can by very useful in preventing errors. Care must be exercised if there are side-effects in right operand.

(0 != x) && ( (y/x) > 1) (0 != x) && ( (y/x) > 1) Useful: cannot result in division by zero.Useful: cannot result in division by zero.

(y >= x) || (j < k++)(y >= x) || (j < k++) Dangerous: Will not increment k if x < y!Dangerous: Will not increment k if x < y!

Page 9: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Assignment OperatorsAssignment Operators

In expressions, evaluate to the value assigned.In expressions, evaluate to the value assigned.

x = y = j + (k = 12 + (x = j = 3)); is valid code!x = y = j + (k = 12 + (x = j = 3)); is valid code!Abbreviated Assignment OperatorsAbbreviated Assignment Operators

*=, /=, %=, +=, -=, *=, /=, %=, +=, -=, k += x; is the same as k = k + x;k += x; is the same as k = k + x;

Increment/Decrement OperatorsIncrement/Decrement Operators

k++, k-- (post-increment/decrement)k++, k-- (post-increment/decrement) evaluates as original value of k in expressions.evaluates as original value of k in expressions.

++k, --k (pre-increment/decrement)++k, --k (pre-increment/decrement) evaluates as new value of k in expressions.evaluates as new value of k in expressions.

Page 10: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Precedence of OperatorsPrecedence of Operators

Largely follow familiar rulesLargely follow familiar rules (*, /, %) before (+, -) (*, /, %) before (+, -)

Use parentheses liberallyUse parentheses liberally Easy to get trapped in subtleties. Easy to get trapped in subtleties. Prevents the compiler from making your decisions for you!Prevents the compiler from making your decisions for you! Prevents common mistakes such as:Prevents common mistakes such as:

x = a+b / c; instead of x = (a+b)/c;x = a+b / c; instead of x = (a+b)/c;

Page 11: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Associativity of OperatorsAssociativity of Operators

Determines order of evaluation when all else is equal.Determines order of evaluation when all else is equal.

Left Associativity:Left Associativity: x = y * z * w; => x = ( (y * z) * w);x = y * z * w; => x = ( (y * z) * w);

Right Associativity:Right Associativity: x = y += z += w; => x = (y += (z += w));x = y += z += w; => x = (y += (z += w));

Use parens liberally - don’t let the compiler decide!Use parens liberally - don’t let the compiler decide!

Page 12: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Type Cast OperatorsType Cast Operators

Used to convert the representation of a value within an expression.Used to convert the representation of a value within an expression. Has NO effect on the variable that is cast. Operates on the value immediately to the right of the cast. Use name of type in parens:

x = (double) j / (double) k; will force floating point division.x = (double) j / (double) k; will force floating point division. x = (double) (j/k); will still perform integer division.x = (double) (j/k); will still perform integer division.

Compiler will do some casting automatically.Compiler will do some casting automatically. Only performed at points where necessary. Only performed at points where necessary. k = 12 + (14/20) + (7.5/3);k = 12 + (14/20) + (7.5/3);

(7.5/3) => (7.5/3.0) = 2.5 (integer promoted to floating point)(7.5/3) => (7.5/3.0) = 2.5 (integer promoted to floating point) (14/20) = 0 (no cast necessary, integer division used)(14/20) = 0 (no cast necessary, integer division used) (12 + 0 + 2.5) => (12.0 + 0.0 + 2.5) = 14.5 (promotion)(12 + 0 + 2.5) => (12.0 + 0.0 + 2.5) = 14.5 (promotion) k = 14.5 => k = 14 (demotion if k is an int)k = 14.5 => k = 14 (demotion if k is an int)

Page 13: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Standard OutputStandard Output

#include <stdio.h>

printf(“format string”, arg1, arg2, .... argN); // to the screen fprintf(file_ptr, “format string”, arg1, arg2, .... argN); // to a file sprintf(str_ptr, “format string”, arg1, arg2, .... argN); // to a string each returns the number of characters printed.

Format String

All but two characters printed verbatim. % character used to format and output the next argument. \ character used to print characters that can’t be typed directly. Must be at least as many arguments as % specifiers.

Page 14: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

% - printf() conversion specifiers% - printf() conversion specifiers

Format: %[flags][width][.precision][modifier]type_characterAll except type_character are optional.Conversion specifier is everything from % sign to the first type_character.[flags] - controls justification, leading spaces, sign, etc.

{-, +, , #, 0}[width] - sets the minimum width of the field - may be longer. [.precision] - sets the number of digits following the decimal point.

Usually used for floating points, but also affects character and integer types as well.

[modifier] - combines with type_character to determine argument type.type_character - the basic type of that argument.

{c, d, e, E, f, g, G, i, o, p, s, u, x, X, %}

In general, look up what you need.

You will tend to remember the forms you use most often.

Page 15: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

\ - printf() escape sequences\ - printf() escape sequences

Format: \(character) or \(number)If a character follows the \, the action indicated by the character is performed.

\n - newline \r - return w/o line feed. \” - print double quote \’ - print single quote (aka apostrophe) \a - sound bell \b - backspace \\ - print backslash \? - question mark

If a number follows the \, the ASCII character of the octal number is printed.

Page 16: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Standard InputStandard Input

#include <stdio.h>

scanf(“format string”, arg1, arg2, .... argN); // from the keyboard scanf(file_ptr, “format string”, arg1, arg2, .... argN); // from a file sscanf(str_ptr, “format string”, arg1, arg2, .... argN); // from a string each returns the number of successful conversions.

Format String

Literal characters in format string can be used to skip characters. % conversion specifiers similar (but not identical) to printf(). Arguments need to be memory locations where values will be stored.

Big Time Caveat

If input does not adequately match format, results can be VERY unpredictable. Many programmers avoid the use of scanf() at nearly any cost. Use of fscanf() for file I/O is generally safe provided the file format is adequately

constrained.

Page 17: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Common mistakes with scanf()Common mistakes with scanf()

Passing values instead of memory locations. The scanf() function read values and store them at the memory locations you

supply.k = 10;scanf(“%i”, k);

Tells scanf() to format the value as an integer and store it at location 10. But the memory location for variable k is almost certainly not 10. The address operator, &, returns the memory location of the specified variable.

scanf(“%i”, &k); Tells scanf() to format the value as an integer and store it at the memory address

used for variable k.

Using %lf for doubles. printf() uses %lf for both floats and doubles because the compiler promotes all

arguments of type float to double. scanf() cannot due this. It must know which type the variable is so that the number

and format of the bytes stored is correct.

Page 18: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Math functionsMath functions

#include <math.h>

Approximately 30 function, most with a companion function for long ints. trigonometric

sin(), cos(), tan() and their inverses. sinh(), cosh(), tanh() but NOT their inverses (use trig relations) atan2(y,x) - two argument version of atan() allowing four quadrant answer. arguments and/or return values in radians - not degrees.

exponential/logorithmic

base e: exp(), log() base 10: pow10(), log10 other: sqrt(), pow(x, y) - returns x^y

absolute value

abs() - for integers only! common mistake fabs() - for floating point values

Page 19: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

ASCII CodesASCII Codes

American Standard Code for Information Interchange - 7 bits (128 values)

Used to represent characters and certain codes used to control their display. Originally developed for Teletypewriter applications. Many nonstandard extensions exist to extend the code to 8 (or more) bits. Codes <32 are control codes, some of which are no longer used. Codes >=32 are printable characters (except 127 which is the DELETE control code. ASCII 32 is the space character.In C, a character surrounded with single quotes is that character’s ASCII code.

‘ ‘ = 32 ‘0’ = 48 ‘A’ = 65 ‘a’ = 97 = ‘A’ + 32 = ‘A’ + ‘ ‘

Page 20: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Character functionsCharacter functions

#include <ctype.h>

Character test functions

Return a logical value (0 or not-0) based on the properties of the character code.

isdigit(k) returns 0 unless ‘0’ <= k <= ‘9’

isupper(k) returns 0 unless ‘A’ <= k <= ‘Z’

Character manipulation functions

Return a different code for some characters, otherwise return the value passed.

tolower(k) returns k+32 if isupper(k) is TRUE otherwise returns k.

Page 21: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

System LimitationsSystem Limitations

#include <limits.h>

Symbolic constants that define the limits of integer representations.

Examples:

INT_MAX Maximum value of an int INT_MIN Minimum value of an int ULONG_MAX Maximum value of an unsigned long int

#include <float.h>

Symbolic constants that define the limits of floating point representations.

Examples: DBL_MAX Largest representable value for a double. DBL_MIN Smallest positive representable value for a double. DBL_EPSILON Smallest value x such that 1+x is not equal to 1.

Page 22: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Algorithm DevelopmentAlgorithm Development

Structured Programming

Combinination of Sequences Selections Loops

Sequences

Series of operations that are performed in order.

Selections

Choose one path from two or more possible paths.

Loops

Execute a block of code repeatedly as long as a condition is true.

Page 23: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Basic Flowcharting ElementsBasic Flowcharting Elements

test?test?FF

TT

startstarttasktask

I/O taskI/O taskstopstop

Selection BlockSelection Block

Arrows show the flow - cannot branch but can convergeArrows show the flow - cannot branch but can converge

Exit PointExit Point

Execution BlockExecution Block

Input/Output BlockInput/Output Block

Entry PointEntry Point

Page 24: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Top-Down Design ProcessTop-Down Design Process

Start with clear statement of overall problem. Define using a minimal number of elements. Each element represents a more narrowly defined

component of overall design. Treat each element as its own problem and proceed

accordingly. As elements are implemented, integrate into total solution.

Page 25: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Selection StatementsSelection Statements

Selectively choose one path of execution.Selectively choose one path of execution. Based on the evaluation of a test.Based on the evaluation of a test. Test outcome is either TRUE or FALSE.Test outcome is either TRUE or FALSE. FALSE if expression evaluates to ZERO.FALSE if expression evaluates to ZERO. TRUE if expression evaluates to ANYTHING else.TRUE if expression evaluates to ANYTHING else. Three varieties of selection statements.Three varieties of selection statements.

if()/elseif()/else switch()switch() ()? (conditional or ternary operator) ()? (conditional or ternary operator)

Page 26: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Conditional ExpressionsConditional Expressions

(test)(test) May be ANY expression that evaluates to a value. May be ANY expression that evaluates to a value.

logical operator (!, ||, &&)logical operator (!, ||, &&) relational operator (==,!=),(>, <=), (<,>=)relational operator (==,!=),(>, <=), (<,>=) assignment operator (equal to value assigned)assignment operator (equal to value assigned) single variable (or constant)single variable (or constant) function return valuefunction return value

Test outcome is either TRUE or FALSE.Test outcome is either TRUE or FALSE. FALSE if expression evaluates to ZERO - FALSE if expression evaluates to ZERO - exactlyexactly.. TRUE if expression evaluates to ANYTHING else.TRUE if expression evaluates to ANYTHING else.

Page 27: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

if() and if()/else statementsif() and if()/else statements

SyntaxSyntaxif(test)if(test){{ if_code;if_code;}}elseelse{{ else_code;else_code;}}

test?test?

if_codeif_code

if()if()

TT

FFtest?test?

if_codeif_code else_codeelse_code

if()...elseif()...else

FF

TT

Page 28: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

()? - conditional or ternary operator()? - conditional or ternary operator

SyntaxSyntaxx = (test)? if_expr:else_expr;x = (test)? if_expr:else_expr;

Shorthand version of if()/elseShorthand version of if()/elsetest?test?

x = if_exprx = if_expr x = else_exprx = else_expr

x = ()?:x = ()?:

FF

TT

Page 29: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

switch() statementswitch() statement

SyntaxSyntaxswitch(int_expr)switch(int_expr)

{{

case int_const1: code1;case int_const1: code1;

break;break;

case int_const2: code2;case int_const2: code2;

case int_const3: code3;case int_const3: code3;

break;break;

default: code4;default: code4;

}}

Compact way of writing certain Compact way of writing certain types of complex but common types of complex but common if()/else blocks.if()/else blocks.

(int_expr)(int_expr)

must evaluate to an integer value must evaluate to an integer value at runtimeat runtime..

(int_constN)(int_constN)

must evaluate to a unique integer must evaluate to a unique integer constant at constant at compilecompile time. time.

execution jumps to case whereexecution jumps to case where

(int_constN == int_expr) is TRUE.(int_constN == int_expr) is TRUE.

break;break;

terminates switch() execution.terminates switch() execution.

default casedefault case

Optional. Executes only if NO Optional. Executes only if NO other case executes.other case executes.

Page 30: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Testing for floating point equalityTesting for floating point equality

if(x == y) // BAD!!!if(x == y) // BAD!!!

Only true if EXACTLY equal. Because a great deal of care went into how floating Only true if EXACTLY equal. Because a great deal of care went into how floating point values are represented, you can frequently get away with it - but unless you point values are represented, you can frequently get away with it - but unless you REALLY know what is going on and what is preventing disaster from visiting you, REALLY know what is going on and what is preventing disaster from visiting you, you are just being lucky!you are just being lucky!

#DEFINE EPSILON (0.0001)#DEFINE EPSILON (0.0001)

if(EPSILON > fabs(x-y)) // GOOD!!! if(EPSILON > fabs(x-y)) // GOOD!!!

Looking for the difference between two values to be less than some small amount. Will Looking for the difference between two values to be less than some small amount. Will work most of the time but EPSILON must be matched to the size of the values to be work most of the time but EPSILON must be matched to the size of the values to be compared and the range of values can’t be too great.compared and the range of values can’t be too great.

#DEFINE EPSILON (0.0001)#DEFINE EPSILON (0.0001)

if(fabs(EPSILON*y) > fabs(x-y)) // EVEN BETTER!!! if(fabs(EPSILON*y) > fabs(x-y)) // EVEN BETTER!!!

Looking for two values to vary by less than a certain percentage. Comparison written Looking for two values to vary by less than a certain percentage. Comparison written so as to avoid potential division by zero during test.so as to avoid potential division by zero during test.

Page 31: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

Loop StructuresLoop Structures

Special case of Selection Statement One branch eventually leads back to the original selection statement. Permits a block of code to be executed repeatedly as long as some test

condition is satisfied.

C provides three different looping structures while(), do/while(), for() Only one is needed and any one is sufficient. Different structures are better matches for the logic. Using the “proper” one aides the programmer and anyone else reading the

code. The compiler doesn’t care and will often implement the code identically

regardless of which structure is used.

Page 32: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

while() loopwhile() loop

SyntaxSyntaxini_code // not part of loopini_code // not part of loop

while(test_expr)while(test_expr)

{{

loop_code;loop_code;

increment_code;increment_code;

}}

next_code; // not part of loopnext_code; // not part of loop

Features loop does not execute at all if test

fails the first time.

loop_codeloop_code

test?test?FF

TT

inc_codeinc_code

ini_codeini_code

next_codenext_code

Page 33: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

do/while() loopdo/while() loop

SyntaxSyntaxini_code // not part of loopini_code // not part of loop

dodo

{{

loop_code;loop_code;

increment_code;increment_code;

} while(test_expr);} while(test_expr);

next_code; // not part of loopnext_code; // not part of loop

Features loop will always execute at least

once, even if test fails the first time.

loop_codeloop_code

test?test?

FF

TT

inc_codeinc_code

ini_codeini_code

next_codenext_code

Page 34: Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.

for() loopfor() loop

SyntaxSyntaxfor(ini_code; test_expr; inc_code)for(ini_code; test_expr; inc_code)

{{

loop_code;loop_code;

}}

next_code; // not part of loopnext_code; // not part of loop

Features Just a while() loop with the initialization

and increment code formally incorporated into the syntax.

Can makes a cleaner divide between the loop logic and the housekeeping logic.

loop_codeloop_code

test?test?FF

TT

inc_codeinc_code

ini_codeini_code

next_codenext_code


Recommended