statement- Computer Programming and Utilization (CPU) 110003 D) Decision Control Structure (if, if, if e if, switch, ) 1 Explain if with example and draw flowchart. if is used to control the flow of execution of statements. It is two way decision making statement. It evaluates expression first and based on its result, the control is transferred to the particular statement. The general form of simple if statement is if (testt expression) statement-block statement-x the test expression is true, the statement-block will be executed; otherwise the block will be skipped and the execution will jump to the statement-x. Flowchart: Entry Expression? Statement-block Next-statement Example: #include< <stdio.h> int no; printf("enterr the number:"); scanf("%d", &no); if(no%2==0) printf("no is even"); if(no%2!=0) printf("no is odd"); 1
2 Explain if with example and draw flowchart. supports statements only for true part supports statementss for true part and false part. The general format is given below. if(test-expression) -block statement. -block statement statement-x; test-expression is true, then true-block statement is executed otherwise false-block statement is executed. Every time, either true block or false block will be executed. In both the cases, statement-x will be executed immediately after that block. Flowchart: Entry Expression? block -block Next statement Example: Check whether the given number is even or odd. #include< <stdio.h> int no; printf("enterr the number:"); scanf("%d", &no); if(no%2==0) printf("the number is even" "); printf("the number is odd") ; 2
3 Explain if....if ladder with example and draw flowchart. When multipath decisions are involved, we may use if if ladder, which takes the following general form. if(condition-1) statement-1; if(condition-2) statement-2; if(condition-n) statement-n; default statement; statement-x First condition-1 is checked and if it is true, then statement-1 will be executed and control goes to statement-x. condition-1 control goes to statement-x. condition-2 is false, then condition-3 is checked and processs repeats is false, then condition-2 is checked and if it is true then statement-2 will be executed and This process is repeated until either it finds one of the conditions is true or all the conditions are false. If all the conditions are false, then default-statement will be executed. Else part is optional in if if ladder. Flowchart: Entry Condition 1 Statement-1 Condition 2 Statement-2 Condition 3 Statement-3 Conditionn N Statement-N Default statement 3
Example: Find the class of student based on average marks. #include< <stdio.h> int m1,m2,m3,,m4,m5; float avg; printf("enterr m1,m2,m3, m4,m5:"); scanf("%d%d%d%d%d", &m1, &m2, &m3, &m4, &m5); avg=(m1+m2+m3+m4+m5)/5; if(avg>=70) printf( Distinct ); if(avg>= =60) printf("first class"); if(avg>= =50) printf("second class"); if(avg>= =40) printf("pass class"); printf("fail"); 4 Explain nested if with example and draw flowchart. When a series of decisions are involved, we may have to use more than one if statement in nested form as shown below. if(test-condition-1) if(test-condition-2) Statement-1; e Statement-2; Statement-3; test-condition-1 is true then test-condition-2 is evaluated. If it is true then Statement-1 will be executed, if it is false then Statement-2 is false then Statement-3 will be executed. will be executed. test-condition-1 4
Flowchart: Entry Condition 1? Statement-3 Condition 2? Statement-1 Statement-2 Next statement Example: Find maximum number from given three numbers. #include< <stdio.h> int a,b,c; printf("enterr value of a, b, c:"); scanf("%d%d%d", &a, &b, &c); if(a>b) if(a>c) printf("a is max"); printf("c is max"); if(b>c) printf("b is max"); printf("c is max"); 5
5 Explain switch-case statement with example. The switch statement is a multi-way decision making. It tests whether an expression matches any one of the constant values or not. General form of switch-case is as below, switch(expression) case const-expr 1: statement 1; case const-expr 2: statement 2; case const-expr 3: statement 3; default: statements expression in switch should be integer or character expression. Float or any other data type is not allowed. Each case is labeled by one or more integer-valued constant. a case matches the expression value then execution starts at thatt case. Value of all case expressions must be different. none of the cases are matched then default case is executed. default case is optional. The break statement causess an immediate exit from the switch. Example: switch (grade) case 1: printf("fall (F)\n"); case 2: printf("bad (D)\ n"); case 3: printf("good (C)\n"); case 4: printf("very Good (B)\n"); case 5: printf("excellent (A)\n"); default: printf("you have inputted false grade\ \n"); // break isn t necessary here 6
Rules for switch statement The switch expression must be integral type. Float or other dataa types are not allowed. Case labels must be constant or constant expression. Case labels must be unique. Case labels must end with colons. The break statement is optional. The default case statement is optional. Nesting of switch statement is allowed. 6 State difference between if- and switch-case if- If statement is used to select among two alternatives. If can have values based on constraints. switch The switch statement is used to select among multiple alternatives. Switch can have values based on user choice. If implements Linear search. Switch implements Binary search. Float, double, char, int and other data types can be used in if condition. Only int and char data types can be used in switch block. Write a C Program to. 1 Find out whether the given number is odd or even. #include<stdio. h> int no; printf("enter no:") ; scanf("%d",&no); if(no%2== =0) printf("no is even"); printf("no is odd"); 2 Find maximum number from given 3 numbers. #include<stdio. h> int a,b,c; printf("enter a,b,c:"); scanf("%d%d%d",&a,& &b,&c); 7
if(a>b&&a>c) printf("a is if(b>c) printf("b is printf("c is maximum"); maximum"); maximum"); 3 which asks day number and prints corresponding day name using switch-case. #include<stdio. h> int day; printf("enter day number (1-7) \n"); scanf("%d",&day); switch(day) case 1: printf("sunday") ; case 2: printf("monday") ; case 3: printf("tuesday" ); case 4: printf("wednesday"); case 5: printf("thursday"); case 6: printf("friday") ; case 7: printf("saturday"); default: printf("wrong input"); 8