Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

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

Fundamental of C programming
Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS.
Unit 2 The if-else-if LECTURE – 14
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Fundamentals of C and C++ Programming Control Structures and Functions.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Control Statements Spring Semester 2013Programming and Data Structure1.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Control Statements (Decision Making)
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Chapter 5: Structured Programming
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Iterations Very Useful: Ability to repeat a block of code Example:
Example #include void main() { int a,b,c,n; clrscr(); printf("\nEnter the value of a,b:"); scanf("%d%d",&a,&b); printf("\nMENU"); printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT");
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
INTRODUCTION TO C. C was developed by Dennis Ritchie at Bell laboratory in 1972 It is an upgrade version of languages B and BCPL.
Statements
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
Types of Operator Arithmetic operator Relational operator Logical operator Assignment operator Increment or decrement operator(unary) Bitwise operator.
Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
UNIT II C PROGRAMMING BASICS Problem formulation – Problem Solving - Introduction to ‘ C’ programming –fundamentals – structure of a ‘C’ program – compilation.
Program Program is a collection of instructions that will perform some task.
Functions The fruitful & parameterized functions.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Algorithm & Flow Charts Decision Making and Looping
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CSC COMPUTER EDUCATION, M.K.B.NAGAR CHAPTER 3. CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Objectives Explain If, If..else statements Understand Looping.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
The Repetition control structure using while loop.
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.
Control Structures Introduction
WHILE, DO-WHILE AND FOR LOOPS
‘C’ Programming Khalid Jamal.
EKT120 COMPUTER PROGRAMMING
INTRODUCTION TO C.
Unit VI- Selection – Making Decisions, Repetition
Decisions Chapter 4.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Condition Statements.
Looping.
Chapter 4 - Program Control
Loops in C.
Department of Computer Science
REPETITION Why Repetition?
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration structure – Encapsulation structure

Decision Making (cont) Sequential structure – In which instructions are executed in sequence. Selection structure – In which instruction are executed based on the result of some condition. Iteration structure – In which instruction are executed repeatedly. Encapsulation structure – In which some compound structure are used.

SELECTION STRUCTURE It allows the program to make a choice from alternative paths. C provide the following selection structures – IF statement – IF … ELSE statement – Nested IF … ELSE statement – IF … ELSE ladder

IF Statement Syntax IF (condition is true) { Statements; } If condition False True Statements

Example #include void main ( ) { int a; clrscr( ); printf("\nEnter the number:"); scanf("%d",&a); if(a>10) { printf(" \n a is greater than 10"); } getch( ); }

Output Enter the number: 12 a is greater than 10

IF…ELSE Statement Syntax IF (condition) { True statements; } ELSE { False statements; } If Condition True False True statements False statements

#include void main ( ) { int a; clrscr( ); printf("\nEnter the number:"); scanf("%d",&a); if(a>10) { printf(" \n a is greater than 10"); } else { printf(" \n a is less than 10"); } getch( ); }

NESTED IF… ELSE If Condition 2 True False True statements False statements If Condition 1 False Statements True

NESTED IF… ELSE Syntax IF (condition1) { IF (condition2) { True statements; } ELSE { False statements; } ELSE { False statements; }

IF…ELSE LADDER Condition 1 Statements Condition 2 Statements Condition 3 Statements TRUE FALSE

IF…ELSE LADDER Syntax IF (condition1) { statements; } else if (condition2) { statements; } else if (condition3) { statements; } else { statements; }

Example #include void main() { int m1,m2,m3; float avg; printf("\nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("\n The average is:%f",avg); printf("\n The Grade is:"); if(avg>=60) { printf("First class"); }

else if(avg>=50) { printf("Second class"); } else if(avg>=35) { printf("Thrid class"); } else { printf("Fail"); } getch(); }

Output Enter the marks: The average is: The Grade is: First class

Looping structure It is used to execute some instructions several time based on some condition. – WHILE – Do…WHILE – For

WHILE Loop Syntax. WHILE (condition) {. Body of the loop;. } Body of The loop condition False True

Example #include void main() { int i=1,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); while(i<=n) { fact =fact *i; i++; // i=i+1 } printf("\n The value of %d! is:%d",n,fact); getch(); }

Output Enter the Number:3 The value of 3! is: 6

DO…WHILE Loop Syntax do { Body of the loop }while (condition); Body of The loop condition False True

for loop Syntax for (initialization; test condition; Increment/Decrement) { Body of the loop }

for loop Initialization condition False Body of the loop Inc / Decrement

Example #include void main() { int i,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact =fact *i; } printf("\n The value of %d! is:%d",n,fact); getch(); }

Output Enter the Number:3 The value of 3! is: 6

