Sunteți pe pagina 1din 29

CHAPTER 4

FLOW OF CONTROL-1 Operators, If and switch statements

CS115 FALL 2008-2009 Senem KUMOVA-METN

Operators used for flow of control:

REVIEW

Relational

Less than: Greater than: Less than or equal: Greater than or equal:

< > <= >=

Equality

Logical

Equal: == Not equal: !=


Negation: ! Logical and: && Logical or: ||

CS115 FALL 2008-2009 Senem KUMOVA-METN

Operator Precedence : REVIEW


Operator () ++(postfix) * + < <= == && || ?: = += -= ,
CS115 FALL 2008-2009 Senem KUMOVA-METN

Associativity Left to right Right to left Left to right Left to right

- - (postfix)

+(unary) -(unary) ++(prefix)


/ > != >= %

- -(prefix) !

Left to right Left to right Left to right Left to right Right to left

*=

/=

etc.

Right to left Left to right


3

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);
CS115 FALL 2008-2009 Senem KUMOVA-METN

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

CS115 FALL 2008-2009 Senem KUMOVA-METN

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


CS115 FALL 2008-2009 Senem KUMOVA-METN

Logical Operators: REVIEW


Negation: !
&&

Logical and: Logical or: ||

CS115 FALL 2008-2009 Senem KUMOVA-METN

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

CS115 FALL 2008-2009 Senem KUMOVA-METN

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

CS115 FALL 2008-2009 Senem KUMOVA-METN

Short Circuit Evaluation


(With operands && , ||)
S H O R T

expression1 && expression2

If expression1 is FALSE, No need to check the other, RESULT will be FALSE

C I R C U I T

expression1 || expression2

If expression1 is TRUE, No need to check the other, RESULT will be TRUE


CS115 FALL 2008-2009 Senem KUMOVA-METN

10

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 */
CS115 FALL 2008-2009 Senem KUMOVA-METN

11

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

CS115 FALL 2008-2009 Senem KUMOVA-METN

The if statements

1
2

if(expression) statement; if(expression) statement_1; else statement_2; if(expression_1) else if (expression _2) else if (expression_3) . . else statement_n;
CS115 FALL 2008-2009 Senem KUMOVA-METN

statement_1; statement_2; statement_3;

13

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); } }
CS115 FALL 2008-2009 Senem KUMOVA-METN

14

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);} }
CS115 FALL 2008-2009 Senem KUMOVA-METN

15

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 !!)} ......
CS115 FALL 2008-2009 Senem KUMOVA-METN

} } } }

16

EXAMPLE : if- else if else Calculators 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 !!); } ......


CS115 FALL 2008-2009 Senem KUMOVA-METN

17

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) else } else { statement3 }

{ statement1 { statement2

} }

CS115 FALL 2008-2009 Senem KUMOVA-METN

18

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 else printf("%d } else { if(a%5==0) printf("%d else printf("%d } return 0; }

is a multiple of 2 and 5\n", a); is only a multiple of 2\n",a);

is only a multiple of 5\n",a); is not a multiple of 2 or 5\n",a);

CS115 FALL 2008-2009 Senem KUMOVA-METN

19

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;

CS115 FALL 2008-2009 Senem KUMOVA-METN

20

CONDITIONAL OPERATOR : REVIEW


expression1?expression2:expression3

same as
if(expression1) expression2 else expression3
CS115 FALL 2008-2009 Senem KUMOVA-METN

21

CONDITIONAL OPERATOR : EXAMPLE


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

if (x < 5) y = 5; else y = 10;


CS115 FALL 2008-2009 Senem KUMOVA-METN

22

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 */ }
CS115 FALL 2008-2009 Senem KUMOVA-METN

23

The switch Statement : Example1


int x; if(x==1) else if(x==3) else if(x==4) else printf(x printf(x printf(x printf(x equals to 1); equals to 3); equals to 4); 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); }

CS115 FALL 2008-2009 Senem KUMOVA-METN

24

The switch Statement: Example2


char x; if(x==A) else if(x==B) else if(x==C) else printf(x printf(x printf(x printf(x equals to A); equals to B); equals to C); does not equal to A,B or C);

switch(x) // { case A: case B: case C: default :

switch and check if x equals to any case printf(x equals to A); printf(x equals to B); printf(x equals to C); prinf(x does not equal to A,B or C); }
CS115 FALL 2008-2009 Senem KUMOVA-METN

25

Example: switch Statement


#include <stdio.h> main() { OUTPUT ??? 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"); } }
CS115 FALL 2008-2009 Senem KUMOVA-METN

26

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
CS115 FALL 2008-2009 Senem KUMOVA-METN

27

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"); } }
CS115 FALL 2008-2009 Senem KUMOVA-METN

28

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
CS115 FALL 2008-2009 Senem KUMOVA-METN

29

S-ar putea să vă placă și