Unit VI- Selection – Making Decisions, Repetition
Control statements in C language
if break continue goto switch Control statements Conditional Ternary Sequential Branching Conditional if switch Ternary Unconditional goto break continue Looping for while do -while
Decision Making and Branching
*conditional statement *goto statement contents *if statement *switch *conditional statement *goto statement
if-statement conditional branching statement Types of if statements * Simple-if * if- else *Nested if-else * if-else ladder
simple-if The general form:( with compound statement) if( condition) { } statement-x;
A block if statement may be modeled as: if (A>0) { A=A+1; Print A; B=A+2; } Yes A>0 A=A+1 Print A B=A+2
simple-if if( condition) statement-1; statement-2; statement-3; ×The general form:( without compound statement) if( condition) statement-1; statement-2; statement-3; statement-x;
If statements can be modeled as a flow chart. if (A>0) print A Print A Yes A>0 No
The following is a segment of program: x=1; y=1; if(n>0) x=x+1; y=y+1; printf("x=%d\ny=%d\n",x,y); What will be values of x and y if n assumes a value of 1. n=1 2.n=0 OUTPUT: n=1 x=2 y=2 n=0 x=1 y=2
The following is a segment of program: x=1; y=1; if(n>0) { x=x+1; y=y+1; } printf("x=%d\ny=%d\n",x,y); What will be values of x and y if n assumes a value of 1. n=1 2.n=0 OUTPUT: N=1 x=2 y=2 N=0 x=1 y=1
if-else The general form:- if(condition) { true block statements; } false block statements; statement-x;
Flowchart of if-else statement. Print A is greater Yes A>0 Print A is not greater No
C-Program using if-else Program to accept a character in any case and print in another case without using character test functions
# include <stdio.h> # include <conio.h> main( ) { char ch,c1; clrscr( ); printf("enter a char in anycase:"); ch=getchar(); if(ch>=65 && ch<=90) c1=ch+32; else if(ch>=97 && ch<=122) c1=ch-32; printf("the given char in another case IS:"); putchar(c1); getch(); }
Nesting of if-else if-else statements in nested form if(test condition-1) { if( test-condition-2) statement-1; } else statement-2; statement-3; statement-x;
program using nested if-else *write a c-program to find largest of three numbers using nested if-else statements.
if(a>b) { if(a>c) printf("%d",a); else printf("%d",c); } #include<stdio.h> #include<conio.h> main() { int a,b,c; printf("enter three numbers \n"); scanf("%d%d%d",&a,&b,&c); printf("\n largest value is: " ); if(a>b) { if(a>c) printf("%d",a); else printf("%d",c); } if(c>b) printf("%d",b); getch();
The else-if ladder if( condition-1) statement-1; else if( condition-2) The general form if( condition-1) statement-1; else if( condition-2) statement-2; else if( Condition-3) statement-3; else if( condition n) statement-n; else default-statement; Statement-x;
C-programs using else-if ladder Write a c-program to grade the students according to following rules using else-if ladder. avgmarks grade 80 to 100 Honours 60 to 69 First Division 50 to 59 Second Division 40 to 49 Third Division 0 to 39 Fail
Else-if Logic flow.
Lab program-1 1. Design, develop and execute a program in C to find and output all the roots of a given quadratic equation, for non-zero coefficients.
Algorithm to find roots of quadratic equation STEP 1: INPUT THE VALUES FOR a,b,c Input a,b,c STEP 2: DETERMINE DISCRIMINANT Discriminant = b*b – (4*a*c) STEP 3: FIND TWO EQUAL ROOTS If Discriminant = 0 then Roots X1=X2=-b/(2*a) Print the two equal roots
STEP 4: FIND TWO DISTINCT ROOTS If Discriminant > 0 then X1 = -b + sqrt(Discriminant) / (2*a) X2 = -b – sqrt(Discriminant) / (2*a) Print the two distinct roots
STEP 5: FIND COMPLEX ROOTS If Discriminant < 0 then Real part = - b / (2 *a) Imaginary Part = sqrt(abs(disc)) / (2*a) Print the imaginary roots in the form of Real part + i Imaginary part and Real part - i Imaginary part. STEP 6: STOP
#include<stdio. h> #include<conio. h> #include<math #include<stdio.h> #include<conio.h> #include<math.h> main() { float a,b,c,d,root1,root2,rpart,ipart; printf("Enter the three co-effients\n"); scanf("%f%f%f",&a,&b,&c); 6/18/2018 Dept of CSE,DSCE
d=(b. b-4. a. c); if(d==0) { printf("Roots are equal\n"); root1=-b/2 d=(b*b-4*a*c); if(d==0) { printf("Roots are equal\n"); root1=-b/2*a; root2=root1; printf("root1=%f\nroot2=%f\n",root1,root2); } 6/18/2018 Dept of CSE,DSCE
else if(d>0) { printf("Roots are real & distinct\n"); root1=(-b+sqrt(d))/(2*a); root2=(-b-sqrt(d))/(2*a); printf("root1=%f\nroot2=%f\n",root1,root2); } 6/18/2018 Dept of CSE,DSCE
else { printf("Roots are imaginary\n"); rpart=-b/(2 else { printf("Roots are imaginary\n"); rpart=-b/(2*a); ipart=sqrt(fabs(d))/(2*a); printf("root1=%f+i%f\n",rpart,ipart); printf("root2=%f-i%f\n",rpart,ipart); } getch(); 6/18/2018 Dept of CSE,DSCE
Multiway Selection In addition to two-way selection, most programming languages provide another selection concept known as multiway selection. Multiway selection chooses among several alternatives. C has two different ways to implement multiway selection: the switch statement else-if construct.
Switch is a composite statement used to make a decision between many alternatives. The format is: Switch Statement.
Program to demonstrate the use of Switch Statement.
Results
Switch With Break Statements Break statements is used to execute only one of the case label sequences. Break Statements causes the program to jump out of the switch statement and follow the code following after the switch statement.
Program Modified With Break Statements
Flow chart of Switch(With Break) Statement.
Results
Rules while using Switch Statements
example switch(choice) //valid if choice is integer variable. switch(i+1) //valid if i is integer variable switch(i+2.1) //invalid even if i is integer 2 or more case labels with same value is not allowed case 1:printf(“hello:”); break; case 1:printf(“lll”);break; //invalid case 10+2: //valid case i+2: //invalid Since i variable is present case4: //invalid since no space between case and 4. Case 1: //invalid case should be in lowercase case 1:2:3: //invalid there should be separate case for each.
Write a c program to design a simple calculator using switch
#include<stdio.h> #include<conio.h> #include<process.h> main() { float a,b,res; char op; printf(“Enter the operator\n”); op=getchar( ); printf("Enter two operands\n"); scanf("%f%f", &a, &b); switch(op) case '+': res = a + b; break;
case '-': res=a-b; break; case '*': res=a*b; case '/': if(b!=0) res=a/b; else { printf("Divide by zero error"); } default: printf("Illegal operator\n"); printf("%f%c%f=%f",a,op,b,res); getch();
goto statement Unconditional branching statement This statement is used to transfer the control from one point to another point anywhere in the program. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name
The general forms of goto goto label; label: statement-1; statement-1; statement-2; statement-2; label: statement-3; statement-x; goto label; Forward jump backward jump
Assuming x=5,y=0,z=0.what is the value of z after execution of the following code if(x===0 ||x&&y) if(!y) z=1; else z=2; z=3; (A) 0 (B)1 (c ) 2 ( D) 3
Problems on switch Assuming that x=2,y=1 and z=0 initially ,what will be their values after executing the following code segments? switch(y) { case 2: x=1; y=x+1; case 3: x=0; break; default: x=1;y=0; }
switch(y) { case 0: x=0; y=0; case 2: x=2;z=2; default: x=1;y=2; }
For the following program segment give an equivalent program segment using the switch statement if(opr==1) res=a+b; else if (opr==2) res=a-b; else if(opr==3) res=a*b; else res=a/b; Feb-March/2005 (6 marks)
Write a C-program to find roots of quadratic equation using switch.
#include<stdio. h> #include<conio. h> #include<math #include<stdio.h> #include<conio.h> #include<math.h> main() { int a,b,c,d,m; float root1,root2,ipart,rpart; printf("Enter three co-effients\n"); scanf("%d%d%d",&a,&b,&c);
d=(b*b-4*a*c); if(d==0) m=1; else if(d>0) m=2; else m=3;
switch(m) { case 1:printf("Roots are equal\n"); root1=-b/(2*a); printf("root1=root2=%f",root1); break; case 2:printf("Roots are real & distinct\n"); root1=(-b+sqrt(d))/(2*a); root2=(-b-sqrt(d))/(2*a); printf("root1=%f\nroot2=%f",root1,root2); break;
case 3:printf("Roots are imaginary\n"); rpart=-b/(2 case 3:printf("Roots are imaginary\n"); rpart=-b/(2*a); ipart=sqrt(fabs(d))/(2*a); printf("root1=%f+i%f\n",rpart,ipart); printf("root2=%f-i%f\n",rpart,ipart); break; default:printf("Invalid opeartion\n"); } getch();
C-Skills
Program to print "Hello, world" without using semicolon (;) in your program main() { if (printf ("Hello, world\n")) }
What is the ouput? int x; x=printf(“welcome\n”); printf(“%d”,x);
What is the ouput? int x=65; Printf(“%d\n”,printf(“%d”,x);
Which is better? X++ OR X=x+1
Solution x++ generates inc instruction x = x + 1 generates inc followed by move instruction
Find out errors? If any #include<stdio.h> #include<conio.h> int main( ) { int a=--2; printf(“ a=%d”,a); getch(); }
Find out output? int x=7,p=2; printf(“%d%d%d”,x++,x,--p); printf(“%d%d%d”,p,p++,++p);