1 / 20

Control Structures

Control Structures. Contents. Decision making statements if statement if...else statement Nested if...else statement (if... elseif ....else Statement) Selection statements Switch-Case Statement Iteration statements for Loop While loop Do-while Loop Jump statements Break Statement

garret
Download Presentation

Control Structures

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. Control Structures

  2. Contents • Decision making statements • if statement • if...else statement • Nested if...else statement (if...elseif....else Statement) • Selection statements • Switch-Case Statement • Iteration statements • for Loop • While loop • Do-while Loop • Jump statements • Break Statement • Continue Statement

  3. Control Structures Control flowrefers to the order in which the individual statements ,  instructions or function calls of an imperative or declarative program are executed or evaluated. There are four types of control statements in C: • Decision making statements • Selection statements • Iteration statements • Jump statements • Decision making statements : The if-else statement , nested if and if-else statements is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test . • if statement syntaxif (test expression) { statement/s to be executed if test expression is true; } Back

  4. If the test expression is true then, statements for the body if, i.e, statements inside parenthesis are executed. But, if the test expression is false, the execution of the statements for the body of if statements are skipped. • Write a C program to print the number entered by user only if the number entered is negative. #include <stdio.h> int main() { int num; printf("Enter a number to check.\n"); scanf("%d",&num); if(num<0) /* checking whether number is less than 0 or not. */ printf("Number=%d\n",num); printf("The if statement in C programming is easy."); return 0; } Enter a number to check. -2 Number=-2 The if statement in C programming is easy. Back

  5. if...else statement : The if...else statement is used, if the programmer wants to execute some code, if the test expression is true and execute some other code if the test expression is false. • Syntax of if...else if (test expression) statements to be executed if test expression is true; else statements to be executed if test expression is false;

  6. Write a C program to check whether a number entered by user is even or odd #include <stdio.h> int main() { int num; printf("Enter a number you want to check.\n"); scanf("%d",&num); if((num%2)==0) printf("%d is even.",num); else printf("%d is odd.",num); return 0; } Back

  7. Enter a number you want to check. 25 25 is odd. • Nested if...else statement (if...elseif....else Statement) The if...else statement can be used in nested form when a serious decision are involved. • Syntax of nested if...else statement if (test expression) statements to be executed if test expression is true; else if(test expression 1) statements to be executed if test expressions 1 is true; else if (test expression 2) . . . else statements to be executed if all test expressions are false; Back

  8. Selection Statement: Switch-Case Statement : A switch statement is used for multiple way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type. • Syntax: switch (expression) { case value1: code segment1; break; case value2: code segment2; break; . . . case valueN: code segmentN; Back

  9. break; default: default code segment; } The value of this expression is either generated during program execution or read in as user input. The case whose value is the same as that of the expression will be executed. Back

  10. Iteration Statements : Iteration statements are used to execute a particular set of instructions repeatedly until a particular condition is met or for a fixed number of iterations. There are 3 types of loops in C programming: • for loop • while loop • do...while loop • for Loop Syntax: The for statement or the for loop repeatedly executes the set of instructions that comprise the body of the for loop until a particular condition is satisfied. Back

  11. Syntax: for(initialization; termination; increment/decrement) { //statements to be executed } The for loop consists of three expressions: • The initialization expression, which initializes the looping index. The initialization expression is executed only once, when the loop begins. • The termination expression, which represents a condition that must be true for the loop to continue execution. • The increment/decrement expression is executed after every iteration to update the value of the looping index.

  12. Write a program to find the sum of first n natural numbers where n is entered by user. Note: 1,2,3... are called natural numbers #include <stdio.h> int main() { int n, count, sum=0; printf("Enter the value of n.\n"); scanf("%d",&n); for(count=1;count<=n;++count) { sum+=count; } printf("Sum=%d",sum); return 0; } Back

  13. While Statement : The while statement executes a block of statements repeatedly while a particular condition is true. • Syntax of while loop: while (test expression) { statements to be executed. } • Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n = 1*2*3*...*n #include <stdio.h> int main() { int number, factorial; printf("Enter a number.\n");

  14. scanf("%d",&number); factorial=1; while (number>0) { factorial=factorial*number; --number; } printf("Factorial=%d",factorial); return 0; } Back

  15. Do-while Loop : The do-while statement evaluates the condition at the end of the loop after executing the block of statements at least once. If the condition is true the loop continues, else it terminates after the first iteration. • Syntax: do { some code/s; } while (test expression); • Write a C program to add all the numbers entered by a user until user enters 0. #include <stdio.h> int main() { int sum=0,num; do

  16. { printf("Enter a number\n"); scanf("%d",&num); sum+=num; } while(num!=0); printf("sum=%d",sum); return 0; } Back

  17. Jump statements : There are two statement built in C, break; and continue; to interrupt the normal flow of control of a program. • Break Statement : • Syntax of break statement Break; • Example #include <stdio.h> int main () {int a = 10; while( a < 20 ) { printf("value of a: %d\n", a); a++; if( a > 15) { break; } } return 0; } Back

  18. Continue Statement : It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used. • Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it. # include <stdio.h> int main() { inti,num,product; for(i=1,product=1;i<=4;++i) { printf("Enter num%d:",i); scanf("%d",&num);

  19. if(num==0) continue; product*=num; } printf("product=%d",product); return 0; } Back

  20. Thank You

More Related