cs115 fall 2008-2009 senem kumova-metİn1 chapter 4 flow of control-1 operators, if and switch...

29
CS115 FALL 2008-2009 Senem KU MOVA-METİN 1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements

Upload: jasper-oneal

Post on 19-Jan-2018

224 views

Category:

Documents


1 download

DESCRIPTION

CS115 FALL 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

TRANSCRIPT

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

1

CHAPTER 4

FLOW OF CONTROL-1

Operators, If and switch statements

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

2

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

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

3

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 

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

4

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

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

5

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

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

6

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

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

7

Logical Operators: REVIEW

Negation: !

Logical and: &&

Logical or: ||

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

8

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

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

9

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 1printf(“%d %d”, x||y, x||y||y); //1 1

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

10

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

SHORTCIRCUIT

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

11

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 */

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

12

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; }

}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

13

The if statements1 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;

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

14

if without else/* DOES NOT CARE ON THE ELSE CONDITION */

#include<stdio.h>#include<stdlib.h> // 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); }

}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

15

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);}

}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

16

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 !!”)}......

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

17

EXAMPLE : if- else if – elseCalculator’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 !!”); }......

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

18

Nested if StatementEXAMPLE 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 }

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

19

Example 1: Find if x is a multiple of 2 and 5 #include <stdio.h> 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;

}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

20

Example 2: Find if x is a multiple of 2 and 5#include <stdio.h> 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;

}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

21

CONDITIONAL OPERATOR : REVIEW

expression1?expression2:expression3

same as

if(expression1) expression2

else expression3

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

22

CONDITIONAL OPERATOR : EXAMPLE

y = (x < 5) ? 5 : 10;

if (x < 5) y = 5;

elsey = 10;

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

23

The switch StatementA 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 */

}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

24

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”); }

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

25

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”); }

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

26

Example: switch Statement #include <stdio.h>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 ???

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

27

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

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

28

Example: switch Statement

#include <stdio.h>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"); }}

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

CS115 FALL 2008-2009 Senem KUMOVA-METİN

29

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