Managing input and output operations & Decision making and branching and looping

letheyabala 29 views 83 slides Aug 28, 2024
Slide 1
Slide 1 of 83
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83

About This Presentation

Managing input and output operations & Decision making and branching and looping


Slide Content

UNIT 2 MANAGING INPUT AND OUTPUT OPERATIONS

WHAT IS INPUT AND OUTPUT OPERATIONS?

statement-x;

Switch Statement

SYNTAX OF SWITCH STATEMENT

EXAMPLE, #include <stdio.h> int main() { int num = 2; switch (num) { case 1: printf("Value is 1\n"); break; case 2: printf("Value is 2\n"); break;

case 3: printf("Value is 3\n"); break; default: printf("Value is not 1, 2, or 3\n"); break; } return 0; } OUTPUT: Value is 2

?: Statement in C The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible. It is also known as the ternary operator in C as it operates on three operands. Syntax of Conditional/Ternary Operator in C The conditional operator can be in the form, variable = Expression1 ? Expression2 : Expression3; Or the syntax can also be in this form variable = (condition) ? Expression2 : Expression3;

EXAMPLE, C Program to Store the greatest of the two Numbers using the ternary operator // C program to find largest among two // numbers using ternary operator #include <stdio.h> int main() { int m = 5, n = 4;

(m > n) ? printf("m is greater than n that is %d > %d", m, n) : printf("n is greater than m that is %d > %d", n, m); return 0; } Output m is greater than n that is 5 > 4

GOTO STATEMENT The goto statement is used to transfer the program's control to a defined label within the same function. It is an unconditional jump statement that can transfer control forward or backward. The goto keyword is followed by a label. When executed, the program control is redirected to the statement following the label.If the label points to any of the earlier statements in a code, it constitutes a loop. On the other hand, if the label refers to a further step, it is equivalent to a Jump.

goto Statement Syntax The syntax of goto statement is − goto label; . . . . . . label: statement; The label is any valid identifier in C. A label must contain alphanumeric characters along with the underscore symbol (_). As in case of any identifier, the same label cannot be specified more than once in a program. It is always followed by a colon (:) symbol. The statement after this colon is executed when goto redirects the program here.

Example , #include <stdio.h> int main (){ int i = 11; if (i % 2 == 0){ EVEN: printf("The number is even \n"); goto END; }

else{ ODD: printf("The number is odd \n"); } END: printf("End of program"); return 0; } OUTPUT: The number is odd End of program

DECISION MAKING AND LOOPING While statement The while Loop is an entry-controlled loop in C programming language. This loop can be used to iterate a part of code while the given condition remains true. Syntax The while loop syntax is as follows: while (test expression) { // body consisting of multiple statements }

EXAMPLE, // C program to demonstrate while loop #include <stdio.h> int main() { // Initialization of loop variable int i = 0; // setting test expression as (i < 5), means the loop // will execute till i is less than 5

while (i < 5) { // loop statements printf(" Hello \n"); // updating the loop variable i++; } return 0; }

OUTPUT: Hello Hello Hello Hello Hello while Loop Structure The while loop works by following a very structured top-down approach that can be divided into the following parts: Initialization: In this step, we initialize the loop variable to some initial value. Initialization is not part of while loop syntax but it is essential when we are using some variable in the test expression

Conditional Statement: This is one of the most crucial steps as it decides whether the block in the while loop code will execute. The while loop body will be executed if and only the test condition defined in the conditional statement is true. Body: It is the actual set of statements that will be executed till the specified condition is true. It is generally enclosed inside { } braces. Updation: It is an expression that updates the value of the loop variable in each iteration. It is also not part of the syntax but we have to define it explicitly in the body of the loop.

DO Statement (do...while loop) The do…while in C is a loop statement used to repeat some part of the code till the given condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after executing the body of the loop. Due to this, the statements in the do…while loop will always be executed at least once no matter what the condition is. Syntax of do…while Loop in C do { // body of do-while loop } while (condition);

EXAMPLE, // C Program to demonstrate the use of do...while loop #include <stdio.h> int main() { // loop variable declaration and initialization int i = 0; // do while loop do {

printf("hello\n"); i++; } while (i < 3); return 0; } OUTPUT: hello hello hello

The working of the do…while loop is explained below: When the program control first comes to the do…while loop, the body of the loop is executed first and then the test condition/expression is checked, unlike other loops where the test condition is checked first. Due to this property, the do…while loop is also called exit controlled or post-tested loop. When the test condition is evaluated as true, the program control goes to the start of the loop and the body is executed once more. The above process repeats till the test condition is true. When the test condition is evaluated as false, the program controls move on to the next statements after the do…while loop.

FOR STATEMENT ( for Loop in C ) The for loop in C Language provides a functionality/feature to repeat a set of statements a defined number of times. The for loop is in itself a form of an entry-controlled loop. Unlike the while loop and do…while loop, the for loop contains the initialization, condition, and updating statements as part of its syntax. It is mainly used to traverse arrays, vectors, and other data structures. Syntax of for Loop for(initialization; check/test expression; updation) { // body consisting of multiple statements }

Structure of for Loop The for loop follows a very structured approach where it begins with initializing a condition then checks the condition and in the end executes conditional statements followed by an updation of values. Initialization: This step initializes a loop control variable with an initial value that helps to progress the loop or helps in checking the condition. It acts as the index value when iterating an array or string. Check/Test Condition: This step of the for loop defines the condition that determines whether the loop should continue executing or not. The condition is checked before each iteration and if it is true then the iteration of the loop continues otherwise the loop is terminated. Body: It is the set of statements i.e. variables, functions, etc that is executed repeatedly till the condition is true. It is enclosed within curly braces { }. Updation: This specifies how the loop control variable should be updated after each iteration of the loop. Generally, it is the incrementation (variable++) or decrementation (variable–) of the loop control variable.

EXAMPLE, // C program to demonstrate for loop #include <stdio.h> int main() { int abc = 0; // 'abc' <= 5 is the check/test expression // The loop will function if and only if 'abc' is less // than 5 //'abc++' will increments it's value by this so that the // loop can iterate for further evaluation

// conditional statement for ( abc = 1; abc <= 5; abc ++) { // statement will be printed printf(" Hello \n"); } // Return statement to tell that everything executed // safely return 0; }

Output : Hello Hello Hello Hello Hello