1 / 30

CHAPTER 3

CHAPTER 3. Control Statements. Session Objectives. Explain Types of Control Statements. Explain If, If..else statements. Understand Looping concepts. Explain Switch..Case statement. Explain Break,continue Statements. Explain the Goto Statement. Control Statements. UnConditional

danno
Download Presentation

CHAPTER 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 3 Control Statements MAHENDRAN

  2. Session Objectives • Explain Types of Control Statements • Explain If, If..else statements • Understand Looping concepts • Explain Switch..Case statement • Explain Break,continue Statements • Explain the Goto Statement

  3. Control Statements UnConditional Control Statements Conditional Control Statements • Jumping Statement • Decision making& Branching • Looping Statement

  4. Decision Making &Branching Statement Jumping Statement Looping Statement By default the statements are executed sequentially. However in practice we have situations where we may have to change the order of executions of statements until specified conditions are met. if if..else switch goto while do..while for

  5. If Statement TRUE Test Condition FALSE True Block Statements Executable Statements Syntax : if(condition) { True Block Statement(s); } Executable statement(s); FLOWCHART

  6. If - else Statement Syntax : if(condition) { True Block Statement(s); } else {False Block statement(s); } FLOWCHART FALSE TRUE Test Condition True Block Statements False Block Statements

  7. WHILE LOOP Start STOP Initialize FALSE Test Condition TRUE Body of the loop Increment or Decrement Syntax : Initialization counter; while(condition) { Body of the Loop; Increment/Decrement counter ; }

  8. Start Stop Initialize Body of the loop Increment or Decrement TRUE Test Condition FALSE DO.. WHILE LOOP Syntax : Initialization counter; do { Body of the Loop; Increment/Decrement counter ; } while(condition);

  9. Start STOP Initialize FALSE Test Condition TRUE Body of the loop Increment or Decrement FOR LOOP Syntax : For(initialization;condition;increment/decrement) { Body of the Loop; }

  10. Multiple Initialization / Increments • The following loop is used for multiple initialization -

  11. Nested for Loops • The for loop will be termed as a nested for loop when it is written like this : -

  12. Write a “C” Program swapping two numbers #include<stdio.h> void main() { int a,b,c; printf("enter two number="); scanf ("%d %d",&a,&b); b=a+b; a=b-a; b=b-a; printf("%d %d",a,b); } Decrement Operator Example #include<stdio.h> void main() { int i; i=2; printf("\n The value of i is %d",i); printf("\n The value of i is %d",--i); printf("\n The value of i is %d",i); } Write a program to convert farenheit into celsius = (far - 32) *5/9 #include<stdio.h> void main() { float faren,celsius; printf("\n Enter the farenheit = "); scanf(" %f", &faren); celsius = (faren -32) *5/9; printf("The Celsius is = %f",celsius); } Increment operator(++) Example #include<stdio.h> void main() { int i=2; printf("\n The value of i is %d",i); printf("\n The value of i is %d",++i); printf("\n The value of i is %d",i); }

  13. Write a program to accept your name,address & city and print it one by one #include<stdio.h> void main() { char name[20],add[30],city[20]; printf("\n Enter the name="); scanf("%s",&name); printf("\n Enter the Address="); scanf("%s",&add); printf("\n Enter the City="); scanf("%s",&city); printf("The Name is = %s\n",name); printf("The Address is = %s\n",add); printf("The City is = %s\n",city); } INPUT OUTPUT Enter the Name = CSCComputer Enter the Address= Meenambalsalai Enter the city = Chennai The Name is = CSCComputer The Address is= Meenambalsalai The city is = Chennai

  14. How to print a Special characters #include<stdio.h> #include<conio.h> void main() { char c; clrscr(); printf("\n This is how to print a double quote : \"\n"); printf("\n This is how to print a backslash : \\\n"); c='\''; printf("\n c now contains %c\n",c); printf("\n This is how to print a percent - sign : %%\n"); c='\007'; /* beep sound code is 7 */ printf("\n c now contains %d\n",c); getch(); }

  15. Write a program to find the greatest among two numbers a,b #include<stdio.h> void main() { int a,b,c; printf("\n Enter 2 nos. ="); scanf("%d %d",&a,&b); if (a>b) { printf("A is greatest %d“,a); } else { printf(" B is greatest %d“,b); } getch(); } Write a program to print employee name, basic pay,pf,lic & calculate the net salary #include<stdio.h> void main() { char name[20]; int basic,pf,lic,net; printf("\n Enter the name="); scanf("%s",name); printf("\n Enter Basic Bpay ="); scanf("%d",&basic); printf("\n Enter PF ="); scanf("%d",&pf); printf("\n Enter LIC ="); scanf("%d",&lic); net = basic – (pf + lic); printf("Net Salary is = %d ",net); } OUTPUT Enter 2 nos. = 100 20 A is greatest 100

  16. Write a program to find the greatest of Three numbers #include<stdio.h> void main() { int a,b,c; printf("\n Enter 3 nos. ="); scanf("%d %d %d",&a,&b,&c); if ((a>b) && (a>c)) { printf("A is greatest %d“,a); } elseif (b>c) { printf(" B is greatest %d“,b); } else { printf("C is greatest %d“.c); } getch(); } Write a program to check whether the given number is positive or negative. #include<stdio.h> void main() { int a; printf("Enter the number="); scanf("%d",&a); if (a>0) { printf( "Given number is positive %d“.a); } Elseif(a<0) { printf("Given number is negative %d“,a); } Else { printf("Given number is Zero %d“,a); } } OUTPUT Enter 3 nos. = 10 20 5 B is greatest OUTPUT Enter the number = 10 The Given Number is positive

  17. Write a progrm input the given Number id Odd or Even Number #include<stdio.h> void main() { int no; printf("Enter the Number =\n"); scanf("%d",&no); if ((no%2)==0) { printf("\n The Number is Even Number %d“,no); } else { printf("\n The Number is Odd Number %d“,no); } getch(); } Write a progrm input the given year is LEAP YEAR or Not #include<stdio.h> void main() { int year; printf("Enter the year =\n"); scanf("%d",&year); if ((year%4)==0) { printf("\n It is a leap year"); } else { printf("\n It is not a leap year"); } getch(); } OUTPUT OUTPUT Enter the number = 12 The Number is Even Number 12 Enter the Year 2000 It is a leap year

  18. PASCAL TRIANGLE #include<stdio.h> void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\n",i); } printf("\n"); } Write a program to print the FIBONACCI SERIES upto N Numbers #include<stdio.h> void main() { int n,i,f1,f2,f3; printf("enter the number = "); scanf("%d",&n); f1=-1; f2=1; for(i=0;i<=n;i++) { f3=f1+f2; printf("%d\n",f3); f1=f2; f2=f3; } } Output 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 OUTPUT Enter the number = 5 0 1 1 2 3 5

  19. PASCAL TRIANGLE #include<stdio.h> void main() { int i,j,c=1; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\n",c); c++; } printf("\n"); } PASCAL TRIANGLE #include<stdio.h> void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\n",j); } printf("\n"); } Output 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  20. PASCAL TRIANGLE to print * #include<stdio.h> void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(“*"); } printf("\n"); } PASCAL TRIANGLE #include<stdio.h> void main() { int i,j; clrscr(); for(i=0;i<5;i++) { for(j=i;j>0;j--) { printf("%d\n",j); } printf("\n"); } Output * * * * * * * * * * * * * * * Output 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1

  21. continue Statement • The continue statement causes the next iteration • of the enclosing loop to begin. • When this statement is encountered, the remaining statements in the body of the loop are skipped and • the control is passed on to the re-initialization step. Syntax : continue;

  22. function exit() • The exit() is used to break out of the program. • The use of this function causes immediate termination of the program and control rests in the hands of the Operating System.

  23. statement break • The break statement is used to terminate a case in a switch statement. • When the break statement is encountered in a loop, the loop is terminated immediately and control is passed to the statement following the loop. Syntax : break;

  24. label goto • The goto statement transfers control to any other statement within the same function in a C program. • They reduce program reliability and make program difficult to maintain. goto Labelname: Statements; -- Labelname: --- Statements; Syntax :

  25. Syntax : switch(Expression) { case 1: Statements; break; case 2: Statements; break; case 3: Statements; break; default :Statements; break; } Switch..Case Statements Note • Default is Optional • It is useful while writing menu driven programs • Break statement transfers the control to the end of switch..case statement.

  26. Write a menu type program to solve arithmetic calculation using switch case stmts. #include<stdio.h> void main() { int a,b,choice,tot; printf("\n 1. Addition"); printf("\n 2. Subtraction"); printf("\n 3. Multiplication"); printf("\n 4. Division"); printf("\n Enter the Values="); scanf("%d %d",&a,&b); printf("enter your choice="); scanf("%d",&choice); switch(choice) { case 1: tot = a+b; printf("\n The Addition of 2 nos. % d",tot); break; case 2: tot = a-b; printf("\n The Subtraction of 2 nos. % d",tot); break; case 3: tot = a*b; printf("\n The Multiplication of 2 nos. % d",tot); break; case 4: tot = a/b; printf("\n The Division = % d",tot); break; default: printf("\n invalid"); break; } }

  27. Shortcut Keys F2 Key  Save File F3  Open an existing File F5  Maximize F6  Move to next Program Alt+F9  To compile (Check Errors) Ctrl+F9  Compile and Linking (Execute a Program) Alt+F5  Display output mode Alt+F3  Close screen Quit  Alt+X (come out from Turbo “C”)

  28. Session Summary • The if statement is used to make decisions • The switch statement allows us to make a decision from a number of choices. • The break & continue statements used inside a for, while and do..while loops • The loop does does not terminate when a continue statement is encountered • The switch statement can only for test equality • Break used to terminate or exit from a switch statement • The goto statement is used to transfer the control in a loop or function from one point to any other portion in that program where it encounters a label

  29. EXERCISES Write a program to find the perfect square using if..else statement? Write a program to input the given number is prime or Not? Write a program to perform arithmetic operations using switch statement? Write a program to find the mean and standard deviation of N Numbers? Write a program to find the number of five hundreds,Hundreds, Fifties,Twenties,Tens,Fives,two’s and one’s in a amount given using while loop? Write a program to convert Octal number to a Decimal Number? Write a program to generate N even Numbers and calculate its sum? Write a program to count the number of digits in an integers using while loop? Write a program to calculate the sine series?

  30. EXERCISES 10. Write a program to print the following Outputs using for while and do..while loops?

More Related