1 / 22

C Programming

C Programming. Input output Conditional statements and Iterative statements Rohit Khokher. Input &amp; output in C. Reading a Character from a keyboard variable_name =getchar (); #include &lt;stdio.h&gt; void main () { char x; x= getchar (); printf (“<br> Input = %c”,x) }.

dunn
Télécharger la présentation

C Programming

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. C Programming Input output Conditional statements and Iterative statements RohitKhokher

  2. Input & output in C • Reading a Character from a keyboard • variable_name =getchar (); #include <stdio.h> void main () { char x; x= getchar (); printf (“\n Input = %c”,x) } • Writing a character one at a time to screen. • variable_name =putchar(); #include <stdio.h> void main () { char x; x= getchar (); putchar (x}; putchar (‘\n’); }

  3. Formatted Input Data represented in a format as shown below: A 15.75 123 John Character String Real number with two decimal digits Integer %wc %wd %f %ws Format specification Field width Specifies a data to be read Specifies a real data to be read

  4. scans (“format control string”, arg1,arg2, arg3, …, arg n) A 15.75 123 John char x; float y; int z; char name[3]; scanf(“%c %f %d %s”, x, &y, &z, name); String and character type declarations provide the address of the first character so we don’t need &. scanf accepts the address of y and z so we need & operator that gives the address of the variables y and z.

  5. Commonly used scanf format code

  6. Formatted output scans (“format control string”, arg1,arg2, arg3, …, arg n) “The out put of the program is” Integer %wd Real numbers %w.pf or w.pe where w>=p+7 String %w.ps or %wc Display heading specification Display format specification Escape sequences \n, \t, \b

  7. Commonly used printf format code

  8. Decision making & branching Simple if statement if …else statement if statement Nested if …else statement else if ladder Switch statement Conditional operator statement gotostatement

  9. Decision making & branching Simple if statement Example . . . … If ( a==b) { x=a*b+20; y=b/x; } …. ….. General form if( test expression) { statement-block; }

  10. Decision making & branching • if …. else statement • Example . . . If ( a==b) { x=a*b+20; y=b/x; } else { z=a/b; } General form If ( test expression) { True -block; } Else { False-block; }

  11. Nesting of if …. else statement if ( test expression 1) { if ( test expression 2) { True -block; } else { False-block; } else { } if ( A> B ) { if ( A>C) { printf ( “%f\n”,A);} else {printf ( “%f\n”,C);} else { if ( B>C) {printf ( “%f\n”,B);} else {printf ( “%f\n”,C); } }

  12. The else if ladder if ( m > 79) printf(“\n Honours”); else if (m>59) printf(“\n First”); else if ( m>49) printf(“\n Second”); else printf(“\n Fail”); if ( test expression 1) statement 1; else if ( test expression 2) statement 2; else if ( test expression 3) statement 3; else statement 4;

  13. The switch statement switch (expression) { case value : block; break; case value : block; break; ……………. …………….. default: block; break; } switch (i) { case 10 : block; break; case 9: block; break; default: block; break; } switch (ch) { case ‘A’ : block; break; case ‘B’: block; break; default: block; break; }

  14. Iteration (Looping) sum =0; label: sum=sum+1; if (sum <5) { goto label; } Sum 0 1 2 3 4 5

  15. While & do while • while (expression){ ...block of statements to execute... • } • do{ block of code } while (condition is satisfied);

  16. Example while & do while loop #include <stdio.h> int main(void){ int loop = 0; while (loop <=10){ printf(“%d”, loop); ++loop} return 0; } #include <stdio.h> int main(void){ int value, r_digit; printf(“enter a number to be reversed.\n”);scanf(%d, &value); do{ r_digit = value % 10;printf(“%d”, r_digit);value = value / 10;} while (value != 0);printf(\n);return 0; }

  17. Algorithm tracing . . . . . . . . value =986 do{r_digit = value % 10;printf(“%d”, r_digit);value = value / 10;} while (value != 0); . . . . . . . . . . . . .

  18. For loop for (expression_1; expression_2;expression_3){...block of statements to execute...} for (count = 1; count <=10; count = count +1) printf(“%d”, count); for (count = 1; count <=10; count = count + 1) { printf(“%d”, count); printf(“\n”); }

  19. Example for loop #include <stdio.h> int main(void){ int count; for (count = 1; count <=10; count = count + 1) printf(“%d”, count); printf(“\n”); return 0;}

  20. Example for loop #include <stdio.h> void main(){    // using for loop statement   int max = 5;    int i = 0;    for(i = 0; i < max;i++) {       printf("%d\n",i);   } }

  21. Example do while #include <stdio.h> void main(){    int x = 5;    int i = 0;    // using do while loop statement     do{        i++;        printf("%d\n",i);     }while(i < x); }

  22. Loop with break #include <stdio.h> void main(){   int x = 10;   int i = 0;     // when number 5 found, escape loop body    int numberFound= 5;    int j = 1; .    while(j < x){       if(j == numberFound){             printf("number found\n"); break;         }         printf("%d...keep finding\n",j);       j++;     } }

More Related