Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Decision Making, Branching And Loop

Loading...

Published in: C / C++
15,746 Views

Decision Making, Branching and Loop.

Akhilesh K / Lucknow

4 years of teaching experience

Qualification: M.Sc (NIT Rourkela - 2019)

Teaches: All Subjects, English, Mathematics, Science, Chemistry, Physics, Algebra, IIT JEE Mains, AIPMT, NEET

Contact this Tutor
  1. D as10N MAKING BRANCHING
  2. INTRODUCTION o Instruction of a programs are executed either Sequential manner fir; J Branching C language supports the following decision making O statements. if statement switch statement conditional operator goto statement
  3. if statement It is used to control flow of execution of statement. It is two-way decision statement and is used in conjuction Wit expression Syntax- if (condition) statement 1 ; Entry Test expression True False Ex: if (age is more than 55) Person is retired
  4. DIFFERENT FORMS OF IF STATEM NT simple if if-else nested if-else else if ladder
  5. Simple if statement Syntax- if ( Condition) statement block; statement x ; Ex: if ( category sports) entry Test expression? false Statement x true Statement block printf ( marks=marks+bonus; "%f' marks);
  6. Program of simple if statement main() int a,b,c,d; float ratio; printf("\n enter four integer value"); scanf("%d %d %d",&a,&b,&c); if(c-d ratio= (float) (a+b)/(float)(c-d); printf("Ratio= 0/0f' ratio); Output- enter four integer values 12 23 Ratio= 34 45 -3.181818
  7. The if... else Syntax- if( Condition) true true block statement; else True block statement false block statement; statement x; Ex-: if (code== 1) boy=b0Y+ 1 ; if ( code== 2) girl=girl+l ; entry Condition fals fir; [ False block statement Statement x if (code==l) boy=b0Y+1 ; else girl=girl+l ;
  8. Program for If . main() int a; printf("Enter an integer"); scanf("%d",&a); printf(" %d is even number",a); else printf("%d is an odd number",a); output- Enter an integer 46 46 is an even number . else
  9. NESTING Syntax- if( test condition IF...ELSE STATEMENT Entry 9 l) if (test condition 2) statement 1 ; else statement 2; statement 3; 1 statement x False Materrent-3 test condition 1 statement-2 statement-x Next statement False True test condition 2 True statement-I
  10. PROGRAM FOR NESTED IF...ELSE STATEMENT main( ) float A. B. C; printfCEnter three values\n"); scanfCO/0f 0/0f 0/0f" &C); nLargest value is 0): if (A>C) if (C>B) Output Enter three values 23445 67379 88843 Laraest value is 88843 000000
  11. The else if ladder syntax-: if ( condition 1) statement 1 , else if ( condition 2) fir; J statement 2 ; else if ( condition 3) statement 3 ; else if( condition n) statement n ; else default statement ; statement x;
  12. N/A
  13. Program for else... if ladder man( ) int units. custnum; float charges; printf("Enter CUSTOMER NO. and UNITS consumed\n"); scanf("%d 0/0d", &custnum, &units); if (units
  14. THE SWITCH STATEMENT The complexity of a program increases by increasing no. of if statement. ' To overcome this C has a built in multiway decision statement known as switch.
  15. SYNTAX switch (expression) case value-I : block 1 ; break; case value-2: block2; break; default: —.caselabeJs (257 case labels) fir; J default block; break; statement x;
  16. FLOWCHART FOR SWITCH STATEMENT Entry switch expression expression value-I expression = value-2 (no match) default block-I block-2 default block statement-x
  17. PROGRAM FOR SWITCH STATEMENT main() int grade,mark,index; printf("Enter ur marks \n"), scanf("%d" &mark); index=mar(
  18. THE GOTO STATEMENT The goto statement is used for branching unconditionally. The goto statement breaks normal sequential execution of the progra The goto requires label to where it will transfer a control. Label is a valid variable name followed by a colon( : ). goto label: label: statement; FORWARD JUMP fir; J label: statement; goto label;; BACKWARD JUMP
  19. PROGRAM TO CALCULATE SUM OF SQUARES OF ALL INTEGERS main() int sum=O,n=1; loop: sum=sum+n*n; if( n==10) goto print; else n=n+l; goto loop; print: printf("\n Sum=%d" ,sum); fir; J
  20. LOOPING
  21. LOOPS A loop allows a program to repeat a group of statements, either any number fir; J of times or until some loop condition occurs Convenient if the exact number of repetitions are known Loop Consists of Body of the loop Control Statement
  22. False Test Condition True Body of Loop Entry Controlled/Pre-Test Loop Body of Loop fir; J False Test Condition True Exit Controlled/Post-Test Loop
  23. STEPS IN LOOPING fir; J Initialization of condition variable Execution of body of the loop Test the control statement Updating the condition variable
  24. for COMMON LOOPS while do while
  25. Repetition occurs as long as a condition is true false true Body fir; J
  26. FOR Syntax: for (exprl , expr2; expr3) statement exprl controls the looping action, expr2 represents a condition that ensures loop continuation, expr3 modifies the value of the control variable initially assigned by exprl
  27. keyword control variable i for (i=l; i n; i initial value of control variable loop continuation condition final value of control variab for which the condition is true increment of control variable
  28. EXAMPLES • Vary the control variable from 1 to 100 in increments of" for (i = 1; i 100; i++) • Vary the control variable from 100 to 1 in increments of -1 for (l • = 100; i 1; • Vary the control variable from 5 to 55 in increments of 5 for (i = 5; i 55; i+=5)
  29. EXAMPLES 2 #include void main() /* a program to produce a Celsius to Fahrenheit conversion chart for the numbers 1 to 100 */ int celsius; for (celsius = 0; celsius 100; celsius++) printf("Celsius: %d Fahrenheit: %d\n", celsius, (celsius * 9) / 5 + 32);
  30. NESTED FOR LOOPS Nested means there is a loop within a loop Executed from the inside out Each loop is like a layer and has its own counter variable, its own loop expression and its own loop body In a nested loop, for each value of the outermost counter variable, the complete inner loop will be executed once fir; J
  31. General form fior (loopl exprs) { loop body_la for (loop2 exprs) { loop body 2 loop body _ 1b
  32. WHILE expression 1; while (expression 2) statements; expression 3; fir; J initialization condition checking updation of condition
  33. fir; J The statement is executed repeatedly as long as the expression is true (non zero) The cycle continues until expression becomes zero, at which point execution resumes after statement
  34. • If the test expression in a while loop is false initially, the while loop Will never be executed inti=l, sum = 0; while (i 10) sum = sum + i; printf("Sum = %d\n", sum);
  35. FOR AND WHILE for(exprl ; expr2; expr3) statement' exprl ; while(expr2) statement; expr3;
  36. BREAK AND CONTINUE fir; J These interrupt normal flow of control break causes an exit from the innermost enclosing loop continue causes the current iteration of a loop to stop and the next iteration to begin immediately
  37. while (expression) statements; if(condition) break; more statements while (expression) statements continue; more statemen s fir; J
  38. DO-WHILE When a loop is constructed using while, the test for continuation is carried out at the beginning of each pass With do-while the test for continuation takes place at the end of each pass do statement } while (expression);
  39. EXAMPLE int i = 1, sum fir; J do sum = sum + i; printf("Sum = %d\n", sum);
  40. WHILE VS. DO-WHILE fir; J • • while -- the expression is tested first, if the result is false, the loop body is never executed do-while -- the loop body is always executed once. After that, the expression is tested, if the result is false, the loop body is not executed again