Download presentation
Presentation is loading. Please wait.
Published byCorey Lawson Modified over 6 years ago
1
Unit VI- Selection – Making Decisions, Repetition
2
Control statements in C language
3
if break continue goto switch Control statements Conditional Ternary
Sequential Branching Conditional if switch Ternary Unconditional goto break continue Looping for while do -while
4
Decision Making and Branching
5
*conditional statement *goto statement
contents *if statement *switch *conditional statement *goto statement
6
if-statement conditional branching statement Types of if statements
* Simple-if * if- else *Nested if-else * if-else ladder
7
simple-if The general form:( with compound statement) if( condition) {
} statement-x;
8
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
9
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;
10
If statements can be modeled as a flow chart.
if (A>0) print A Print A Yes A>0 No
11
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
12
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
13
if-else The general form:- if(condition) { true block statements; }
false block statements; statement-x;
14
Flowchart of if-else statement.
Print A is greater Yes A>0 Print A is not greater No
15
C-Program using if-else
Program to accept a character in any case and print in another case without using character test functions
16
# 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(); }
17
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;
18
program using nested if-else
*write a c-program to find largest of three numbers using nested if-else statements.
19
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();
20
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;
21
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 Honours 60 to First Division 50 to Second Division 40 to Third Division 0 to Fail
22
Else-if Logic flow.
23
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.
24
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
25
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
26
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
27
#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
28
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
29
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
30
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
31
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.
32
Switch is a composite statement used to make a decision between many alternatives. The format is:
Switch Statement.
33
Program to demonstrate the use of Switch Statement.
34
Results
35
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.
36
Program Modified With Break Statements
37
Flow chart of Switch(With Break) Statement.
38
Results
39
Rules while using Switch Statements
40
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.
41
Write a c program to design a simple calculator using switch
42
#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;
43
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();
44
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
45
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
46
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
47
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; }
48
switch(y) { case 0: x=0; y=0; case 2: x=2;z=2; default: x=1;y=2; }
49
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)
50
Write a C-program to find roots of quadratic equation using switch.
51
#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);
52
d=(b*b-4*a*c); if(d==0) m=1; else if(d>0) m=2; else m=3;
53
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;
54
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();
55
C-Skills
56
Program to print "Hello, world" without using semicolon (;) in your program
main() { if (printf ("Hello, world\n")) }
57
What is the ouput? int x; x=printf(“welcome\n”); printf(“%d”,x);
58
What is the ouput? int x=65; Printf(“%d\n”,printf(“%d”,x);
59
Which is better? X++ OR X=x+1
60
Solution x++ generates inc instruction
x = x + 1 generates inc followed by move instruction
61
Find out errors? If any #include<stdio.h> #include<conio.h> int main( ) { int a=--2; printf(“ a=%d”,a); getch(); }
62
Find out output? int x=7,p=2; printf(“%d%d%d”,x++,x,--p); printf(“%d%d%d”,p,p++,++p);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.