Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Input output Conditional statements and Iterative statements Rohit Khokher.

Similar presentations


Presentation on theme: "C Programming Input output Conditional statements and Iterative statements Rohit Khokher."— Presentation transcript:

1 C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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

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

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

5 %cRead a single character %dRead a decimal integer %fRead a floating point value %eRead a floating point value %gRead a floating point value %hRead a short integer %iRead a decimal, hexadecimal or octal integer %oRead an octal integer %sRead a string %uRead an unsigned decimal integer %xRead a hexadecimal integer %[..]Read a string of words Commonly used scanf format code

6 Formatted output scans (“format control string”, arg 1,arg 2, arg 3, …, arg n ) Display heading specification Display format specification Escape sequences \n, \t, \b “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

7 %cPrint a single character %dPrint a decimal integer %fPrint a floating point value %ePrint a floating point value %gPrint a floating point value %hPrint a short integer %iPrint a decimal, hexadecimal or octal integer %oPrint an octal integer %sPrint a string %uPrint an unsigned decimal integer %xPrint a hexadecimal integer Commonly used printf format code

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

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

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

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 if ( test expression 1) statement 1; else if ( test expression 2) statement 2; else if ( test expression 3) statement 3; else statement 4; 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”);

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 int main(void) { int loop = 0; while (loop <=10) { printf(“%d”, loop); ++loop } return 0; } #include 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);............. valuer_digit 9866 988 99 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 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 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 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 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++; } }


Download ppt "C Programming Input output Conditional statements and Iterative statements Rohit Khokher."

Similar presentations


Ads by Google