Sie sind auf Seite 1von 17

Control & Loops continued

P. P. Chakrabarti

20-01-03

P.P.Chakrabarti, IIT Kharagpur

else if statements
if (expression) statement else if (expression) statement else if (expression) statement else statement
2
20-01-03

if (x%4 == 0) sum += 2; else if (x%4 == 1) sum *= 2; else if (x%4 == 2) sum--; else if (x%4 == 3) sum++; else sum += 4;

P.P.Chakrabarti, IIT Kharagpur

switch
switch (expression) { case const-expr/: statements case const-expr/: statements default : statements } 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); }

20-01-03

P.P.Chakrabarti, IIT Kharagpur

int main () { int operand1, operand2; int result = 0; char operation ; /* Get the input values */ printf (Enter operand1 :); scanf(%d,&operand1) ; printf (Enter operation :); scanf (\n%c,&operation); printf (Enter operand 2 :); scanf (%d, &operand2); switch (operation) { case + : result=operand1+operand2; break; 4
20-01-03 P.P.Chakrabarti, IIT Kharagpur

case - : result=operand1-operand2; break; case * : result=operand1*operand2; break; case / : if (operand2 !=0) result=operand1/operand2; else printf(Divide by 0 error); break; default: printf(Invalid operation\n); } printf (The answer is %d\n,result); }

20-01-03

P.P.Chakrabarti, IIT Kharagpur

Math functions
First one is tan(sin-1(x/2)) #include<math.h> Guess the second one int main() { float x,y,z; scanf(%f, &x); printf(tan(asine(%f/2)) = %f \n, x, tan(asine(x/2.0))); scanf(%f%f%f, &x,&y,&z); printf(pow(%f, pow(%f, pow(%f, 1.0/3.0))) = %f\n, x,y,z, pow(x, pow(y, pow (z, 1.0/3.0)))); } compile with lm option

20-01-03

P.P.Chakrabarti, IIT Kharagpur

for loop
for ( expression1; expression2; expression3) statement expression1 (init) : initialize parameters expression2 (test): test condition, loop continues if satisfied expression3 (reinit): used to alter the value of the parameters after each iteration statement (body): body of the loop
7
20-01-03 P.P.Chakrabarti, IIT Kharagpur

Counting in for loops


/* Print n asterisks*/ for (count=1; count <= n; count=count+1) { printf(*); } for (count=0; count < n; count=count+1) { printf(*); }
8
20-01-03 P.P.Chakrabarti, IIT Kharagpur

for ( expression1; expression2; expression3) statement


expression1; while (expression2) { statement expression3; }
expression1 (init)

expression2 (test) T statement (body)

expression3 (reinit)

20-01-03

P.P.Chakrabarti, IIT Kharagpur

Examples : for loop


for (i=0; i<10; i++) printf (%d ,i); for (i=0; i<= 10; i++) printf (%d ,i);

output: 0123456789

letter = a; for (c=0; c < 26; c++) printf (%c , letter+c);

10

20-01-03

P.P.Chakrabarti, IIT Kharagpur

Computing a series
main () x + x2/2 + x3/6 + x4/24 + { or x/1! + x2/2! + x3/3! + x4/4! + int i,inp; float x, term=1, sum=0; scanf(%d%f, &inp,&x); for (i=1; i<=inp; i++) { term= term*x/i; sum = sum + term; } printf(Result = %f \n, sum); }
11
20-01-03 P.P.Chakrabarti, IIT Kharagpur

2-D Figure
Print ***** ***** *****
#define ROWS 3 #define COLS 5 .... for (row=1; row<=ROWS; row++) { for (col=1; col<=COLS; col++) { printf(*); } printf(\n); }

12

20-01-03

P.P.Chakrabarti, IIT Kharagpur

Another 2-D Figure


Print * ** *** **** *****
#define ROWS 5 .... int row, col; for (row=1; row<=ROWS; row++) { for (col=1; col<=row; col++) { printf(* ); } printf(\n); }

13

20-01-03

P.P.Chakrabarti, IIT Kharagpur

do-while statement
do statement while (expression)
main () { int digit=0; do printf(%d\n,digit++); while (digit <= 9) ; }
14
statement

F expression T

20-01-03

P.P.Chakrabarti, IIT Kharagpur

Some Loop Pitfalls


while (sum <= NUM) ; sum = sum+2; for (i=0; i<=NUM; i++); sum = sum+i;

for (i=1; i!=10; i=i+2) sum = sum+i; double x; for (x=0.0; x<2.0; x=x+0.2) printf(%.18f\n, x);
15
20-01-03 P.P.Chakrabarti, IIT Kharagpur

Use ints as loop counters


int i; double x; for (i=0; i<10; i=i+1) { x = (double)i/5.0; printf (%.18f\n, x); }
16
20-01-03

[ppchak@cse programs]$ ./a.out 0.000000000000000000 0.200000000000000011 0.400000000000000022 0.599999999999999978 0.800000000000000044 1.000000000000000000 1.199999999999999956 1.399999999999999911 1.600000000000000089 1.800000000000000044 [ppchak@cse programs]$

P.P.Chakrabarti, IIT Kharagpur

Some exercises
? ?

Sum= a + (a+d) +(a+2d) + . . . + (a+ (n-1)d) The sine of x can be calculated approximately by summing the first n terms of the infinite series: x - x3 /3! + x5 /5! x7 /7! + . . . or upto a given accuracy level, say correct upto 5 decimal places. (Here x is in radians.) Read an integer and convert to its binary representation. (Find out what a binary representation is). Read in a number in its binary representation and print the number as an integer. Read in a string of characters and print the number of times each vowel occurs.

17

20-01-03

P.P.Chakrabarti, IIT Kharagpur

Das könnte Ihnen auch gefallen