Presentation is loading. Please wait.

Presentation is loading. Please wait.

BNF ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= [ ]... ::= [ + | - ] ::= [. [ ] ] f ::= [. [ ] ] ::= 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.

Similar presentations


Presentation on theme: "BNF ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= [ ]... ::= [ + | - ] ::= [. [ ] ] f ::= [. [ ] ] ::= 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."— Presentation transcript:

1 BNF ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= [ ]... ::= [ + | - ] ::= [. [ ] ] f ::= [. [ ] ] ::= 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 ::= 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 ::= | ::= | | _ ::= _ | [ ]...

2 BNF (details) ::= [. [ ] ] f [ ] mean optional f is required (no brackets) ::= _ | [ ]... a variable must begin with a _ or alpha character, and may be followed by 0 or more alphanumext characters ORzero or more

3 BNF ::= ::= = ; ::= | | | | | // many more ::={ [ ]... } ::= ; |

4 BNF (if) ::= if ( ) [ else ]

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

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) 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 a single equals means ASSIGN. a double equal must be used to check for equality.

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 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 = ] expression 1 ? expression 2 : expression 3 if expression 1 is non-zero, expression 2 is evaluated and optionally assigned; if expression 1 is zero, expression 3 is evaluated and optionally assiged;


Download ppt "BNF ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= [ ]... ::= [ + | - ] ::= [. [ ] ] f ::= [. [ ] ] ::= 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."

Similar presentations


Ads by Google