Presentation is loading. Please wait.

Presentation is loading. Please wait.

BNF <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Similar presentations


Presentation on theme: "BNF <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9"— Presentation transcript:

1 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> ]...

2 BNF (details) f is required (no brackets) [ ] mean optional OR
<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

3 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>

4 BNF (if) <if_stmnt> ::= if ( <expression> )
<statement> [ else <statement> ]

5 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");

6 if (relational operators)
> Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal != Not Equal

7 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

8 if (common pitfalls) a single equals means ASSIGN. x=12345;
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

9 if (uses for a single = in ifs)
if ( (disc = b*b-4*a*c) < 0 ) { // something } else { // another something }

10 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!"); } }

11 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!");

12 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!");

13 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!");

14 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!");

15 if (example) 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 } } }

16 ECE 160 03/04/2005 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; (c) 2005, P. H. Viall


Download ppt "BNF <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9"

Similar presentations


Ads by Google