Decisions Chapter 4.

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
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
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.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Computer Science Department Relational Operators And Decisions.
DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Chapter 4 Selection Structures: Making Decisions.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
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
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
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.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Chapter 4: Control Structures I
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Week 3 C Program Structures (Selection Structures)
Chapter 4 (Conditional Statements)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 – Selection Structures
Chapter 2.1 Control Structures (Selection)
Condition Statements.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
DKT121: Fundamental of Computer Programming
Chapter 6: Conditional Statements and Loops
Control Structures.
Looping.
Chapter 4: Control Structures I
Lecture 2: Logical Problems with Choices
Compound Assignment Operators in C++
Control Structures: Selection Statement
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Relational, Logical, and Equality Operators
SELECTIONS STATEMENTS
Lecture Notes – Week 2 Lecture-2
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
Control Structures: Selection Statement
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CSC215 Lecture Control Flow.
Selection Control Structure
CSCE 206 Lab Structured Programming in C
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

Decisions Chapter 4

Introduction Computer Languages, too, must be able to perform different sets of actions depending on the circumstances C language has three major decision making structures: If Statement If-Else Statement Switch Statement

If Statement Used for Condition statement The “if statement” is used to execute or ignore a set of statements after testing a condition The “ if statement” evaluates a condition If the given condition is true, the statement or set of statements following the “if statement” is executed If condition is false, the statement or set of statements following the “if statement” condition is ignored and the control transfers to the next statement

If Statement Syntax Single Statement if (condition) statement 1; Multiple Statements { Statement 1; Statement 2; Statement 3; Statement m; } Statement n;

If Statement Program void main (void) { char ch; ch = getche( ); if ( ch = = ‘ y ‘ ) printf ( “ \n You typed y.” ); } Output: y You typed y.

If Statement Operations (Flow Chart) Test Condition FALSE TRUE Body of if Statement Statement immediately after if statement

If-Else Statement The “ if-else statement” is used for making two-ways decisions. Either one of two blocks of statements is executed after evaluating a condition Syntax – 1 if (condition) statement 1; else statement 2; Syntax – 2 : if (condition) { Statement 1; Statement 2; First Block Statement m; } Statement 2; Second Block Statement n;

If-Else Statement Operations (Flow Chart) Test Condition TRUE FALSE Block 1 Block 2 Statement immediately after if else statement

If-Else Statement Program void main (void) { char ch; ch = getche( ); if ( ch = = ‘ y ‘ ) printf ( “ \n You typed y. “ ); else printf ( “ \n You did not typed y. “ ); } Output: a You did not typed y. y You typed y.

Nested If-Else Statement Operations (Flow Chart) Test Condition TRUE Block FALSE Test Condition TRUE Block FALSE Statement immediately after if else statement

Nested Statements void main(void) { int temp ; clrscr ( ) ; printf ( “ Type today’s temperature. “ ) ; scanf ( “ %d “, &temp ) ; if ( temp < 80 ) if ( temp > 60 ) printf ( “ Nice Day.“ ) ;} } else printf ( “ Sure is Hot! “ ) ;

Switch Statement The “switch statement” is used as substitute of nested if-else” statements. It is used when multiple choices are given and one choice is to be selected. switch (expression) { case const1: statements; break; case const2: case const3: case const4: default: }

printf ( “ = %f “, num1 * num2 ) ; break ; case ‘ / ‘ : void main (void) { float num1 = 1.0 , num2 = 1.0 ; char op ; clrscr ( ) ; while ( ! ( num1 = = 0.0 && num2 = = 0.0 ) ) printf ( “ Type number , operator , number : \n “ ) ; scanf ( “ %f %c %f “, &num1, &op, &num2 ) ; switch ( op ) case ‘ + ‘ : printf ( “ = %f “, num1 + num2 ) ; break ; case ‘ - ‘ : printf ( “ = %f “, num1 - num2 ) ; case ‘ * ‘ : printf ( “ = %f “, num1 * num2 ) ; break ; case ‘ / ‘ : printf ( “ = %f “, num1 / num2 ) ; default : printf ( “ Unknown Operator “ ) ; } printf ( “ \n \n “ ); getch ( );

Switch Statement Operations (Flow Chart) Test Condition TRUE Block FALSE Test Condition TRUE Block FALSE Statement immediately after switch statement

Nested if else Statement Switch Statement It becomes complicated for multiple selections It uses an independent expression for each case The test condition can be given in special range of values. If given condition match then the statements under it will be executed. It is easy to understand for multiple selections It uses a single expression for all cases. But each case must have constant value of integer type or character type Only a single expression is given in the switch statement which returns a single value. The test condition cannot be given in a specified range. It is the major drawback of the switch statement.

Logical Operators The logical operators are used to combine relational expressions or relational conditions. The expression containing logical operators is called logical expression or logical condition. It is also called Compound Condition or Compound Expression There are 3 types of Logical Operators or Boolean Operators && AND operator || OR operator ! NOT operator

Logical Operator Program void main (void) { int x ; clrscr ( ) ; printf ( “ Press the number 5 or 6 : “ ) ; scanf ( “ %d “, &x ) ; if ( x = = 5 | | x = = 6 ) printf ( “You have typed the right number. “ ) ; else printf ( “ Sorry wrong number. “ ) ; } Output: 2 Sorry wrong number. 5 Yes You have typed the right number.

The Conditional Operator The conditional operator is used as an alternative of a simple if-else statement. (condition)? Expression 1 : Expression 2 For example: result = (a>b)?a:b If(a>b) result = a; Else result = b;

Lab Work Write a program to input four integers. Find out the largest value and then print it on the screen by using if statement. Write a program to input single character and print a message “it is a vowel” if it is a vowel otherwise print message “it is a consonant’. Use if-else structure and OR (||) operator only. Write above program by using switch statement Hint: vowel characters (A, E,I, O, U)