0% found this document useful (0 votes)
12 views

Unit 2 c

The document discusses input/output operations in C programming, detailing both formatted and unformatted I/O functions, such as printf() and scanf(). It also covers control structures, including decision-making statements (if, if-else, nested if) and looping constructs (for, while, do-while), along with their syntax and usage. Additionally, it introduces the switch statement for multi-way decision making and explains the use of break and continue statements within loops.

Uploaded by

sunnyharne0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit 2 c

The document discusses input/output operations in C programming, detailing both formatted and unformatted I/O functions, such as printf() and scanf(). It also covers control structures, including decision-making statements (if, if-else, nested if) and looping constructs (for, while, do-while), along with their syntax and usage. Additionally, it introduces the switch statement for multi-way decision making and explains the use of break and continue statements within loops.

Uploaded by

sunnyharne0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 23
2.11/O Operations (Functions) Input/output operations are used to take data from the user via standard input device (keyboard) and display output to the standard output device (monitor). In order to write an interactive C program, we require certain input and output functions. A set of standard library functions are available in C which includes a number of input and output functions. These functions permit the programmer. to transfer information between the computer and standard input/output devices. These types of functions can be classified as i) Unformatted I/O functions and ii) Formatted I/O functions. * List of formatted. and unformatted output functions are shown in the Figure 2.1. Input/Output functions Unformatted functions Output peteh() | putch() getche( ) putehar( ) getchar() 2S gets(), puts() input Formatted functions Output printf) Input ——>-] Input seanf( } suing 2.1.1 Formatted I/O Functions In this type of’ functions. we «specify format | string or control string to take data or display data. There are two very popular functions are. used for formatted input and formatted output functions as: er a) printf() function b) scanf() function a) printf() Function: 1. This is output function: . 2. printf function is:formatted output function. 3. The general format of the printf statement is as follows: : a printf(“Control String”, Argument List); 4. This function is available in stdio.h header file. 5. Some commonly used formats are as %d for int data type = %f for float data type Ne for char data type 6. The Control String or format types of items; the first type is made up of character type that will be printed on the . Screen as it appears in the statement. string'consists of two o ane ee . The second type consists of command thar define the way the arguments are displayed, known as format. The formats always begin with % sign and followed by format code. . The format command may have modifiers that specify the field width, the number of decimal places and the left or right justification. Scanf Function: . This is an input function. » scanfis formatted input function... *, . This function is.used to-take: forrnatted input from the keyboard. 4. It can read all type of built in data types. - The general format of this. statement..is. as follows: : scanf(“Control String”, Argument List);' The Control String contains field specification consisting of a conversion character % and a type specifier. Control String» may’ ‘coritain blanks, tabs, new line character etc. - eae etchar getch FF . : = Tee i Single Single Single Character character character input input input function, function. , | function... _| The getch() is a | The getche() is The getchar() is function, a function. a micro. | This function | This function | This’) function will read} will read alrequires an isingle single enter key to character character write the without without 4:| character. that waiting for the | waiting for |‘we typed. enter key to the enter key be pressed. to be pressed. The etch ‘in| The getchar function just getche function returns the | function display the character that | means it | chatacter, you you typed, displays . the typed, to. the without : character that screen and displaying. it | you typed ‘to | wait for on the screen. |-the screen. another to be typed, 2.2 Control Structures ‘A program is a collection of instructions (statements). Each program is executed in a sequence. The instructions are executed in the same order in which they written in a program. But sometimes we want to change the order of execution of*instructions or répeat a group of instructions (statements) in a program. This can be achieved with the help of control statements. "A. statement which is used to change the order of execution of statements based on certain conditions or repeat a group of statements until certain specified ‘condifions are met is known as control statement.” That is the flow ~ of instructions is controlled by some’.statements, they are called control statements. Changing the flow of control might. j involve one of the following situations: (i) Jumping (ii) Branching (iii) Looping. C provides the: following control structures for Controlling the order of execution of statements are: i : 1. The Decision Control structure _ 2. The Loop Control structure 3. The Case Control structure. . Problem Solving Using C WV 010 2-25 2.3 The Decision Control Structure In a program, many times we want a set 9 instructions to be executed in one way and othe, set of instructions to be executed in another way. “A branching is one of several’ possibj actions will then be carried out depending on th outcome of the logical test." This kind of situation is control using a decisio: control instruction, they are: (1) simple if statement (2) — if..else statement (3) _ Nested if...else statement (4) else ..if ladder. ; Roa syntax, of if and if-else statement with example. (W-17 for 6 marks) : Explain different forms of If statement with example. (W-18 for 6 marks) 2.3.1 Simple if Statement The general form of’ simple if statement is as follows, ; : if(Test Expression) _ { True Statement Block; } Statement x; 2.3.2 if-else Statement Sometimes we come at the situation when we have two choices and among them only one will be possible at the moment then we have if...else statement. The if...else is the extension of the simple if statement: The syntax of the if...else statement is as follows ' : _ if(Test Expression) © { True Statement Block;: } else ee { False Statement Block; . } : Statement x; If the Test Expression is true the True Statement Block will be executed otherwise False Statement Block will be executed. In either case True Statement Block or False Statement Block will be executed. pifferences between Conditional Operator and jf..else Statement j. Program using conditional operator, becomes more concise as compare to if...else statement. g. Readability of the program is less if conditional operator is used; as compare to the if..else statement. ‘3. For more complicated: conditions ®oiditional operator is not the better choice as compare to if..else statement. Explain nested if structure with example. (W-16/19 for 6 marks) ean nested if with its eee suitable example. ‘ (5-23 for 8 marks). 2.3.3 Nested if...lse Statement When a series of decisions are involved, we may have: to-use: more than one ‘f...else statement in the nested format as follows: ’ 2.3.4 if...else Ladder (else-if statement) The if..else ladder is used when we have multi ple decisions. It contains a chain of ifs in which the statements associated with each else is an if. The general form of the if...else ladder is as-follows. if(test condition1) statement1; : else if(test condition2) -statement2;. else if(test condition3) statement3; | else if(test cond en n) ie os | statement n; else statement; statement x; Here test conditions are evaluated from top to - bottom.” As soon as a true condition is found, statement associated with it is executed. Then after this execution the control is transferred to outside the ladder at statement x by skipping the rest of the ladder. If all the .test conditions became false then the final else block is executed. Flowchart of the if...else ladder is as follows: - False Test condition n Statement n Statement x ———————— 2.4 The Loop Control Structure This type of control structure is required when we need to execute some instructions more than one times..In this type control structure we specify a number to repeat instruction. Repetition may be control by either a specified number of times or until some logical condition is being satisfied. This is known as looping. This is done through a loop control structure. A loop control structure may be divided into two categories. namely entry control loop and exit control loop. : Entry : Entry rst loop statements Pose Test rep Sirona | SN Condition, 7 res a ee : . a loop statements . : ' J : . True * (a) Entry Control (b). Exit Control (i) Entry Control Loops: In this case, the Condition is tested before the start of the loop €xecution. If the condition is true then execute the body of loop and go back to condition. Otherwise body of loop will not be executed and nce execute the statement after control Structure. ’ (ii) Exit Control Loop: In this case the condition 1S tested at the end of the body of the loop. If the “ondition is true then go to back in to the loop therwise go out of the loop. Problem Solving Using C Ww = False ~~. Test. True < pos Condition oe 2-37 2.4.1 for Loop The for loop is very commonly used loopin statement in the C. the general form of the fo loop is as follows: for(expression1; expression2; expression3) { loop statements; Where expression1 is used to initialize some variables that coritrols the looping action and thi variable is called loop control variable. expression2 is the condition that must be true for the loop to continue execution, expression3 is used. to increment/decrement the loop variable. Loop statements are -either simple or compound statements. If the loop statements are more than one then curly brace are needed. . The execution of the for loop is as follows: The expression’ is executed first, and then expression2 is evaluated and tested at the beginning of each pass, through the loop. If it is true then Joop statements will be executed. and expression3 is evaluated at the end of each pass. 2.4.2 while Loop The while ‘statement is used to carry out loo pi operation in which a group of statemen t executed repeatedly until some condition h been satisfied. The general form of the while lo is as follows.. coe 4 while(test condition). { loop statements; Where loop statements are simple or compou! statement. If the loop statements are more tha one then curly brace are needed. while loop the entry control loop statement.. The te condition is evaluated first and if this condit . is true the loop statements are executed, the control is transfer again to check test condition This process will continue till the test.condition true. As soon as thé test condition becomes loop will exit. The flowchart representation of while loop is as follows. : ; 2.4.3 do-while Loop ; i ed for looping 4 The do-while statement is use¢ ; ° this is a modification of the while statement, ¢ In this structure the statements in the log are executed and the condition testing is don at the end of the loop. e. The general form of this statement is do ‘ { loop statements; } while(test condition); * The loop statements will be executed repeated as long as the test condition is true. are more thdn one * jis loop is also known as exit controle loop. The flow chart for do-while loop is give below 1 Difference between for Loop and do-whi e Loop 1. for loop is entry control loop: but do.. .while loop is exit control loop. 2.In the do..while loop, loop counter initialized, tested and incremented th different places: of the program, but in the fo loop all three are performed in one line. ; 3. We can initialize. more than one variables i - the initialization section in the for loop b do..while loop does not has initialization section. ; 4. We can increment more than one variable ir the increment section of the for loop but do...while loop does not has separate increment section. ae 5. All three initialization, condition testing and incrementing are perform at one place in the for loop so visibility of the loop is more thar do...while loop. Difference between while Loop and for Loop 1. In the while loop, loop counter is initialized, tested and incremented three different places of the program, but in the for loop all three are performed in one line. Both while loop and for loop are entry control loops. We can initialize more than one variables in the initialization section in the for loop but while loop does not has initialization section. We can increment more than one variable in the increment section of the for loop but while loop does not has separate increment section.. All three initialization, condition testing and incrementing are perform at one place in the for loop so visibility of the loop is more than while loop. 2.4.5 break Statement We often want-to jump out from the loop switch statement without executing any oth statement. The keyword break allows us to do th; task: The break statement is used to terminate to exit from the loop or switch statement. ‘We c use break statement with in'while loop, do...whi _loop, for loop and switch statement. : When the keyword break is ‘encount inside the loop; control'is ‘automatically jump the first statement after the loop. Generally thi statement came with one condition as shown i the following example program. 2.4.6 continue Statement In some programming situations we want to take control at the beginning of the loop. We want to by pass all the statements inside the loop, which have not yet been executed. The continue statement is used to skip (by pass) those statements. The loop does not terminate when continue statement is encountered. This Difference between break and continue Statements 1. break statement transfers control outside the loop, while continue statement transfers control at the beginning of the loop. 2. break statement terminates the loop while continue statement does not terminate loop. j 3. break statement is used in loops and switch statement while ‘continue statement is used in the loops only. 2.5 Comma Operator ¢ The symbol for the Comma Operator in C is (,) In C, the comma operator has the lowest level of importance. * In C, the comma operator guarantees that two or more expressions , are, .evaluated sequentially from left’ to’ right, with the value of the ‘rightmost expression serving as the result of the whole expression. * The comma operator in C acts on ‘the first operand that is available, discards the result, eyaluates the following operands, and then. returns the value. - : Uses of the Comma Operator in C * In C; we utilize the comma operator to divide up two or more expressions. Where expression1 is evaluated first, followed by expression 2, and the result for the complete expression is the value of expression 2. 2.6 The Case Control Structure In this type of control structure we have two control statements as 1) switch Statement 2) goto Statement 2.6.1 switch Statement .. The switch statement is -a° multi-way decision making statement. It is used to choose a particular group of statements from’ several available group of statements: The séléction is Two Control Structures % Losps pased upon the correct value of an expression which is included with in the switch statement. The switch has the following format (syntax). switch(expression) e constant 1: statement block 1; j break; case constant 2: statement block 2; break; case constant n: statement block n; break; default: default statement block; ) Where the expression is an integer’ expression or Constant. constant 1, constant 2, ......, constant: n are Constant expressions and are known as’ case labels and it is ends with colon (:). Each case label must be unique with ‘in a switch statement. When the switch is executed, the! value of €xpression is successively..compared ‘against © values constant 1. constant 2:..constant n. If ‘a ase is found whose value matches with; the Value of the expression, then the statements that follow the case are executed. § “The: break statement at the end of. each tatement block signals the end of a particular P "Oblem Solving Using C BAi oe, 2-57 2.6.2 goto Statement In addition to break and continue statement C supports goto statement to branch unconditionally from one point of program to another in a program. : mms i! The goto requires a label in order to identig, ‘ place where a branch is to be made. A label js valid variable name, and must be followeq colon. The label is placed immediately before the statement where the control is to be transferreg| The general form of goto and label statement are as follows. th a goto label; ead ry aren statement; | label: a re te Statement; ~ goto label; a Forward jump : Backward jump 4 The label'can be anywhere in the program either before or after goto statement. ‘Note that a goto breaks -normal- sequential.’ execution of program. If the label. is before the statement goto; a loop will be perform and some statement will be | executed repeatedly. This type of jump is knows as backward jump. On the other hand if a label is placed after the goto Statement then some statement will be skipped and thi jump is known as forward pemes tiie bRecord

You might also like