REVIEW
Relational
Less than: Greater than: Less than or equal: Greater than or equal:
Equality
Logical
- - (postfix)
- -(prefix) !
Left to right Left to right Left to right Left to right Right to left
*=
/=
etc.
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
Equal:
==
Not equal: !=
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);
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
C I R C U I T
expression1 || expression2
10
11
Compund Statements
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
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
13
if without else
/* DOES NOT CARE ON THE ELSE CONDITION */
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
} } } }
16
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"); }
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
} }
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; }
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;
20
same as
if(expression1) expression2 else expression3
CS115 FALL 2008-2009 Senem KUMOVA-METN
21
22
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
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); }
24
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
26
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
28
29