Nested for loop Syntax for (initi; cond; Inc/Dec) { for (initi; cond; Inc/Dec) { Body of the loop }

CASE structure Case 1 Case 2 Default case Switch

CASE structure Syntax switch (expression) { case constant 1: block1; break; case constant 2: block2; break;. default : default block; break; }

Example #include void main() { int i,n; printf("\nEnter the Number:"); scanf("%d",&n); switch(n) { case 1: { printf("\n Its in case 1"); break; }

case 2: { printf("\n Its in case 2"); break; } default: { printf("\n Its in default"); break; } getch(); }

Output Enter the Number:2 Its in case 2

break Statement It is used to terminate the loop When a break statement is encountered inside a loop, then the loop is terminated.

Loops with break Statement while(cond) { ………… if(cond) break; ………… }

do { ………… if(cond) break; ………… } while(cond);

for (initi; condt; Inc/Dec) { ………… if(cond) break; ………… }

Continue Statement When a continue statement is encountered inside a loop, the control is transferred to the beginning.

Loops with continue Statement while(cond) { ………… if(cond) continue; ………… }

do { ………… if(cond) continue; ………… } while(cond);

for (initi; condt; Inc/Dec) { ………… if(cond) continue; ………… }

goto Statement When a goto statement is encountered inside a loop, the control is transferred to the beginning.

Syntax for goto Statement label: ………… goto label; …………

goto label; ………… label: …………

getchar() Example #include void main() { char x; printf("enter the character:"); x=getchar();

if(islower(x)) putchar(toupper(x)); else putchar(tolower(x)); getch(); } Output: enter the character:ABC a

getche() Example #include void main() { char c ; clrscr(); printf("\nInput a string:"); c = getche();

printf("\nstring is:"); putch(c); getch(); } Output: Input a string:k string is:k

Getch() Example #include void main() { char c; clrscr(); printf("\nInput a string:"); c = getch();

printf("\nstring is:"); putch(c); getch(); } Output: Input a string: string is:h

getc Example #include void main() { char x; printf("enter the character:"); x=getc(stdin);

if(islower(x)) putc(toupper(x),stdout); else putc(tolower(x),stdout); getch(); } Output: enter the character:abc A

gets() Example #include void main() { char c[80]; clrscr(); printf("Input a string:"); gets(c);

printf("The string is:"); puts(c); getch(); } Output: Input a string:qwerty The string is:qwerty

Example #include void main() { int a,b,c,n; clrscr(); printf("\nEnter the value of a,b:"); scanf("%d%d",&a,&b); printf("\nMENU"); printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT"); printf("\nEnter the choice:"); scanf("%d",&n);

switch(n) { case 1: c=a+b; printf("\nThe result of Addition is:%d",c); break; case 2: c=a-b; printf("\nThe result of Subtraction is:%d",c); break;

case 3: c=a*b; printf("\nThe result of Multiplication is:%d",c); break; case 0: exit(0); break; } getch(); }

Output Enter the value of a,b:5 6 MENU 1.ADD 2.SUB 3.MULTIPLY 0.EXIT Enter the choice:1 The result of Addition is:11

Finding Armstrong No #include void main() { int r=0,sum=0,n,a; printf("\nEnter the number:"); scanf("%d",&n); a=n; while(n>0) { r=n%10; sum=sum+r*r*r; n=n/10; }

if(a==sum) { printf("\nIt is an armstrong number"); } else { printf("\nIt is not an armstrong number"); } getch(); }

Output Enter the number:153 It is an armstrong number

Sum of the Digits #include void main() { int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10;

sum=sum+r; n=n/10; } printf("sum of the digits is:%d",sum); }

Output Enter the no:156 sum of the digits is:12

Reverse of a number #include void main() { int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)

{ r=n%10; sum=sum*10+r; n=n/10; } printf("Reverse of the number is:%d",sum); getch(); }

Output Enter the no:567 Reverse of the number is:765

Fibonacci Series #include void main() { int f=0,f1=-1,f2=1,n,i; printf("\nEnter the number:"); scanf("%d",&n);

while(f<n) { f=f1+f2; f1=f2; f2=f; printf("\t%d",f); } getch(); }

Output Enter the number:

Swapping #include void main ( ) { int a,b,c; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b); c=a; a=b; b=c;

printf(" \nThe value of a is:%d",a); printf(" \nThe value of b is:%d",b); getch( ); } Output: Enter the value of a:5 Enter the value of b:4 The value of a is:4 The value of b is:5

Swapping without using third variable #include void main ( ) { int a,b; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b);

a=a+b; b=a-b; a=a-b; printf(" \nThe value of a is:%d",a); printf(" \nThe value of b is:%d",b); getch( ); } Output: Enter the value of a:5 Enter the value of b:6 The value of a is:6 The value of b is:5

Quadratic Equation #include void main ( ) { int a,b,c,d,r1,r2; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b); printf(" \nEnter the value of c:"); scanf("%d",&c); d=b*b-4*a*c;

if(d>=0) { r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf(" \nThe roots are %d,%d",r1,r2); } else { printf(" \nThe roots are imaginary"); } getch( ); }

Output Enter the value of a:4 Enter the value of b:5 Enter the value of c:6 The roots are imaginary