
Computer Programming: Data Input/Output Functions and Flow of Control
Explore the concepts of data input/output functions such as formatted input and output using scanf and printf in computer programming. Additionally, learn about the flow of control featuring if statements and conditional operators for decision-making purposes.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
099 CS 23 COMPUTER PROGRAMMING Degree 2ndSemester of first year By Dr. Prabakaran Narayanan Associate professor, CSISE DEPT
2.1. DATA INPUT/OUTPUT FUNCTIONS i) Formatted Input ii) formatted Output i) Formatted Input: Formatted input to an input data that has been arranged in a particular format scanf( Control string , arg1, arg2, .., argn); Where Control String field format in which the daa is to be entered (consists of character) Arguments address of locations where the data is stored
2.1. DATA INPUT/OUTPUT FUNCTIONS Example: scanf( %d%d , &a, &b); Scanf( %d%c%f , &a, &b, &c); ii)Formatted Output: Printf function is used to print or display any content is screen (or) monitor. printf function for printing caption and numerical results. Syntax: printf ( control string ,arg1,arg2, argn); Where control string 1.Characters that will be printed on the screen as they appear 2.Escape sequence characters such as in, \t & \b
2.1. DATA INPUT/OUTPUT FUNCTIONS (CONT..) 3.Format specifications that define the output format for display arg1,arg2, .argn are the variables whose values are formatted &printed according to the specification of the control string. Example: printf ( Programming is C ) printf( ); printf( %d ,x) printf( a=%f in b=%f ,a,b); printf( sum =%d , sum); printf( \n\n );
2.2 FLOW OF CONTROL 2.2.1 : if statement 2.2.2 : switch statement 2.2.3 : conditional operator statement 2.2.4 : goto statement
2.2.1 IF STATEMENTS One way selection Two way selection Multi-way selection Multiway selection One way selection If (condition) Statements If(condition) statements-1 Else if statements-2 else if statements-3 Else statements -4 Two way selection if (condition) statement -1 else statement-2
2.2.1 . If statement (cont.) If statement is a powerful decision making statements and is used to control the flow of execution of statements. i) Simple if statements ii) If .else . Statements iii) Nested if .else statements iv) Else if ladder
i) Simple if statements Syntax: if(test expression) { statements- blocks } Statement x Where Statements blocks may be single statements or a group of statements If the test expression if true the statements block will be executed otherwise the statement-block will be skipped and jump to another statement-x
Example of simple if statement int a=21; If(a>18) { } cout << out of the loop ; cout << you are eligible for voting\n ;
ii) if.. Else statements Syntax: if(test expression) { statements- x else statements-y } Statement z Where Statements blocks group of statements If the test expression is true the statements block-x will be executed otherwise If the text expression is false the statements-y will be executed may be single statements or a
Example of if else statement int a=21; If(a>18) { else } cout << out of the loop ; cout << you are eligible for voting\n ; cout << you are not eligible for voting ;
iii) Nested if statements Syntax: if(test expression-1) { } else { statement-z } if(test condition-2) else statements-x statements y may be single statements or a group of statements Statements If the test expression-1 is true it goes to test condition-2 if it is true the statements block-x will be executed If the text expression-2 is false the statements-y will be executed If the text expression-1 is false the statements-z will be executed
Nested if statements Example: int a=21, b=129; If(a>18) { if( b < 100) cout << you are eligible \n ; else cout << you are not eligible ; } Else { cout << I am in false statements ; }
iv) Else if ladder Syntax: If(condition-1) statement -1 Else if( condition-2) statement -2 else if (condition-3) statement -3 else if (condition-4) statement -4 else . . default-statement
The conditions are evaluated from the top of the ladder towards downwards as soon as a true condition is found Ex: if(marks>79) grade= Honours ; else if(marks >59) grade = first class ; else if (marks >49) grade = Second class else grade= fail ;
2.2.2. SWITCH STATEMENT Definition: The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements is executed . Syntax: Switch(expression) { case value 1: .. ... break; case value 2: .. . break; .. ............ default: default block break; }
Example of switch statements Index = mark/10; Switch(index) { case 10: case 9: case 7: case 6: default: } grade = honours ; break; grade = first class ; break; grade= fail ; break;
2.2.3 . Conditional(Ternary Operator) C language has an unusual operator useful for making two-way decisions This operator is a combination of ? And : Syntax: conditional expression ? Expr 1: expr 2; Conditional expression is evaluated first if the result is true expr 1 is evaluated if the result is false expr 2 is evaluated
2.2.3 . Conditional(Ternary Operator) Example: y= (x>2)? (23* x +10) : ( 1.735 * x +3); Write a c program for ternary operator #include <stdio.h> Void main() { int x, y; scanf ( %d , x) y= (x>2)? (2*x+5) : (5* x +3); printf( Ternary operator is =%d , y); }
2.2.4. The GOTO statement C supports the goto statements to branch unconditionally from one point to another in the program. Syntax: Goto label: ............ ............ Label: statements label: goto label; statements .
Goto requires a label in order to identify the place where the branch is to be made A label is any valid variable name & must be followed by a colon Ex: Void main() { double x, y; Read: scanf( %f , &x); if(x<0) goto read; y=sqrt(x); printf( %f%f , x,y); }
2.3 . Loop control Structure Looping: a sequence of statements are executed until some conditions are termination of the loop are satisfied. It has two segments 1. body of the loop repeated execution of the statements 2. control statements