1 / 67

UNIT II

UNIT II. Input Output Statements In ‘C’ language, two types of Input/Output statements are available and all input and output operations are carried out through function calls. These function collectively known as standard I/O library. 1.unformated Input/Output statements

faye
Download Presentation

UNIT II

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. UNIT II

  2. Input Output Statements In ‘C’ language, two types of Input/Output statements are available and all input and output operations are carried out through function calls. These function collectively known as standard I/O library. 1.unformated Input/Output statements 2.Formated Input/Output statements Unformatted Input/Outputstatemetns These statements are used to Input/Output a single/group of characters from/to the input/output devices.Here the user cannot specify the type of data i.e, going to be input/output.

  3. Input/output Functions

  4. The following are the unformatted Input/output statements available in c: Single Character Input-getchar() function A single character can be given to the computer using ‘C’ input library function getchar(). The getchar() function is written in standard I/O library. It reads a single character from a standard input device.

  5. Example: /* Program for testing character type */ #include<stdio.h> void main() { char ch; printf(“Enter any character/digit”); ch=getchar(); if(isalpha(ch)>0) printf(“It is a alphabet”); else if(isdigit(ch)>0) printf(“It a digit”); else printf(“It is alphanumeric”); }

  6. Single Character Input-putchar() function The putchar() function is used to display one character at a time on the standard output device. Example: #include<stdio.h> void main() { char ch; printf(“Enter any alphabet either in Lowercase or Uppercase”); ch=getchar(); if(islower(ch)) putchar(toupper(ch)); else putchar(tolower(ch)); }

  7. getc() function This is used to accept a single character from the standard input to a character variable. Syntax: putc() function This is used to display a single character in a character variable to standard output device.

  8. gets() and puts() function: The gets() function is used to read the string(string is a group of characters) from the standard input device(keyboard). The putc() function is used to display/write the string to the standard output device(Monitor).

  9. Example #include<stdio.h> void main() { char name[40]; puts(“Enter Name:”); gets(name); puts(“Print Name:”); put(name); }

  10. Formated Data Input Output Functions:Formated Input/output that has been arranged in a particular formatExample : LAK 397 The following are the formatted Input/Output statements: scanf() Function The scanf() function is used to read information from the standard input device(keyboard),scanf() function starts with a string argument and may contain additional arguments.

  11. Control String: The below table illustrates code formats(control strings) in Input/Output statements. Each variable name(argument) must be proceeded by an ampersand(&).The ‘&’ symbol “address of” the variable.

  12. Rules for writing scanf() function 1.The control string must be preceded with(%) sign and must be within quotations. 2.If there is a number of input data items, items must be separated by commas and must be preceded with(&) sign. 3.The control string and the variables going to input should match with each other. 4.It must have termination with semicolon. 5.The scanf() reads the data values until the blank space n numeric input or maximum number of character have been read, or error is detected.

  13. printf() Function: The function is used to output any combination of data. It is similar to the input function scanf(). Rules for writing printf() function a)Place appropriate headings in the output. b)The variable must be separated by commas, and need not be preceded with ‘&’ sign. c)The control string ad the variables must match in their order. d)The control string must be in quotations and there we can also use any other text to print with data. e)Print special messages wherever required in output.

  14. CONTROL STRUCTURE • When a program is given to a computer, it carries out the instructions in the sequential order. This is called the normal flow of control. • The C statements used to change the control flow are called thee C control statements or control structures. • The following control structures: • Logical IF structure • IF-ELSE structure • Nested if…else statement • If…else ladder • Unconditional GOTO statement • Switch structure

  15. Logical IF Structure The logical IF statement is a decision making statement. It is used to control the flow of execution of the statements and also used to test logically the condition is true or false. Syntax if(condition is true) { true statements; } Where condition->is a logical condition statement->executable statement

  16. Condition F T True Statement

  17. Properties of an If statement: 1.If the condition is true, then the simple or compound condition statements are executed. 2.If the condition is false, it does not do any thing. 3.The condition is given in parenthesis and must be evaluated as true(non-zero value) or false(zero value) 4.If a compound structure is provided, it must be enclosed in opening and closing braces. Example: #include<stdio.h> #include<conio.h> void main() { int x=10,y; clrscr(); if(x==10) printf("TRUE"); getch(); }

  18. If-else statement • It is used to control the flow of execution and also used to carry out the logical test then pickup one of the two actions depending on the logical test. Syntax: if(condition) { True statement; } else { False statement; }

  19. Condition T F True Statement False Statement

  20. #include<stdio.h> #include<conio.h> void main() { int num,rem; clrscr(); printf(“Enter your number:”); scanf(“%i”,&num); rem=num%2; if(rem==0) printf(“The entered number is even.”); else printf(“The entered number is odd.”); }

  21. Nested if…else statement:When series of if…else statements are occurred in a program entire if…else statement in another if…else statement called nesting,& the statement is called nested if. Condition1 F if(condition 1) { if(condition 2) { True statement2; } else { False statement2; } } else { False statement1; } False statement 1 T Condition2 T F True statement2 False statement2

  22. if…else Ladder: Nested if statements can become quite complex. If there are more than three alternatives & indentation. Syntax: if(condition 1) { statement 1; } else if(condition 2) { statement 2; } else if(condition 3) { statement 3; } else { default-statements; }

  23. T F Condition 1 Condition 2 T F Statement 1 F Condition 2 T Statement 2 Statement3 Default statement

  24. goto statement C Provides the goto statement to transfer control unconditionally from one place to another place in the program. A goto statement can cause program control almost anywhere in the program unconditionally. The goto statement requires a label to identify the place to move the execution.A label is a valid variable name and must be ended with colon(:). Syntax: goto label; label: …… ……. …… ……. label: goto label;

  25. Example: #include<stdio.h> void main() { int a,b; printf(“\n Enter the number”); scanf(“%d %d”,&a,&b); if(a==b) goto equal; else { printf(“\n A and b are not equal”); exit(0); } equal: printf(“A and B are equal”); }

  26. switch statement • The switch statement is used to pickup or execute a particular group of statements from several available group of statements. • It is multiway decision statement it test the value of given variable or expression a list of case values & when a match is found, a block of statements associated with case is executed. Syntax: switch(expression) { case label1: statements; break; case label2: statements; break; default: statements; break; }

  27. switch case 1 statements case 2 statements case 3 statements

  28. Rules for writing switch() statement 1.The expression in switch statement must be an integer value or a character constant. 2.No real numbers are used in an expression. 3.Each case block and default blocks must be terminated with break statements. 4.The default is optional and can be placed anywhere, but usually placed at end. 5.The case keyword must terminate with colon(:). 6.No two case constants are identical. 7.The case labels must be constants. 8.The value of switch expression is compared with the case constant expression in the order specified. 9.In the absence of break statement, all statement that are followed by matched by matched cases are executed.

  29. Example: #include<stdio.h> void main() { int a=1; switch(a) { case 1: printf(“I am in case 1 \n”); break; case 2: printf(“I am in case 2 \n”); break; case 3: printf(“I am in case 3 \n”); break; } }

  30. Comparison of switch() case and Nested if

  31. Branching and Looping • Repeat a set of instructions in specified number of times or until a particular condition is statified.This reparation is known as Looping. • The loop in a program consists of 2 parts: 1.body of the loop 2.Control statement • The control statement is used to test a condition and the body of the loop executed repeatedly. • In C there are 3 looping constructs: 1.for loop 2.while loop 3.do…while loop

  32. while loop • In while, used to execute the statements within the body until the condition becomes false. • The while loop statement, the condition is evaluated first if it is true, the body of the loop evaluated. The process is repeated until the test condition false, then the control is transferred out of the loop. T Condition True statement F

  33. Syntax while(text expression) { sequence of statements; } Example: /* Addition of numbers upto 10 using while loop #include<stdio.h> void main() { int i=1,sum=0; while(i<10) { sum=sum+i; i++; printf(“The sum of numbers upto 10 is %d”,sum); }

  34. do…while loop • “do-while” is an exit control loop statement i.e., condition is tested only after executing a loop. In some situations it may be necessary to execute the body of the loop before the test condition is performed, in such a situation do…while loop is useful. Syntax: do { body of the loop; }while (condition); Body of the loop T Condition F

  35. /*Addition of numbers using do…while loop */ #include<stdio.h> void main() { int i=1,sum=0; do { sum=sum+i; i++; }while(i<10); printf((“The sum of numbers upto 10 is %d”,sum); }

  36. /* Program to print n numbers using do…while loop */ #include<stdio.h> #include<conio.h> void main() { int i,n; printf(“Enter the number”); scanf(“%d”,&n); i=0; do { printf(“the number are %d \n”,i); i=i+1; } while(i<n); }

  37. Comparison between while and do while statements.

  38. for loop The for loop is another repetitive control structure. and is used to execute set of instructions repeatedly until the condition becomes false. The assignment,incrementation or decrementation and condition checking is done in for statement only, where other control structures are Syntax: for(initialization; text-expression; updation) { statements; }

  39. 1.Initialization statement is executed. 2.Test-expression is evaluated. 3.If the test-expression evaluates to true, the statements in the body of the loop would get executed. 4.Control goes to the updation statement, the updation statement is executed, which changes the value of the loop variable. 5.Test-expression is again evaluated. 6.If it evaluates to true, again the statements in the body of the loop would get executed. 7.Conrol once again goes back to the updation statement, which updates the looping variable. The sequence is repeated as long as the test-expression to true. Once the test-expression evaluates to false, the loop is exited and the control is transferred to the first statement following the looping construct.

  40. Initialization Test-expression Next Statement T Statements

  41. /* To find sum of N natural numbers using for-loop */ #include<stdio.h> void main() { int n,i,sum; printf(“Enter a number \n”); scanf(“%d”,&n); sum=0; for(i=1;i<=n;i++) sum+=i; printf(“Sum=%d \n”,sum); }

  42. /* To Print numbers from 1 to 10 and their squares */ #include<stdio.h> void main() { int i; printf(“Number \t Squares \n”); for(i=1;i<=10;i++) printf(“%d \t\t %d \n”,i*i); printf(“\n Press any key to continue”); }

  43. Nested Loops Any loop can be placed within another loop. Such loops are called nested loops. Rules for implementing the nested for loop: 1.Each loop should have a unique index variable. 2.The loops should not overlap each other. 3.The loops should be completely embedded within each other. 4.The loop can have any number of exit points inside the range, but should not permit entry to it. Syntax: for(initialization; text-expression; updation) { for(initialization;text-expression;updation) { statements; } statements; }

  44. /* Program for Pascaline triangle */ #include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\t",i); } printf("\n\n"); } }

  45. Comma Operator • The (,). • comma operator is used primarily in association with the ‘for’ statement. This operator permits two different expressions to appear . • Two or more variables van be initialized and used in the ‘for’ statement using a comma operator. Syntax 1: for(expression1a,expression1b;expression2;expression3) { statements; } where expression1a and expression1b are two expressions separated by the comma operator. These two expressions initialize two separate indices that would be simultaneously used within the for loop.

  46. /* Program prints the numbers from 1 to 10 and 10 to 1 */ void main() { int a,b; for(1=1,b=10;a<=10;a++,b--) printf(“a=%d,b=%d”,a,b); } Syntax 1: for(expression1;expression2;expression3a,expression3b) { statements; } where expression 3a and expression 3b separated by the comma operator.

  47. /* Program for print numbers from 1 to 10 */ #include<stdio.h> void main() { int a,b; for(a=0,b=10;a!=b;a++,b--) printf(“a=%d,b=%d”,a,b); }

  48. break statement • The break statement is used to terminate loops or exit from a switch. • It can be used within a do-while,for and switch statement. • When used within a switch-case statement, control is transferred to the end of the construct. Example: #include<stdio.h> void main() { int x=1; while(x<=10) { printf(“x=%d \n”,x); if(x==5) break; x++; } }

  49. Continue Statement • The continue statement can be used to skip the rest of the body of an iterative loop. Syntax: continue; The continue statement can be included only in a while,do-while or for statement. It is simply written as continue.

  50. /* To print odd numbers using coninue statement */ #include<conio.h> void main() { unsigned x; x=0; clrscr(); while(x<10) { ++x; if(x%2==0) { continue; } printf("%i is an odd number. \n",x); } }

More Related