Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures Introduction

Similar presentations


Presentation on theme: "Control Structures Introduction"— Presentation transcript:

1 Control Structures Introduction
The statements which ‘control’ the flow of the execution, are known as control statements. There are 2 types of control statements Decision making / Selection statements Loop / Iterative statements

2 Decision making / Selection statements
The decision making statements are if statement if –else statement Nested if statement if –else ladder switch statements

3 2. The if….else Statement :
The if…else statement is an extension of the if statement. It is generally used whenever you want to execute the condition’s true and false part.

4 2. The if….else Statement :
Syntax : if(expression) { true- block statements; } else false – block statements;

5 true false expression true block statement false block statement

6 printf(“a is larger”); else printf(“b is larger”);
e.g. if(a>b) printf(“a is larger”); else printf(“b is larger”); true a>b false b is larger a is larger

7 3. Nested if…else statements:
The if clause and the else part may contain a compound statement. Either or both may contain another if or if….else statement. This is called the nesting of if…else statements.

8 Syntax: if(condition 1)
{ statement – 1; } else statement – 2; statement – 3;

9 E.g. if(a>b) { if(a>c) printf(“a is largest”); else printf(“c is largest”); } { if(b>c) printf(“b is largest”);

10 4. The else – if ladder: If there is an if else statement nested in each else of an if-else construct, it is known as the else..if ladder. Syntax: if (condition 1) statement – 1; else if (condition 2) statement – 2; else if (condition 3) statement – 3; else statement-4;

11 4. The else – if ladder: The conditions are evaluated from the top to downwards. If none of the conditions are true, the final else will be executed.

12 5. The Switch Statement : When one of the many alternative is to be selected, we can use an if statement to control the selection. For this reason ‘C’ has a built in multiway decision statement known as switch. The switch statement tests the value of a given variable against a list of case value and when a match is found, a block of statement associated with that case is executed. Dalcomp Technologies (India) Pvt. Ltd.

13 default: default-block }
5. The Switch Statement : Syntax: switch(expression) { case value-1: block-1 break; case value-2: block-2 ……….. default: default-block } Dalcomp Technologies (India) Pvt. Ltd.

14 The expression is an integer or character expression
The expression is an integer or character expression. Value1, value2 are constants and are known as case label. There is no need to put braces around these blocks. break statement exit the control from the switch case and transfer the control after the switch case statement. The default is an optional case, when present, it will be executed if the value of the expression does not match with any of the case values.

15 Rules for switch statement:
Case label must be integer or character. Case label must be unique. No two case labels can have the same value. Case label must end with colon. The break statement is optional. The default label is optional. There can be at most one default label. The default may be placed any where but usually placed at the end It is permitted to nest switch statement.

16 Write a program to enter a no between 0 to 9 and display it in words using switch
#include<stdio.h> #include<conio.h> void main() { int n; printf(“\n Enter no”); scanf(“%d”,&n); switch(n) case 0 : printf(“ZERO”); break; case 1 : printf(“ONE”); case 2 : printf(“TWO”); . case 9 : printf(“NINE”); default : printf(”\nInvalid i/p”); } getch(); }//main Output Enter no 5 FIVE

17 Write a menu driven program for add, sub, multi, div of 2 nos
Write a menu driven program for add, sub, multi, div of 2 nos. using switch #include<stdio.h> #include<conio.h> void main() { int a,b,c,opt; printf(“\n Enter 2 nos”); scanf(“%d%d”,&a,&b); printf(“\n1.Add\n2.Sub \n3.Multi\n4.Div”); printf(“\n Enter your option”); scanf(“%d”,&opt)

18 switch(opt) { case 1 :c=a+b; printf(“\nAdd =%d”,c); break; case 2 : c=a-b; printf(“\nSub=%d”,c); case 3 : c=a*b; printf(“\nMult=%d”,c); case 4 : c=a/b; printf(“\nDiv=%d”,c); default : printf(”\nInvalid i/p”); } getch();} //main Output Enter 2 nos 5 10 1.Add 2.Sub 3.Mult 4.Div Enter your option 3 Mult = 50

19 Example – 1 : Accept a year from the user
Example – 1 : Accept a year from the user. Find out if the year entered is leap year or not. #include<stdio.h> #include<conio.h>  void main() { int y; printf("Enter the year : "); scanf("%d",&y);   if(y%4= =0) printf("%d is a leap year \n“,y); else printf("%d is not a leap year \n“,y); getch();  } Output: Enter the year : 1992 1992 is a leap year

20 Example – 2 : Accept the selling price and cost price from the user
Example – 2 : Accept the selling price and cost price from the user. Find out if the seller has made profit or loss. Also calculate the loss incurred or profit made. #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the cost price and selling price "); scanf("%d%d",&a,&b);   if(b>a) printf("Seller has made profit which is : %d“,b-a); else if(a > b) printf("Seller has incurred loss which is %d“,a-b); else printf("Seller has neither made profit nor loss\n"); getch(); } Output : Enter the cost and selling price : 54 75  Seller has made profit which is : 21

