Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental of C programming - Ompal Singh. Conditional Statement IF- Statement: It is the basic form where the if statement evaluate a test condition.

Similar presentations


Presentation on theme: "Fundamental of C programming - Ompal Singh. Conditional Statement IF- Statement: It is the basic form where the if statement evaluate a test condition."— Presentation transcript:

1 Fundamental of C programming - Ompal Singh

2 Conditional Statement IF- Statement: It is the basic form where the if statement evaluate a test condition and direct program execution depending on the result of that evaluation. Syntax: If (Expression) { Statement 1; Statement 2; } Where a statement may consist of a single statement, a compound statement or nothing as an empty statement. The Expression also referred so as test condition must be enclosed in parenthesis, which cause the expression to be evaluated first, if it evaluate to true (a non zero value), then the statement associated with it will be executed otherwise ignored and the control will pass to the next statement.

3 Example: if (a>=b) { printf (“a is larger than b”); } true false a>=b print “A is greater”

4 IF-ELSE Statement: An if statement may also optionally contain a second statement, the ``else clause,'' which is to be executed if the condition is not met. Syntax: If (Expression) { Statement 1; } else { statement; } stalement;

5 Example: if (a>=b) { printf (“a is larger than b”); } else { printf (“b is larger than a”); } falsetrue print “a is greater” print “b is greater” a >= b

6 if  Only performs an action if the condition is true if / else  Specifies an action to be performed both when the condition is true and when it is false

7 else/if structures  Test for multiple conditions by placing else/if selection structures inside else/if selection structures  Once condition is met, rest of statements skipped Else if

8 Syntax: if (test expression) to be executed if test expression is true; else if(test expression 1) statements to be executed if test expression is true; else if (test expression 2) Statements to be execute if test expression is true; else statements to be executed if all test expressions are false.

9 Example: int main() { int age; printf( "Please enter your age" ); scanf( "%d", &age ); if ( age < 100 ) { printf ("You are pretty young!\n" ); } else if ( age == 100 ) { printf( "You are old\n" ); } else { printf( "You are really old\n" ); } return 0; }

10 Example if( grade >= 90) printf("You got an A!\n"); else if ( grade >= 80 ) printf("You got a B!\n"); else if ( grade >= 70 ) printf("You got a C!\n"); else if ( grade >= 60 ) printf("You got a D!\n"); else printf("You failed!\n");

11 Nested if...else statement (if...elseif....else Statement) The if...else statement can be used in nested form when a serious decision are involved. Syntax: if (test expression) to be executed if test expression is true; Else if(test expression 1) statements to be executed if test expressions 1 is true; else if (test expression 2)... else statements to be executed if all test expressions are false.

12 Example: int main(){ int numb1, numb2; printf("Enter two integers to check"); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) printf("Result: %d=%d",numb1,numb2); else if(numb1>numb2) printf("Result: %d>%d",numb1,numb2); else printf("Result: %d>%d",numb2,numb1); return 0; }

13 Switch Case This is another form of the multi way decision. It is well structured, but can only be used in certain cases where; Only one variable is tested, all branches must depend on the value of that variable. The variable may be an integral type. (int, long, short or char). Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.

14 Syntax switch ( integer expression ) { case constant1 : statement(s) break ; case constant2 : statement(s) break ;... default: statement(s) break ; }

15 Int n; Printf(“Enter the choice”) Scanf(“%d”,&n); switch ( n ) { case 0: printf (“Sunday\n”) ; break ; case 1: printf (“Monday\n”) ; break ; case 2: printf (“Tuesday\n”) ; break ; case 3: printf (“Wednesday\n”) ; break ; case 4: printf (“Thursday\n”) ; break ; case 5: printf (“Friday\n”) ; break ; case 6: printf (“Saturday\n”) ; break ; default: printf (“Error -- invalid day.\n”) ; break ; }

16 #include void main() { int a,b,c,ch; clrscr(); printf(" Main Menu \n\n"); printf("1.Add\n"); printf("2.Subtraction\n"); printf("3.Multiply\n\n\n"); printf("Enter the choice\n\n"); scanf("%d",&ch); printf("\n\nenter the number a and b\n"); scanf("%d%d",&a,&b); switch(ch) { case 1: c=a+b; printf("%d",c); break; case 2: c=a-b; printf("%d",c); break; case 3: c=a*b; printf("%d",c); break; default: printf("wrong choice"); break; } getch(); }

