Download presentation

Presentation is loading. Please wait.

Published byChance Folsom Modified over 3 years ago

2
Lectures on Numerical Methods1 Statements çExpressions, when terminated by a semicolon, become statements. çExamples X = 5; I++; IsPrime(c); c = 5 * ( f – 32 ) / 9 çOne can construct compound statements by putting statements and declarations in Braces { and }. Also called blocks. çExample if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”);

3
Lectures on Numerical Methods2 Labeled statements çOne can give a label to any statement. The form is çIdentifier : statement çThere are other labeled statements like case and default, to be used with switch statement. çExample

4
Lectures on Numerical Methods3 If – else The if-else statement expresses simplest decision making. The syntax is if (expression) statement 1 else opt Statement 2 The expression is evaluated and if it is nonzero (true) then the statement 1 is executed. Otherwise, if else is present statement 2 is executed. Expression can just be numeric and need not be conditional expression only. Statement 1 and statement 2 may be compound statements or blocks.

5
Lectures on Numerical Methods4 If – else if ( n > 0 ) if ( isPrime(n) ) { printf(“%d is prime\n”,n); return; } else printf(“Input must be positive\n”,n); printf(“%d is non-prime\n”,n); Each else is associated with closest previous else-less if. Using this principle multi-way decisions can be constructed.

6
Lectures on Numerical Methods5 Switch çThe syntax of switch statement is switch (expression) { case const-expression 1 : statement 1 case const-expression 2 : statement 2 : default : statement n } çAll case expressions must be different. Expression must evaluate to an integer. çFirst the expression is evaluated. Then the value of expression is compared with the case expressions. The execution begins at the case statement, whose case expression matches. All the statements below are executed. çIf default is present and if no other case matches then default statement is executed.

7
Lectures on Numerical Methods6 Switch switch ( marks / 10 ) { case 3 : grade = “DD”; break; case 4 : grade = “CD”; break; case 5 : grade = “CC”; break; case 6 : grade = “BC”; break; case 7 : grade = “BB”; break; case 8 : grade = “AB”; break; case 9 : case 10 : grade = “AA”; break; default : grade = “FF”; break; }

8
Lectures on Numerical Methods7 Iterations çThe three types of loops are given below. while (expression) statement for (expression1 opt ; expression2 opt ; expression3 opt ) statement do statement while(expression); çIn while loop the expression (which must be arithmetic) is evaluated and if the value is nonzero, then the statement is executed. The process is continued till the expression becomes zero. The expression is evaluated before the iteration. çFor statement is equivalent to expression1; while ( expression2 ) { statement expression3; }

9
Lectures on Numerical Methods8 Iterations In the do loop, the statement is executed and then the expression is evaluated. If the expression is nonzero the process is repeated. Thus the condition is evaluated at the end of each iteration. çExample x1 = 1; do { x0 = x1; x1 = x0 – f(x0) / derf(x0); } while ( fabs(x1 – x0) > FLT_EPSILON ) ; çWe needed to evaluate x1 before we could apply the convergence criterion.

10
Lectures on Numerical Methods9 Break and Continue çThe loops have one expression that decides whether the iterative process should be terminated. It is sometimes convenient to be able to exit from the loop. break statement provides an early exit from the for, while and do loops. çIt also provides an exit from switch statement. continue statement causes the jump to the end of the loop body, skipping the statements only once. The loop may continue. while (...) {... continue;... cont: ; } do {... continue;... cont: ; } for (...) {... continue;... cont: ; }

11
Lectures on Numerical Methods10 Break and continue çExample for ( i = 0; i < n; i++ ) { if ( a[i] < 0 ) continue; /* Process only non-negative elements of the array */... } çExample for ( i = 2; i <= (int)sqrt(n); i++ ) { if ( n % i == 0 ) break; if ( n % i ) printf(“Prime\n”); else printf(“Not prime\n”);

Similar presentations

OK

Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Ppt on team building interventions Ppt on van de graaff generator parts Download ppt on respiration in humans Ppt on event driven programming examples Ppt on grade of concrete Ppt on time management for students Ppt on power grid failure Ppt on interesting facts of india Ppt on understanding body language Download ppt on nutrition in plants and animals