21 Example – 3 : Write a program to accept 3 numbers and find the largest of the 3 numbers
#include<stdio.h> #include<conio.h>  void main() { int a,b,c; printf("Enter 3 numbers : "); scanf("%d%d%d",&a,&b,&c);   if(a>=b && a>=c) printf("%d is largest \n",a); else if(b>=c && b>=a) printf("%d is largest \n",b); else printf("%d is largest \n",c); getch(); }//main Output: Enter 3 numbers : 57 is largest

22 Example – 4 : Write a program to check if character entered is an alphabet, a number or a special character. #include<stdio.h> #include<conio.h>  void main() { char n; clrscr(); printf("Enter a character : "); scanf("%c",&n);   if(n>=65 && n<=90 || n>=97 && n<=122) printf("%c is an alphabet\n",n); else if(n>=48 && n<=57) printf("%c is a number \n",n); else printf("%c is a special symbol“,n); getch();} Output: Enter a character : H H is an alphabet

23 Example – 5 : A company insures its drivers in the following cases If the driver is married, If the driver is unmarried, male & above 30 years of age, If the driver is unmarried, female & above 25 years of age. In all other cases the driver is not insured. If the marital status, gender & age of driver is input write a program to determine if the driver is insured or not. void main() { int age; char ms, gender; clrscr(); printf("Marital status : (M/U)\nGender : (M/F)\n\n"); printf("Enter the marital status , age & Gender of driver: "); scanf("%c %d %c",&ms,&age,&gender);   if(ms == 'M' || (ms == 'U' && age>30 && gender =='M') || (ms == 'U' && age>25 && gender ==‘F')) printf("The driver is insured\n"); else printf("The driver is not insured"); getch(); } Enter the marital status , age & gender of driver: M 35 F The driver is insured

24 Loop If we want to perform the same series of actions in the same way ,more than once it will be done by using “Loop”. Loop It is ability to perform a set of instructions repeatedly.

25 Two types of Loops Top –Tested (Entry controlled loop ) Bottom-Tested (Exit controlled loop) Entry controlled loop : In this looping the condition is evaluated before the loop body is executed. Exit controlled loop: In this looping the condition is evaluated after the loop body is executed. The c language provides three statements for performing loop operation. The while statement The do statement The for statement.

26 While Loop It is often used when the number of times the loop is to be executed is not known in advance but depends on the test condition. The while is an entry controlled loop statement. Syntax : while(test condition) { body of the loop; }

27 While Loop The condition is evaluated and the statement (loop body )is executed as long as the expression is TRUE Since it is an entry controlled loop, if the expression evaluate to false the first time itself, the loop body will not be executed even once.

28 While Loop e.g. int count = 1; while (count <= 5) {
printf(“\n%d”,count); count++; } Points to remember The loop control variable must be initialized. The loop body must contain a statement to alter the value of the control variable. Output 1 ,2,3,4,5 Dalcomp Technologies (India) Pvt. Ltd.

29 Nested While Loop Similar to nested if statements, loops can be nested as well. That is, the body of a loop can contain another loop. For each iteration of the outer loop, the inner loop iterates completely. Dalcomp Technologies (India) Pvt. Ltd.

30 Nested While Loop Syntax : while(exp1) --------->Outer Loop {
while(exp2) >Inner Loop loop body of while (exp2); } Dalcomp Technologies (India) Pvt. Ltd.

31 Nested While Loop How many times will the string "Here" be printed?
count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) printf("Here"); count2++; } count1++; OUTPUT “Here” will be printed 10 * 20 = 200 times Dalcomp Technologies (India) Pvt. Ltd.

32 The do-while Statement
It is a bottom tested loop i.e. it evaluates the condition after the execution of loop body. This means that the statements within the loop are executed at least once. Syntax: do { loop body; }while(condition); Dalcomp Technologies (India) Pvt. Ltd.

33 Comparing while and do while loop
It is a top tested loop Condition is evaluated before the executing loop body. If the condition is false very first time, the loop body will never executed. Do while Loop It is a bottom tested loop Condition is evaluated after the execution of loop body. The loop body is executed at least once. Dalcomp Technologies (India) Pvt. Ltd.

34 The for loop It is very flexible, powerful and most commonly used loop in C. It is useful when the number of iterations are known in advance. A for statement has the following syntax: Syntax : for( initialization; test-condition ; increment or decrement) { Body of the loop; } Dalcomp Technologies (India) Pvt. Ltd.

35 The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while(condition) { statement; increment/decrement; } Dalcomp Technologies (India) Pvt. Ltd.

36 The for Statement The increment section can perform any calculation
int num; for (num=30; num > 0; num -= 5) { printf(“%d”, num); } Output : Dalcomp Technologies (India) Pvt. Ltd.

37 Nested For Loop Nested loops consist of an outer loop with one or more inner loops. e.g., for (i=1;i<=100;i++) { for(j=1;j<=50;j++) } The above loops will be executed for 100*50 times. Outer loop loop Inner loop

38 /* Program to display the output on the screen */
#include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the value of n :"); scanf("%d",&n); for(i=0;i<n;i++) > OUTER LOOP for(j=0;j<=i;j++) > INNER LOOP printf("*"); printf("\n"); } getch(); OUTPUT OUPUT Enter the value of n : 4 * ** *** ****

39 /* Program to Display 0 to 9 */
#include<stdio.h> #include<conio.h> void main() { int x; for(x=0; x<=9 ; x++) printf(“%d”,x); } printf(“\n”); getch(); OUTPUT

40 Jumps in Loops : There are four statements which are used to perform jumps in loops – break, continue, goto, exit(). break 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. Syntax : break;

41 Break in do while loop while (……..) do { { ……………….. ………………..
Break in while loop Break in do while loop while (……..) { ……………….. if (condition) break; ……………… } do { ……………….. if (condition) break; ……………… } while(….) ; Exit loop Exit loop

42 Break in nested for loop
Break in for loop for (……..) { ……………….. if (error) break; ……………… } for (……..) { for (……….) { if (condition) break; } Exit loop Exit loop

43 Control goes to outside.
e.g. count=1; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) printf(“\nEnter number”); scanf(“%d”,&n); if(n<0) break; }//inner for count++; }//outer for Output Enter number -3 No output Control goes to outside.

44 continue statement : The use of the continue statement is to skip the statements which are followed by ‘continue’ and continue with the next iteration of the loop. Syntax : continue;

45 continue in do while loop
continue in while loop continue in do while loop while (……..) { ……………….. if (condition) continue; ……………… } do { ……………….. if (condition) continue; ……………… } while(….) ;

46 Continue in nested for loop
Continue in for loop Continue in nested for loop for (……..) { ……………….. if (error) continue; ……………… } for (……..) { for (……….) if (condition) continue; }

47 e.g. do { printf(“Enter a number”); scanf(“%d”,&n); if(n<0) continue; sum=sum+n; }while(n!=999); This code accepts integers and calculates sum of only positive numbers. The loop terminates when user enters 999.

48 #include<stdio.h>
#include<conio.h> #include<math.h> void main() { int count,negative; float no,sqroot; printf("Enter 9999 To Stop\n"); count=0; negative=0; while(count<=20) { printf("\nEnter a number :"); scanf("%f",&no); if(no==9999) break; //break statement

49 if( no < 0 ) { printf("\nNumber is negative"); negative++; continue; // continue statement } sqroot = sqrt(no); printf("Numer = %f\n Square root = %f\n\n",no,sqroot); count++; }//while printf("\nPositive Numbers = %d\n",count); printf("\nNegative Numbers = %d\n",negative);

50 goto Statement : C support go to statement to jump unconditionally from one point to another in the program. Syntax: goto label; label: …… Statement; …….. ………. label: ………. Statement; goto label; [Forword Jump] [Backward Jump]

51 goto Statement : Backward Jump. If a label is before the statement goto, a loop will be formed and some statements will be executed repeatedly, such a jump is known as Backward Jump. Forward Jump: If the label is placed after the goto label, some statements will be skipped and the jump is known as a forward jump. e.g. x=1; loop: x++; if(x<100) goto loop;

52 Using exit() function The exit() function causes immediate termination of the entire program. e.g. void main() { int code; printf(“\n enter the security code”); scanf(“%d”,&code); if(code<0) exit(0); }

53 Example – 1 : Program to find factorial of given no:: void main() {
int i,n; long fact; clrscr(); printf(“\n Enter no”); scanf(“%d”,&n); fact=1; for(i=1;i<=n;i++) fact=fact*i; printf("\n%d!=%d\n",n,fact); getch(); } Let n=5 OUTPUT 5!=120

54 #include<stdio.h>
Example – 2 : Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of other.  #include<stdio.h> #include<conio.h>  void main() { int a,b,ans,i; clrscr(); printf("Enter the 2 numbers : "); scanf("%d%d",&a,&b);  ans=1; for(i=0;i<b;i++) ans=ans*a;   printf("\n%d^%d is %d\n",a,b,ans); getch(); }//main Output: Enter the 2 numbers :2 3 2^3 is 8

55 Example – 3 : Write a program to read a no
Example – 3 : Write a program to read a no. and check if it is palindrome or not. #include<stdio.h> #include<conio.h>  void main() { int n,temp,r; int rev=0; printf("Enter a number : "); scanf("%d",&n);  temp=n; while(n != 0) { r= n%10; rev = rev*10 + r; n/=10; }

56 if(rev = = temp) printf("%d is Palindrome \n",temp); else
printf(“%d is not Palindrome\n”,temp); getch(); } Output: Enter a number : 121 121 is Palindrome

57 #include<stdio.h>
Example – 5 : Accept a number through the keyboard. Display whether the given number is prime or not.  #include<stdio.h> #include<conio.h>  void main() { int i,num,r; clrscr();  printf(" Enter the number : "); scanf("%d",&num); for(i=2;i<=num /2;i++) { r = num % i; if(r = = 0) { printf(" %d is not a prime no",num); getch(); exit(0); }

58 printf(" %d is a prime number ",num); getch(); } Output:
Output: Enter the number : 121 121 is not a prime no


Download ppt "Control Structures Introduction"

Similar presentations


Ads by Google