Unit VI- Selection – Making Decisions, Repetition

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
true (any other value but zero) false (zero) expression Statement 2
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Week 4 Program Control Structure
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Construct 1 ) We divide the basic programming construct into three types: Sequential Structure 、 Selection Structure and Loop Structure.
CPS120: Introduction to Computer Science Decision Making in Programs.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Statements: Part1  if, if…else, switch 1.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Program design Program Design Process has 2 phases:
Control Structures Introduction
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
Course Outcomes of Programming In C (PIC) (17212, C203):
The if…else Selection Statement
REPETITION CONTROL STRUCTURE
Control Structures and Data Files
Chapter 4: Control Structures I
Decisions Chapter 4.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
C Program Controls + Flow Structure
Selection—Making Decisions
Chapter 4 (Conditional Statements)
Engineering Problem Solving with C++, Etter/Ingber
Condition Statements.
Chapter 4: Making Decisions.
Programming Fundamentals
Chapter 4 - Program Selection
Chapter 4: Making Decisions.
Introduction to Programming
Lecture 2: Logical Problems with Choices
Decision Making.
How to develop a program?
CS1100 Computational Engineering
Chapter 5 Decision Making and Branching
Structured Program
Chapter 4 - Program Control
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Week 3 – Program Control Structure
Control Structures Lecture 6.
Department of Computer Science
CSC215 Lecture Control Flow.
ICS103: Programming in C 5: Repetition and Loop Statements
REPETITION Why Repetition?
Presentation transcript:

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);