17 What is a loop? A loop is a programming structure that contains an executable block of code that repeats so long as a given condition is true/not true. Good programmers include a way to satisfy the condition somewhere inside the executable block. Types of Loop While Do While For

18 While Loop/Pre-Test Loops In pre-test loops, the executable block of code is execute after the condition test is true. the while loop is a pre-test loop. Syntax: initialization; While(condition) { statement; inc/dec; }

19 Without loop #include void main() { printf("hello\n"); } #include void main() { int i=1; while(i<=5) { printf("hello\n"); i++; } With loop initialization Condition Increment

20 1. #include 2. Void main() 3. { 4. int i=1; 5. while(i<=10) 6. { 7. printf(“%d”,i); 8. i++; or i=i+1; 9. } } Output: #include 2. Void main() 3. { 4. int i=10; 5. while(i>=1) 6. { 7. printf(“%d”,i); 8. i--; or i=i-1; 9. } } Output: initialization Condition increment

21 Sum from 1 to 100 (while) 1. int i=1,sum=0; 2. while(i<=100) 3. { 4. sum=sum+i; 5. i++; 6. } 7. Printf(“%d”,sum);

22 Program to print a table #include void main() { int i,n,t; clrscr(); i=1; printf("Enter the number"); scanf("%d",&n); while(i<=10) { t=n*i; printf("\n%d",t); i++; } getch(); }

23 Do while/Post-Test Loops In post-test loops, the executable block of code is execute before the condition is test. The loop will always execute at least one during the run of a program. The do…while loop is a post-test loop. Syntax: initialization; do{ statement; inc/dec; } While(condition);

24 1. #include 2. Void main() 3. { 4. int i=1; 5. do 6. { 7. printf(“%d”,i); 8. i++; or i=i+1; 9. }while(i<=10); } Output: #include 2. Void main() 3. { 4. int i=10; 5. do 6. { 7. printf(“%d”,i); 8. i--; or i=i-1; 9. }while(i>=1); } Output: initialization Condition increment

25 Sum from 1 to 100 (do-while) 1. int i=1,sum=0; 2. do{ 3. sum=sum+i; 4. i++; 5. } while (i<=100); 6. Printf(“%d”,sum);

26 For Loop In C programming languages, the for construct is composed of three parts, each divided by a semicolon: Syntax for(initialization; test condition; inc/dec) { statement; } In the first section, the initialization, we initialize an index variable (which is usually being named i, j or k) to its initial value; In the second part, we test whether a variable (usually the same we have just initialized in the first part) satisfies a certain condition: if it does, we enter the loop one more time, otherwise we exit from it; In the third and last part, we update the variable — usually by incrementing or decrementing it by 1.

27 1. #include 2. Void main() 3. { 4. int i; 5. For(i=1;i<=10;i++) 6. { 7. printf(“%d”,i); 8. } } Output: #include 2. Void main() 3. { 4. int i; 5. For(i=10;i>=1;i--) 6. { 7. printf(“%d”,i); 8. } } Output:

28 Sum from 1 to 100 (for) 1. int i,sum=0; 2. for(i=1;i<=100;i++) 3. { 4. sum=sum+i; 5. } 6. Printf(“%d”,sum);

29 break Statement The break statement can be used in while, do-while, and for loops to exit from the loop.

30 Example break in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { break ; } printf (“%d “, i) ; } printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; } OUTPUT: Broke out of loop at i = 5.

31 The continue Statement The continue statement can be used in while, do-while, and for loops. It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.

32 Example continue in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { continue ; } printf (“%d ”, i) ; } printf (“\nDone.\n”) ; return 0 ; } OUTPUT:

33 Nested Loops Loops may be nested (embedded) inside of each other. Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure. It is common to see nested for loops.

34 Nested Loop 1. int i,j; 2. for(i=0;i<5;i++) 3. { 4. for(j=0;j


Download ppt "Fundamental of C programming - Ompal Singh. Conditional Statement IF- Statement: It is the basic form where the if statement evaluate a test condition."

Similar presentations


Ads by Google