Control Flow: Branching
Control Flow: Branching
Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Dept. of CSE, IIT KGP
}
Dept. of CSE, IIT KGP
Looping:
Some applications may also require that a set of instructions be executed repeatedly, possibly again based on some condition.
Expressions
(count <= 100) ((math+phys+chem)/3 >= 60) ((sex == M) && (age >= 21)) ((marks >== 80) && (marks < 90)) ((balance > 5000) | | (no_of_trans > 25)) (! (grade == A))
Non-zero
Indicates TRUE. Typically the condition TRUE is represented by the value 1.
The condition to be tested is any expression enclosed in parentheses. The expression is evaluated, and if its value is non-zero, the statement is executed.
marks >= 40
false
Grade Computation
START
READ MARKS
if (marks >= 80) printf (A) ; else if (marks >= 60) printf (B) ; else if (marks >=60) printf (C) ; else printf (Failed) ; printf (\nEnd\n) ;
NO
MARKS 40?
MARKS 80?
NO
MARKS 60?
NO
YES
OUTPUT A
YES
OUTPUT B
YES
OUTPUT C OUTPUT F
STOP
Dept. of CSE, IIT KGP
STOP
STOP
STOP
int main () { int marks; scanf (%d, & marks) ; if (marks>= 80) { printf (A) ; printf (Good Job!) ; } else if (marks >= 60) printf (B) ; else if (marks >=60) printf (C) ; else { printf (Failed) ; printf (Study hard for the supplementary) ; } printf (\nEnd\n) ;
}
Dept. of CSE, IIT KGP
int main () { int x, y; scanf (%d%d, &x, &y) ; if (x>y) printf (%d\n, x); else printf (%d\n, x); }
OUTPUT Y
READ X, Y
YES
IS X>Y?
NO
OUTPUT X
STOP
STOP
READ X, Y, Z
YES
IS X > Y?
NO
Max = X
Max = Y
YES
OUTPUT Max STOP
Dept. of CSE, IIT KGP
IS Max > Z?
NO
OUTPUT Z STOP
START
READ X, Y, Z
YES
IS X > Y?
NO
Max = X
Max = Y
YES
OUTPUT Max STOP
IS Max > Z?
NO
OUTPUT Z STOP
int main () { int x, y, z, max; scanf (%d%d%d,&x,&y,&z); if (x>y) max = x; else max = y; if (max > z) printf (%d, max) ; else printf (%d,z); }
Example
#include <stdio.h> main() { int a,b,c; scanf (%d %d %d, &a, &b, &c); if ((a>=b) && (a>=c)) printf (\n The largest number is: %d, a); if ((b>=a) && (b>=c)) printf (\n The largest number is: %d, b); if ((c>=a) && (c>=b)) printf (\n The largest number is: %d, c); }
Example:
if ( payCode == 4 ) printf( "You get a bonus!\n" ); if ( payCode = 4 ) printf( "You get a bonus!\n" );
WRONG
Rule to be remembered:
An else clause is associated with the closest preceding unmatched if. Some examples shown next.
OR
More examples
if e1 s1 else if e2 s2 if e1 s1 else if e2 s2 else s3 if e1 if e2 s1 else s2 else s3 if e1 if e2 s1 else s2
Answers
if e1 s1 else if e2 s2 if e1 s1 else if e2 s2 else s3 if e1 if e2 s1 else s2 else s3 if e1 if e2 s1 else s2 if e1 s1 else { if e2 s2 } if e1 s1 else { if e2 s2 else s3 } if e1 { if e2 s1 else s2 } else s3 if e1 { if e2 s1 else s2 }
Common Errors
c = getchar( ); if ((c == y) && (c == Y)) printf(Yes\n); else printf(No\n);
Returns a value
Equivalent to: if (balance > 5000) interest = balance*0.2; else interest = balance*0.1;
More examples
Examples: x = ((a>10) && (b<5)) ? a+b : 0 (marks>=60) ? printf(Passed \n) : printf(Failed \n);
Examples
switch ( letter ) { case 'A': printf ("First letter \n"); break; case 'Z': printf ("Last letter \n"); break; default : printf ("Middle letter \n"); break; }
Examples
switch (choice = getchar()) { case r : case R: printf(Red); break; case b : case B : printf(Blue); break; case g : case G: printf(Green); break; default: printf(Black); Dept. of CSE, IIT KGP }
Since there isnt a break statement here, the control passes to the next statement (printf) without checking the next condition.
Another way
switch (choice = toupper(getchar())) { case R: case G: case B: default: } printf (RED \n); break; printf (GREEN \n); break; printf (BLUE \n); break; printf (Invalid choice \n);
Rounding a Digit
switch (digit) { case 0: case 1: case 2: case 3: case 4: result = 0; printf (Round down\n); break; case 5: case 6: case 7: case 8: case 9: result = 10; printf(Round up\n); break; }
case - : nt main () { result=operand1-operand2; int operand1, operand2; break; int result = 0; case * : char operation ; result=operand1*operand2; /* Get the input values */ break; printf (Enter operand1 :); case / : scanf(%d,&operand1) ; if (operand2 !=0) result=operand1/operand2; printf (Enter operation :); scanf (\n%c,&operation); else printf(Divide by 0 error); printf (Enter operand 2 :); break; scanf (%d, &operand2); default: switch (operation) { printf(Invalid operation\n); case + : } result=operand1+operand2; printf (The answer is %d\n,resul break; }
Dept. of CSE, IIT KGP
0 0
1 1
s0 0
s1 1
s2 2
s3 3
s4 4
1 0
s0 0
s1 s1
s2 s2
s3 s3
1 0
c = getchar( ); switch (state) { s4 s4 case 0: if (c == 0) state = 1 else state = 0; break case 1: if (c == 0) state = 1 else state = 2; break case 2: if (c == 0) state = 3 else state = 0; break case 3: if (c == 0) state = 4 else state = 2; count++; break; case 4: if (c == 0) state = 1 else state = 2; break }
Examples
Initial values :: a = 10; b = 20; x = 50 + ++a; x = 50 + a++; x = a++ + --b; x = a++ ++a; a = 11, x = 61 x = 60, a = 11 b = 19, x = 29, a = 11 ??
Called side effects:: while calculating some values, something else get changed.
Dept. of CSE, IIT KGP