Upload
frederica-waters
View
228
Download
1
Embed Size (px)
Citation preview
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’);}
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
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 &.
%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
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
%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
Decision making & branching
if statement
Switch statement
Conditional operator statementgoto statement
Simple if statement
if …else statement
Nested if …else statement
else if ladder
Simple if statement Example
General form
if( test expression){
statement-block;
}
. . .…If ( a==b){
x=a*b+20;y=b/x;
}….…..
Decision making & branching
• 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
• 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); }}
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”);
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;
}
Iteration (Looping)
sum =0;label:
sum=sum+1;if (sum <5){ goto label;
}
Sum012345
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 =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
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++; } }