Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS115 FALL 2008-2009 Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.

Similar presentations


Presentation on theme: "CS115 FALL 2008-2009 Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements."— Presentation transcript:

1 CS115 FALL 2008-2009 Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements

2 CS115 FALL 2008-2009 Senem KUMOVA-METİN2 Operators used for flow of control: REVIEW  Relational  Less than:<  Greater than:>  Less than or equal:<=  Greater than or equal:>=  Equality  Equal:==  Not equal:!=  Logical  Negation:!  Logical and:&&  Logical or:||

3 CS115 FALL 2008-2009 Senem KUMOVA-METİN3 Operator Precedence : REVIEW Operator Associativity () ++(postfix) - - (postfix) Left to right +(unary) -(unary) ++(prefix) - -(prefix) ! Right to left * / % Left to right + - Left to right >= Left to right == != Left to right && Left to right || Left to right ?: Right to left = += -= *= /= etc. Right to left, Left to right

4 CS115 FALL 2008-2009 Senem KUMOVA-METİN4 Relational Operators : REVIEW, =  EXAMPLE 1: int x=1, y=0; int z=x>y; //???? printf(“%d %d”, z, x<y);  EXAMPLE 2: int x=10,y=4; int z= x-y<5; // which one has // higher precedence? printf(“%d”,z);

5 CS115 FALL 2008-2009 Senem KUMOVA-METİN5 Relational Operators : REVIEW, =  EXAMPLE 3: int x=7; int z=3<x<5; //Remember //associativity!! //LEFT TO RIGHT ((3<x)<5) printf(“%d %d”, z, 3<x<5);

6 CS115 FALL 2008-2009 Senem KUMOVA-METİN6 Equality Operators: REVIEW  Equal:== expr1==expr2  TRUE or FALSE  Not equal:!= expr1!=expr2  TRUE or FALSE  EXAMPLE : int x=0; printf(“%d %d”, x==1, x!=0);

7 CS115 FALL 2008-2009 Senem KUMOVA-METİN7 Logical Operators: REVIEW  Negation:!  Logical and:&&  Logical or:||

8 CS115 FALL 2008-2009 Senem KUMOVA-METİN8 Logical Operators: REVIEW  EXAMPLE 1: int x=1; int z= !x-1; int t= !!x; printf(“%d %d”, z, t);  EXAMPLE 2: int x=5; printf("%d %d", !x, !!x);

9 CS115 FALL 2008-2009 Senem KUMOVA-METİN9 Logical Operators: REVIEW  EXAMPLE 3: int x=1, y=0; printf(“%d %d”, x&&y, x&&y&&y); printf(“%d %d”, x||y, x||y||y);  EXAMPLE 4: int x=4, y=3; printf(“%d %d”, x&&y, x&&y&&y); //1 1 printf(“%d %d”, x||y, x||y||y); //1 1

10 CS115 FALL 2008-2009 Senem KUMOVA-METİN10 Short Circuit Evaluation (With operands &&, ||)  expression1 && expression2 If expression1 is FALSE, No need to check the other, RESULT will be FALSE  expression1 || expression2 If expression1 is TRUE, No need to check the other, RESULT will be TRUE SHORTCIRCUITSHORTCIRCUIT

11 CS115 FALL 2008-2009 Senem KUMOVA-METİN11 Short Circuit Evaluation (With operands &&, ||) EXAMPLE: cnt =3; if(cnt>4 && (c=getchar())!=EOF) {…… } /* cnt>4 will return FALSE so c=getchar())!=EOF expression will never be evaluated */

