Controln Loops Cont
-
Upload
shreya-sharma -
Category
Documents
-
view
217 -
download
0
Transcript of Controln Loops Cont
-
7/29/2019 Controln Loops Cont
1/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 1
Control & Loops continued
P. P. Chakrabarti
-
7/29/2019 Controln Loops Cont
2/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 2
else if statements
if (expression)
statement
else if (expression)
statement
else if (expression)
statementelse
statement
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;
-
7/29/2019 Controln Loops Cont
3/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 3
switch
switch (expression) {
case const-expr/: statements
caseconst-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);
}
-
7/29/2019 Controln Loops Cont
4/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 4
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;
-
7/29/2019 Controln Loops Cont
5/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 5
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);
}
-
7/29/2019 Controln Loops Cont
6/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 6
Math functions
#include
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))));
}
First one is tan(sin-1(x/2))
Guess the second one
compile with lm option
-
7/29/2019 Controln Loops Cont
7/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 7
for loop
for( exp ression1; expression2; exp ression3)statement
expression1 (init) : initialize parameters
expression2 (test): test condition, loop continues ifsatisfied
expression3 (reinit): used to alter the value of theparameters after each iteration
statement (body): body of the loop
-
7/29/2019 Controln Loops Cont
8/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 8
Counting in for loops
for (count=1; count
-
7/29/2019 Controln Loops Cont
9/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 9
for( expression1; expression2; expression3)
statement
expression1;
while (expression2) {
statement
expression3;
}
expression1
(init)
expression2(test)
statement
(body)
expression3
(reinit)
F
T
-
7/29/2019 Controln Loops Cont
10/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 10
Examples : for loop
for (i=0; i
-
7/29/2019 Controln Loops Cont
11/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 11
Computing a series
main ()
{
int i,inp;float x, term=1, sum=0;
scanf(%d%f, &inp,&x);
for (i=1; i
-
7/29/2019 Controln Loops Cont
12/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 12
2-D Figure
Print
* * * * *
* * * * *
* * * * *
#define ROWS 3
#define COLS 5
....
for (row=1; row
-
7/29/2019 Controln Loops Cont
13/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 13
Another 2-D Figure
Print
*
* ** * *
* * * *
* * * * *
#define ROWS 5
....
int row, col;
for (row=1; row
-
7/29/2019 Controln Loops Cont
14/17
-
7/29/2019 Controln Loops Cont
15/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 15
Some Loop Pitfalls
while (sum
-
7/29/2019 Controln Loops Cont
16/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 16
Use ints as loop counters
int i;
double x;
for (i=0; i
-
7/29/2019 Controln Loops Cont
17/17
20-01-03 P.P.Chakrabarti, IIT Kharagpur 17
Some exercises
? Sum= a + (a+d) +(a+2d) + . . . + (a+ (n-1)d)
? The sine of x can be calculated approximately by summing thefirst n terms of the infinite series: x - x3 /3! + x5 /5! x7 /7! + . . .
or upto a given accuracy level, say correct upto 5 decimalplaces. (Here x is in radians.)
? Read an integer and convert to its binary representation. (Findout what a binary representation is).
? Read in a number in its binary representation and print thenumber as an integer.
? Read in a string of characters and print the number of timeseach vowel occurs.