1 / 18

Flow control in C language

Flow control in C language. if () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop. if() and if()…else statement In C language the using structure to write any condition by if() is:. Syntax : if (condition) { statements to execute;

kyran
Download Presentation

Flow control in C language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Flow control in C language if () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop

  2. if() and if()…else statementIn C language the using structure to write any condition by if() is: • Syntax: if (condition) { statements to execute; } else { • statements to execute; } • In C language the using structure to write any condition by if() is: • Syntax: if (condition) { statements to execute; }

  3. Continue: Example: Write a C program to solve this problem: enter a student number and his grade, then print classification according the following values: grade < 60 then print “ fail” 60 <= grade < 70 then print “ pass” 70 <= grade < 80 then print “ good” 80 <= grade < 90 then print “ very good” 90 <= grade <=100 then print “ excellent”

  4. #include<stdio.h> void main() { int id; float grade; printf(“enter student number: ”); scanf(“%d”, &id); printf(“enter student grade: ”); scanf(“%f”, &grade); if (grade <= 60) printf(“%d grade is faile\n”, id); if (grade >= 60) && (grade < 70) printf(“%d grade is pass\n”, id); if (grade >= 70) && (grade < 80) printf(“%d grade is good\n”,id); if (grade >= 80) && (grade < 90) printf(“%d grade is very good\n”, id); if (grade >= 90) && (grade <=100) printf(“%d grade is excellent\n”, id); }

  5. This is structure called nested if #include<stdio.h> void main() { int id; float grade; printf(“enter student number: ”); scanf(“%d”, &id); printf(“enter student grade: ”); scanf(“%f”, &grade); if (grade <= 60) printf(“%d grade is faile\n”, id); else if (grade >= 60) && (grade < 70) printf(“%d grade is pass\n”, id); else if (grade >= 70) && (grade < 80) printf(“%d grade is good\n”,id); else if (grade >= 80) && (grade < 90) printf(“%d grade is very good\n”, id); else if (grade >= 90) && (grade <=100) printf(“%d grade is excellent\n”, id); else printf(“out of the range”); }

  6. Another example: write a program to print the name of a day’s week (i.e. we have 7 days in a week numbered from 0 to 6) according the following table: 0 Saturday 1 Sunday 2 Monday 3 Tuesday 4 Wednesday 5 Thursday 6 Friday

  7. #include<stdio.h> void main() { int n; printf(“ Enter the day number: ”); scanf(“%d”,&n); if (n == 0) printf(“\n Saturday”); if (n == 1) printf(“\n Sunday”); if (n == 2) printf(“\n Monday”); if (n == 3) printf(“\n Tuesday”); if (n == 4) printf(“\n Wednsday”); if (n == 5) printf(“\n Thursday”); if (n == 6) printf(“\n Friday”); }

  8. Switch() statement: You can use switch() to represent if() not if () else. It is very simple to chose one thing from a set of choices have the same type. Syntax: switch( var ) { case v1: statement; break; case v2: statement; break; . . . default : statement; }

  9. Example: solve the same last program by using switch. #include<stdio.h> void main() { int n; printf(“ Enter the day number: ”); scanf(“%d”,&n); switch ( n ) { case 0: printf(“\n Saturday”); break; case 1: printf(“\n Sunday”); break; case 2: printf(“\n Monday”); break; case 3: printf(“\n Tuesday”); break; case 4: printf(“\n Wednsday”); break; case 5: printf(“\n Thursday”); break; case 6: printf(“\n Friday”); break; default: printf(“\n out of range ….”); } }

  10. Ass. Write a program to print a menu on the output screen. But your choices are character . Using if() and switch() at each time. Due next lecture.

  11. Loops: you can use loop when you have a number of iteration, or you need to continue a such process until enter special character (like Y or N) or number (like -1) to exit. There are a different kind of loop in C program like while(), do…while() and for()

  12. While () loop Syntax: initialize variable; while(condition) { statements; increment or decrement variable; }

  13. Example: write a program to enter id_number of students and print what you read until enter (N or n). #include <stdio.h> void main( ) { char ch; int id; printf(“Do you want to enter a student numder?(Y\N)”); scanf(“%c”,&ch); while((ch !=‘N’)&&(ch !=‘n’)) { printf(“\n id number is: ”); scanf(“%d”, &id); printf(“id= %d \n”,id); printf(“Do you want to enter a student numder?(Y\N)”); scanf(“%c”,&ch); } printf(“\n end the program”); }

  14. do … while() loop Syntax: initial_value; do { statements; increment or decrement; } while(condition);

  15. Ass. Write the same last program by using do… while() Due next lecture

  16. for () loop Syntax: for (initial value; condition; increment or decrement variable) { statements; }

  17. Example: write a program to print this shape. * * * * * * * * * *

  18. This is structure called nested for (nested loop) #include <stdio.h> void main( ) { inti, j, k, n; printf(“\n enter number of lines: ”); scanf(“%d”, &n); for (i=1; i <= n; i++) { for(j=n; j > i; j--) { printf(“ ”); } for (k=1; k<= i; k++) { printf(“*”); } printf(“\n”); } }

More Related