12 CS115 FALL 2008-2009 Senem KUMOVA-METİN12 Compund Statements  Series of declarations and statements surrounded by braces EXAMPLE 1: int a =1,b=1,c=1; { a+=b+=c; // b=b+c; a=a+b; printf(“%d %d %d”, a,b,c); } EXAMPLE 2: { a=1; { b=2; c=3; } }

13 CS115 FALL 2008-2009 Senem KUMOVA-METİN13 The if statements 1 if(expression) statement; 2 if(expression) statement_1; else statement_2; 3 if(expression_1) statement_1; else if (expression _2) statement_2; else if (expression_3) statement_3;. else statement_n;

14 CS115 FALL 2008-2009 Senem KUMOVA-METİN14 if without else /* DOES NOT CARE ON THE ELSE CONDITION */ #include #include // for exit() function main() { int x; printf(“Type a number or Press 0 to exit “); scanf(“%d”, &x); if(x==0) { printf(“ eXiting.....\n ”); exit(0); }

15 CS115 FALL 2008-2009 Senem KUMOVA-METİN15 if with else...... main() { int x; printf(“Type 0 for addition or 1 for subtraction\n“); scanf(“%d”, &x); if(x==0) { printf( “ PERFORM ADDITION...”\n) ;} else { printf( “ PERFORM SUBTRACTION...”\n);} }

16 CS115 FALL 2008-2009 Senem KUMOVA-METİN16 if- else if - else /* IF YOU HAVE MORE THAN 2 CASES */...... scanf(“%d”,&x); if(x==0) { printf("x is 0\n"); } else if(x==1) { printf("x is 1\n"); } else if(x==2) { printf("x is 2\n"); } else if(x==3) { printf("x is 3\n"); } else {printf(“ wrong number !!”)}......

17 CS115 FALL 2008-2009 Senem KUMOVA-METİN17 EXAMPLE : if- else if – else Calculator’s Menu int a=9,b=5,c; char x; scanf(“%c”,&x); if(x==‘A’) { printf(“ADDITION\n"); c=a+b; } else if(x==‘S’) { printf(“SUBTRACTION\n"); c=a-b; } else if(x==‘M’) { printf(“MULTIPLICATION\n“); } else if(x==‘D’) { printf(“DIVISION\n"); } else {printf(“ wrong number !!”); }......

18 CS115 FALL 2008-2009 Senem KUMOVA-METİN18 Nested if Statement EXAMPLE 1: if(expression1) { if(expression2) { statement2 } statement1 } /* if expression1 is TRUE, statement1 will be evaluated whether expression 2 is TRUE or FALSE */ /* if expression1 and expression 2 are TRUE, statement2 will be evaluated */ EXAMPLE 2: if(expression1) { if(expression2){ statement1 } else { statement2 } } else { statement3 }

19 CS115 FALL 2008-2009 Senem KUMOVA-METİN19 Example 1: Find if x is a multiple of 2 and 5 #include int main() { int a; printf("Input an integer and push return:\n"); scanf("%d", &a); if (a%2==0) { if(a%5==0) printf("%d is a multiple of 2 and 5\n", a); else printf("%d is only a multiple of 2\n",a); } else {if(a%5==0) printf("%d is only a multiple of 5\n",a); else printf("%d is not a multiple of 2 or 5\n",a); } return 0; }

20 CS115 FALL 2008-2009 Senem KUMOVA-METİN20 Example 2: Find if x is a multiple of 2 and 5 #include int main() { int a; printf("Input an integer and push return:\n"); scanf("%d", &a); if (a%2==0 && a%5==0) { /* Start of if block */ printf("%d is a multiple of 2 and 5\n", a); } else { /* This is the else branch */ printf("%d is not a multiple of both 2&5\n", a); } return 0; }

21 CS115 FALL 2008-2009 Senem KUMOVA-METİN21 CONDITIONAL OPERATOR : REVIEW expression1?expression2:expression3 same as if(expression1) expression2 else expression3

22 CS115 FALL 2008-2009 Senem KUMOVA-METİN22 CONDITIONAL OPERATOR : EXAMPLE y = (x < 5) ? 5 : 10; if (x < 5) y = 5; else y = 10;

23 CS115 FALL 2008-2009 Senem KUMOVA-METİN23 The switch Statement A multiway conditional statement generalizing the if else statement switch (expression) { case expr1: /*one or more statements*/ case expr2: /*one or more statements*/ case expr3: /*one or more statements*/ /*...more cases if necessary */ default: /* do this if all other cases fail */ }

24 CS115 FALL 2008-2009 Senem KUMOVA-METİN24 The switch Statement : Example1 int x; if(x==1) printf(“x equals to 1”); else if(x==3) printf(“x equals to 3”); else if(x==4) printf(“x equals to 4”); else printf(“x does not equal to 1,3 or 4”); switch (x) // switch and check if x equals to any case { case 1 : printf(“x equals to 1”); case 3 : printf(“x equals to 3”); case 4 : printf(“x equals to 4”); default: printf(“x does not equal to 1,3 or 4”); }

25 CS115 FALL 2008-2009 Senem KUMOVA-METİN25 The switch Statement: Example2 char x; if(x==‘A’) printf(“x equals to A”); else if(x==‘B’) printf(“x equals to B”); else if(x==‘C’) printf(“x equals to C”); else printf(“x does not equal to A,B or C”); switch(x) // switch and check if x equals to any case {case ‘A’: printf(“x equals to A”); case ‘B’: printf(“x equals to B”); case ‘C’: printf(“x equals to C”); default : prinf(“x does not equal to A,B or C”); }

26 CS115 FALL 2008-2009 Senem KUMOVA-METİN26 Example: switch Statement  #include main() { int i; printf ("Enter a positive integer from 1 to 4: "); scanf("%d", &i); switch(i) { case 1: printf(“You have entered 1 \n"); case 2: printf(“You have entered 2 \n"); case 3: printf(“You have entered 3 \n"); case 4: printf(“You have entered 4 \n"); default: printf(“not 1,2,3 or 4\n"); } OUTPUT ???

27 CS115 FALL 2008-2009 Senem KUMOVA-METİN27 Example: switch Statement  You'll notice that the program will select the correct case but will also run through all the cases below it (including the default) until the switch block's closing bracket is reached.  To prevent this from happening, we'll need to insert another statement into our cases...  BREAK

28 CS115 FALL 2008-2009 Senem KUMOVA-METİN28 Example: switch Statement #include main() { int i; printf ("Enter a positive integer from 1 to 4: "); scanf("%d", &i); switch(i) {case 1: printf(“You have entered 1 \n"); break; case 2: printf(“You have entered 2 \n"); break; case 3: printf(“You have entered 3 \n"); break; case 4: printf(“You have entered 4 \n"); break; default: printf(“not 1,2,3 or 4\n"); }

29 CS115 FALL 2008-2009 Senem KUMOVA-METİN29 The break statement /* BREAK CAUSES AN EXIT FROM THE INNERMOST ENCLOSING LOOP OR SWITCH STATEMENT */ double x; while(1) // an infinite loop { scanf(“%lf”,&x); if(x<0.0) break; /* exit loop if x is negative */ printf(“%f\n”,fsqrt(x)); } // break jumps to here


Download ppt "CS115 FALL 2008-2009 Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements."

Similar presentations


Ads by Google