170 likes | 301 Views
BNF. <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <unsigned_int_const> ::= <digit> [ <digit> ] ... <signed_int_const> ::= [ + | - ] <unsigned_int_const> <float_const> ::= <signed_int_const> [ . [ <unsigned_int_const>] ] f
E N D
BNF <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <unsigned_int_const> ::= <digit> [ <digit> ] ... <signed_int_const> ::= [ + | - ] <unsigned_int_const> <float_const> ::= <signed_int_const> [ . [ <unsigned_int_const>] ] f <double_const> ::= <signed_int_const> [ . [ <unsigned_int_const>] ] <up_alpha> ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z <low_alpha> ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z <alpha> ::= <up_alpha> | <low_alpha> <alphanum> ::= <alpha> | <digit> <alphanumext> ::= <alpha> | <digit> | _ <variable> ::= _ | <alpha> [ <alphanumext> ]...
BNF (details) f is required (no brackets) [ ] mean optional <float_const> ::= <signed_int_const> [ . [ <unsigned_int_const>] ] f OR zero or more <variable> ::= _ | <alpha> [ <alphanumext> ]... a variable must begin with a _ or alpha character, and may be followed by 0 or more alphanumext characters
BNF <expression> ::= <term> <assignment_stmnt> ::= <variable> = <expression> ; <simple_stmnt> ::= <assignment statement> | <system_function_call> | <user_function_call> | <while_stmnt> | <if_stmnt> | <for_stmnt> // many more <compound_stmnt>::= { [ <simple_stmnt> ]... } <statement> ::= <simple_stmnt> ; | <compound_stmnt>
BNF (if) <if_stmnt> ::= if ( <expression> ) <statement> [ else <statement> ]
if if (a > b) big = a;else big = b; if (a+6*3-43) printf("wow is this not cool");else printf("this is not cool");
if (relational operators) > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal != Not Equal
if (Logical operators) (As covered before in expression evaluation) Recall expressions with Logical Operators evaluate to either zero or Non-zero && Logical AND || Logical OR ! Logical NOT
if (common pitfalls) a single equals means ASSIGN. a double equal must be used to check for equality. x=12345; if (x=3){ printf("x is 3\n");}else{ printf("x is not 3\n");} This code will ALWAYS print:x is 3
if (uses for a single = in ifs) if ( (disc = b*b-4*a*c) < 0 ){ // something}else{ // another something}
if (range checking - take 1) int n; printf("Enter a number 1 to 10: ");scanf("%d",&n);if (n > 10){ printf("Idiot!");}else{ if (n < 1) { printf("Idiot!"); } else { printf("Good job!"); }}
if (range checking - take 2) If there is only one statement needed for the true and/or false condition, the {} are not needed int n; printf("Enter a number 1 to 10: ");scanf("%d",&n);if (n > 10) printf("Idiot!");else if (n < 1) printf("Idiot!"); else printf("Good job!");
if (range checking - take 3) Use the && or || as needed to check for multiple conditions int n; printf("Enter a number 1 to 10: ");scanf("%d",&n); Note these ( ) are needed if ( (n > 10) || (n < 1) ) printf("Idiot!");else printf("Good job!");
if (range checking - take 4) Use the && or || as needed to check for multiple conditions int n; printf("Enter a number 1 to 10: ");scanf("%d",&n); Note these ( ) are needed if ( (1 <= n) && (n <= 10) ) printf("Good job!");else printf("Idiot!");
if (range checking)(The WRONG WAY) int n; printf("Enter a number 1 to 10: ");scanf("%d",&n); if (1 <= n <= 10 ) // THIS WILL NOT COMPILE printf("Good job!");else printf("Idiot!");
void main(void){ float a,b,c,disc; : scanf("%f %f %f",&a,&b,&c); if (a==0) { // statements } else { disc = b*b-4*a*c; if ( disc < 0 ) { // statements } else { // statements } } } if (example)
if (alternative form) if (a > b) big = a;else big = b; The above code may be replaced by: big = (a>b)?a:b General form:[ variable = ] expression1 ? expression2 : expression3 if expression1 is non-zero, expression2 is evaluated and optionally assigned; if expression1 is zero, expression3 is evaluated and optionally assiged;