Basics of Control Statement in C Languages

ChandrakantDivate1 87 views 55 slides May 10, 2024
Slide 1
Slide 1 of 55
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

About This Presentation

Basics of Control Statement in C Languages


Slide Content

Subject: Programming In C Language C. P. Divate

C O NT R OL S T A TEME N TS C language supports the following statements known as control or decision making statements. if statement i f …..else statement Nesting of if…..else statement i f ……else ladder switch statement c o n diti o na l o p e r a t or sta t ement Go to statement

IF ( ) STATEMENT if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not. i.e if a certain condition is true then a block of statement is executed otherwise not. Syntax: if(condition ) { // Statements to execute if // condition is true } E xample : if(bank balance is zero) Borrow money

IF ( ) STATEMENT Here, condition after evaluation will result either true or false. if statement condition results in boolean values. if the value is true then it will execute the block of statements below it otherwise not . If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the first immediately below statement to be inside its block.

IF ( ) STATEMENT Example // C program to illustrate If () # include < stdio.h > int main () { int i = 10; if ( i > 15) { printf ("10 is less than 15"); } printf ("I am Not in if"); } Output: I am Not in if #include < stdio.h > int main() { int x = 20; int y = 22; if (x<y) { printf ("Variable x is less than y"); } } Output1: Variable x is less than y

IF ( ) STATEMENT Example Output1: Enter a number:4 4 is positive number // C program to check that no is positive # include< stdio.h > int main () { int number=0; printf ("Enter a number:"); scanf ("% d",&number ); if(number >= 0) { printf ("%d is positive number",number ); } } Output1: Enter a number : -10

IF ( ) STATEMENT Example // C program to check that no is even # include< stdio.h > int main () { int number=0; printf ("Enter a number:"); scanf ("% d",&number ); if(number%2==0 ) { printf ("%d is even number",number ); } } Output1: Enter a number:4 4 is even number Output2: Enter a number:5

IF ( ) STATEMENT Example // C program to check that no is divisible by 7 # include< stdio.h > int main () { int number=0; printf ("Enter a number:"); scanf ("% d",&number ); if(number % 7== ) { printf ("%d is divisible by 7", number); } } Output1: Enter a number:49 49 is divisible by 7 Output2: Enter a number:55

IF ( ) STATEMENT Example // C to calculate fine in library # include< stdio.h > int main () { int days; float fine=0; printf ("Enter no of days:"); scanf ("% d ",&days ); if(days >=8) { fine=(days-8) * 2; } printf (“Fine = %f for Total days=%d”, fine, (days-8) ); } Output1: Enter no of days:10 Fine = 4 for Total days=2 Output1: Enter no of days:7 Fine = 0 for Total days= -1

Multiple IF ( ) STATEMENT Example // C Example of multiple if statements #include < stdio.h > int main() { int x, y; printf ("enter the value of x:"); scanf ("%d", &x); printf ("enter the value of y:"); scanf ("%d", &y); if (x>y) { printf ("x is greater than y\n"); } if (x<y) { printf ("x is less than y\n"); } Output1: enter the value of x:30 enter the value of y:20 x is greater than y End of Program Output2: enter the value of x:20 enter the value of y:20 x is equal to y End of Program if (x==y) { printf ("x is equal to y\n"); } printf ("End of Program"); return 0; }

IF ( ) STATEMENT Home work Example Write a C program to check income is greater than 30000 Write a C program to calculate income_tax if income is greater than 30000 (formula income_tax =30 % of income) Calculate result of student based on following conditions. if percentage < 35 then fail if percentage >= 35 then pass

IF ( ) … else …. STATEMENT if statement is the decision making statement. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false . i.e if a certain condition is true then a block of statement is executed otherwise else block statement is executed. Syntax: if (condition) { // block of code if condition is true } else { // block of code if condition is false }

Here, condition after evaluation will result either true or false. if statement condition results in boolean values. if the value is true then it will execute the true block of if statements below it otherwise it will execute the false block(else block of if statements . IF ( ) … else …. STATEMENT

IF ( ) … else …. STATEMENT Example

IF ( ) … else …. STATEMENT Example // C program to check given number is odd or even number # include< stdio.h > int main () { int number=0; printf ("enter a number:"); scanf ("% d",&number ); if(number%2== 0) { printf ("%d is even number",number ); } e lse { printf ("%d is odd number ", number ); } printf (“End of Program"); } Output1: Enter a number:4 4 is even number Output2: Enter a number:11 11 is odd number

IF ( ) … else …. STATEMENT Example // C Program to check whether a person is eligible to vote or not. #include < stdio.h > int main() { int age; printf ("Enter your age?"); scanf ("% d",&age ); if(age>=18) { printf ("You are eligible to vote..."); } else { printf ("Sorry ... you can't vote"); } } Output1: Enter your age?18 You are eligible to vote... Output2: Enter your age?13 Sorry ... you can't vote

IF ( ) … else …. STATEMENT Example // Program to check whether an integer is positive or negative // This program considers 0 as a positive number # include < stdio.h > int main() { int number; printf ("Enter an integer: “); scanf (“%d”, &number); if (number >= 0 ) { printf ("You entered a positive integer: %d”, number ); } else { printf (" " You entered a negative integer : % d”, number ); } cout << "This line is always printed."; } Output1: Enter an integer: 4 You entered a positive integer: 4. This line is always printed. Output2: Enter an integer: - 4 You entered a negative integer: -4 . This line is always printed .

LOGICAL OPERATORS IN C

LOGICAL OPERATORS IN C Note: a and b are both relational expression here

NESTING OF IF…..ELSE STATEMENTS When a series of decisions are involved, we may have to use more than one if….else statements, in nested form as follows

Here, if the condition 1 is false then it skipped to statement 3. But if the condition 1 is true , then it tests condition 2.  If condition 2 is true then it executes statement 1 and if false then it executes statement 2. Then the control is transferred to the sta t ement x . This can also be shown by the following flowchart, NESTING OF IF…..ELSE STATEMENTS

Program /*Sele ctin g t h e la rg est of t h r ee v a lu es*/ mai n () { float A, B, C; printf(“Enter three values \n”); scanf (“|%f %f %f”,&A, &B, &C ); printf(“\nLargest value is:”); if(A > B) { if(A > C) printf (“%f \n”,A); else printf (“%f \n”,C); } e lse { if(C > B) printf (“%f \n”,C); else printf (“%f \n”,B); } } OUTPUT E n t er th r ee v alues: 5 8 24 Largest value is 24

This construct is known as the else if ladder . The conditions are evaluated from the top, downwards. This can be shown by the following flowchart The else if ladder

The else if ladder When a multipath decision is involved then we use else if ladder. A multipath decision is a chain of if s in which the statement associated with each else is an if . It takes the following general form,

This construct is known as the else if ladder . The conditions are evaluated from the top, downwards. This can be shown by the following flowchart The else if ladder

THE SWITCH STATEMENT Switch statement is used for complex programs when th e numbe r o f al t ernat i v es i n c r ease s . The switch statement tests the value of the given variable against the list of case values and when a match is found, a block of statements associated with that case is executed.

Switch case flowchart

SWITCH STATEMENT Th e g ene r al f orm of sw i t c h sta t ement is switch(expression) { ca s e v a l u e - 1: block-1 break; ca s e v al u e - 2: block-2 break; ……. ……. default: defau l t - b l o c k break; } statement-x;

Exa m ple: index = marks / 10; switch(index) { case 10: c a se 9: c a se 8: grade = “Honours”; break; c a se 7: c ase 6: grade = “first division”; break; case 5: grade = “second division”; break; case 4: g r ade = “thi r d division”; break; default: g r ade = “ f i r s t division”; break } printf(“%s \n”,grade); ………….

THE ?: OPERATOR The C language has an unusual operator, useful for making two- way decisions. Thi s o p e r a t or is a c o m binatio n o f ? a n d : a nd t a k e s t h r ee operands. It is of the form exp1?exp2:exp 3 Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false then exp3 is evaluated and its value becomes the value of the expression . Eg : if(x < 0) flag = 0; else flag = 1; can be written as flag = (x < 0)? : 1;

UNCONDITIONAL STATEMENTS - THE GOTO STATEMENT C supports the goto statement to branchunconditionally from one point of the p r og r am t o an o the r . The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name and mus t be f oll o w ed b y a c olon.

D E CISION MAKING A N D LOOPING In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statements.

Cont.. Depending on the position of the control statements in the loop, a control structure may be classified either as an entry-controlled loop or as the exit-controlled loop.

Loops In C The C language provides for three loop constructs for performing loop operations. The y a r e: The while statement The do statement Th e f or sta t ement

THE WHILE STATEMENT The basic format of the while statement is w hile ( t est c on d ition) { bo d y of the l oop } The while is an entry–controlled loop statement. The test-condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body, the test-condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test- condition finally becomes false and the control is transferred out of the loop.

Example of WHILE Loop Bo d y of th e loop test condn? w h i le( t est c ondition) { bo d y of th e loop } sum = 0; n = 1; while(n <= 10) { sum = sum + n* n; n = n + 1; } printf(“sum = %d \n”,sum);

While loop

THE DO STATEMENT In while loop the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. Such situations can be handled with the help of the do statement. do { body of the loop } while(test condition );

Cont.. Since the test-condition is evaluated at the bottom of the loop, the do…..while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once. Eg: do { p r int f ( “ Input a numb e r \ n ” ) ; number = g etnum (); } w h i le(numbe r > ) ;

Difference between While( ) loop and Do….while() loop

THE FOR STATEMENT Th e f or lo o p i s a n other ent r y - c ont r ol l e d loop t h a t provides a more concise loop control structure The general form of the for loop is for(initialization ; test-condition ; increment /Decrement) { body of the loop }

THE FOR STATEMENT for(initialization ; test-condition ; increment /Decrement) { body of the loop }

Cont.. The execution of the for statement is as follows: Initialization of the control variables is done first. The value of the control variable is tested using the test-condition. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is either incremented or decremented as per the condition.

Cont.. Eg 2) sum = 0; for(i = 1; i < 20; ++i) { sum =sum + i; printf(“%d %d \n”,sum); } for(initialization ; test-condition ; increment / Devrement ) { bo d y of the loop }

For Statement Eg 1) for(x = 0; x <= 9; x = x + 1) { p ri ntf ) ” %d ” , x ); } printf(“\n”); The multiple arguments in the increment section are possible and separated by commas.

