You are on page 1of 32

PART I: Operators used for flow of control

PART II: SELECTION STATEMENTS : (If and switch) Chapter 3 & 4


CS115 2010_2011 Senem KUMOVA METN

PART I: Operators used for flow of control

CS115 2010_2011 Senem KUMOVA METN

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:

CS115 2010_2011 Senem KUMOVA METN

Operator Precedence : REVIEW


Operator () ++(postfix) * + < <= / > >= - - (postfix) - -(prefix) ! % +(unary) -(unary) ++(prefix)
Associativity Left to right 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

CS115 2010_2011 Senem KUMOVA METN

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 2010_2011 Senem KUMOVA METN

Relational Operators : REVIEW <, >, <=, >=


EXAMPLE 3: int x=7; int z=3<x<5;
//LEFT TO RIGHT

//Remember //associativity!! ((3<x)<5)

printf(%d %d, z, 3<x<5);

CS115 2010_2011 Senem KUMOVA METN

Equality Operators: REVIEW


Equal: Not equal: ==
expr1==expr2 TRUE or FALSE

!=
expr1!=expr2 TRUE or FALSE

EXAMPLE : int x=0; printf(%d %d, x==1, x!=0);


CS115 2010_2011 Senem KUMOVA METN

Logical Operators: REVIEW


Negation: Logical and: ! &&

Logical or:

||

CS115 2010_2011 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 2010_2011 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 2010_2011 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

CI R C U IT

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

CS115 2010_2011 Senem KUMOVA METN

Short Circuit Evaluation


(With operands && , || )
EXAMPLE:

cnt =3; if(cnt>4 && c==3) { }


/* cnt>4 will return FALSE so c==3 expression will never be evaluated */

CS115 2010_2011 Senem KUMOVA METN

PART II: Selection Statements


If switch

CS115 2010_2011 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 2010_2011 Senem KUMOVA METN

statement_1; statement_2; statement_3;

if without else
/* DOES NOT CARE ON THE ELSE CONDITION */
// MISSION : // CHECK IF USER ENTERS ZERO IF USER ENTERS ZERO EXIT

#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); } printf(%d,x); }
CS115 2010_2011 Senem KUMOVA METN

if without else
#include<stdio.h>
main() { int x; printf(Type a number); scanf(%d, &x); if(x==0) { printf(The neumber is zero);} printf(%d,x); }

CS115 2010_2011 Senem KUMOVA METN

if with else
//IF YOU HAVE 2 CASES (IF and ELSE ) // MISSION : CHECK IF A STUDENT FAILS OR PASSES main() { int x; printf(Enter your grade :\n);

scanf(%d, &x);
if(x>=60) { printf( Passed\n) ;} else { printf( Failed\n);} }

CS115 2010_2011 Senem KUMOVA METN

if- else if - else


// IF YOU HAVE MORE THAN 2 CASES /* MISSION: If x is greater than 90 print A else if x is greater than 80 print B else if x is greater than 70 print C else if x is greater than 60 print D else if x is less than 60 print E *\ int x; scanf(%d,&x); if(x>=90) { printf(A\n"); } else if(x>=80) { printf(B\n"); } else if(x>=70) { printf(C\n"); } else if(x>=60) { printf(D\n"); } else { printf(E\n); }
CS115 2010_2011 Senem KUMOVA METN

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); c=a*b;}


else if(x==D) { printf(DIVISION\n"); c=a/b; } else {printf( invalid input !!); } ......
CS115 2010_2011 Senem KUMOVA METN

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 2010_2011 Senem KUMOVA METN

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 not a multiple of 2 and 5\n", a); return 0; }

CS115 2010_2011 Senem KUMOVA METN

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 2010_2011 Senem KUMOVA METN

CONDITIONAL OPERATOR : REVIEW


expression1?expression2:expression3
same as if(expression1) expression2 else expression3
CS115 2010_2011 Senem KUMOVA METN

CONDITIONAL OPERATOR : EXAMPLE 1


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

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


CS115 2010_2011 Senem KUMOVA METN

CONDITIONAL OPERATOR : EXAMPLE 2


f(x>5) printf(greater than 5); else printf(less than 5);

x>5?printf(greater than 5):printf(less than 5);


CS115 2010_2011 Senem KUMOVA METN

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 2010_2011 Senem KUMOVA METN

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) { case 1 : case 3 : case 4 : default: // switch printf(x printf(x printf(x printf(x and check if x equals to 1); equals to 3); equals to 4); does not equal equals to any case break; break; break; to 1,3 or 4); }

CS115 2010_2011 Senem KUMOVA METN

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); break; printf(x equals to B); break; printf(x equals to C); break; prinf(x does not equal to A,B or C); }

CS115 2010_2011 Senem KUMOVA METN

Example 3: switch statement without break


#include <stdio.h> OUTPUT ??? 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"); } }
CS115 2010_2011 Senem KUMOVA METN

Example 3: 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 2010_2011 Senem KUMOVA METN

Example 4: switch Statement


// // // // // Get a number from user if the number is 1 or 2, print A; if the number is 3, print B; if the number is 4 or 5 or 6, print C; otherwise print D

#include <stdio.h> main() { int i; printf("Enter a positive integer : "); scanf("%d", &i); switch(i) { case 1: case 2: printf(A\n"); break; case 3: printf(B\n"); break; case 4: case 5: case 6: printf(C\n"); break; default: printf(D\n"); }} CS115 2010_2011 Senem KUMOVA METN

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 2010_2011 Senem KUMOVA METN

You might also like