c programming input output conditional statements and iterative statements rohit khokher

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

Upload: frederica-waters

Post on 17-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

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

C Programming

Input output Conditional statements and Iterative statements

Rohit Khokher

Page 2: 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 (“\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’);}

Page 3: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

Formatted Input

Data represented in a format as shown below:A 15.75 123 John

Real number with two decimal digits

IntegerString

%wd

Format specification

Fieldwidth

Specifies a data to be read

%f

Specifies a real data to be read

Character

%wc%ws

Page 4: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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);

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 &.

Page 5: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

%c Read a single character

%d Read a decimal integer

%f Read a floating point value

%e Read a floating point value

%g Read a floating point value

%h Read a short integer

%i Read a decimal, hexadecimal or octal integer

%o Read an octal integer

%s Read a string

%u Read an unsigned decimal integer

%x Read a hexadecimal integer

%[..] Read a string of words

Commonly used scanf format code

Page 6: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

Formatted outputscans (“format control string”, arg1,arg2, arg3, …, arg n)

Display heading specificationDisplay format specification Escape sequences \n, \t, \b

“The out put of the program is”

Integer %wdReal numbers %w.pf or w.pe where w>=p+7

String %w.ps or %wc

Page 7: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

%c Print a single character

%d Print a decimal integer

%f Print a floating point value

%e Print a floating point value

%g Print a floating point value

%h Print a short integer

%i Print a decimal, hexadecimal or octal integer

%o Print an octal integer

%s Print a string

%u Print an unsigned decimal integer

%x Print a hexadecimal integer

Commonly used printf format code

Page 8: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

Decision making & branching

if statement

Switch statement

Conditional operator statementgoto statement

Simple if statement

if …else statement

Nested if …else statement

else if ladder

Page 9: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

Simple if statement Example

General form

if( test expression){

statement-block;

}

. . .…If ( a==b){

x=a*b+20;y=b/x;

}….…..

Decision making & branching

Page 10: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

• 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

Page 11: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

• 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); }}

Page 12: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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”);

Page 13: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

The switch statementswitch (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;

}

Page 14: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

Iteration (Looping)

sum =0;label:

sum=sum+1;if (sum <5){ goto label;

}

Sum012345

Page 15: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

While & do while

• while (expression){

...block of statements to execute...

• }

• do{

block of code

} while (condition is satisfied);

Page 16: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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; }

Page 17: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

Algorithm tracing

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

. . . . . . . . . . . . .

value r_digit

986 6

98 8

9 9

0

Page 18: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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”); }

Page 19: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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;

}

Page 20: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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);      } }

Page 21: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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);   }

Page 22: C Programming Input output Conditional statements and Iterative statements Rohit Khokher

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++;     }       }