220 likes | 364 Views
C Programming. Input output Conditional statements and Iterative statements Rohit Khokher. Input & output in C. Reading a Character from a keyboard variable_name =getchar (); #include <stdio.h> void main () { char x; x= getchar (); printf (“<br> Input = %c”,x) }.
E N D
C Programming Input output Conditional statements and Iterative statements RohitKhokher
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’); }
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
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.
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
Decision making & branching Simple if statement if …else statement if statement Nested if …else statement else if ladder Switch statement Conditional operator statement gotostatement
Decision making & branching Simple if statement Example . . . … If ( a==b) { x=a*b+20; y=b/x; } …. ….. General form if( test expression) { statement-block; }
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; }
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); } }
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;
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; }
Iteration (Looping) sum =0; label: sum=sum+1; if (sum <5) { goto label; } Sum 0 1 2 3 4 5
While & do while • while (expression){ ...block of statements to execute... • } • do{ block of code } while (condition is satisfied);
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; }
Algorithm tracing . . . . . . . . value =986 do{r_digit = value % 10;printf(“%d”, r_digit);value = value / 10;} while (value != 0); . . . . . . . . . . . . .
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”); }
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;}
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); } }
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); }
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++; } }