Nesting of For Loops C allows one for statement within another for statement.

Cont.. Eg: for(row = 1; row <= ROWMAX; ++row) { for(column = 1; column < = COLMAX; ++column) { y = row * column; p r int f ( “ %4d ” , y); } printf(“\n”); }

JUMPS IN LOOPS C permits a jump from one statement to another within a loop as well as the jump outof a loop. Jumping out of a Loop An early exit from a loop can be accomplished by using the break statement or the goto statement. When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loops are nested, the break would only exit from the loop containing it. That is, the break will exit only a single loop.

Skipping a part of a Loop Like the break statement, C supports another similar statement called the continue statement. However, unlike the break which causes the loop to be terminated, the continue, as the name implies, causes the loop to be continued with the next iteration after skipping any statements in between. The continue statement tells the compiler, FOLLOWING STATEMENTS AND CONTINUE WITH ITERATION”. The format of the continue statement is simply “ S K IP THE TH E NEXT continue;

Bypassing and continuing I Loops

R e ferences http://www.computer-books.us/c_0008.php http://www.computer-books.us/c_0009 http://www.computer-books.us/c_2.php www.tutorialspoint.com/cprogramming/cprogramming_pdf. Programming in C by yashwant kanitkar ANSI C by E.balagurusamy- TMG publication Computer programming and Utilization by sanjay shah Mahajan Publication www. cprogramming .com/ books .html
Tags