Control Structures Selection

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Decisions If statements in C.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Selection. Flow Chart If selection If/else selection Compound statement Switch.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Control of flow We learned that default flow of instructions is sequential. Then, we learned how to control the flow using "if" and "switch." Now, we will.
Chapter#3 Part1 Structured Program Development in C++
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
If/Else Statements.
CNG 140 C Programming (Lecture set 3)
REPETITION CONTROL STRUCTURE
Decisions Chapter 4.
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Intro to Programming Week # 5 Repetition Structure Lecture # 9
Lecture 2: Logical Problems with Choices
Intro to Programming Week # 3 If-else Statement Lecture # 6
CS1100 Computational Engineering
Introduction to Algorithms and Programming COMP151
Structured Program
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Control Structures Repetition
Introduction to Programming - 3
3 Control Statements:.
Visual Basic – Decision Statements
Dr. Khizar Hayat Associate Prof. of Computer Science
Introduction to Algorithms - 1
Chapter 4: Control Structures I (Selection)
Introduction to Programming – 4 Operators
Control Structures Part 1
2.6 The if/else Selection Structure
Passing Arrays to functions
Structured Program Development in C++
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Statements Paritosh Srivastava.
Introduction to Algorithms and Programming
Dr. Khizar Hayat Associate Prof. of Computer Science
Control Structures contd… Selection
Introduction to Algorithms and Programming COMP151
Introduction to Programming - 1
Introduction to Algorithms - 2
Dr. Khizar Hayat Associate Prof. of Computer Science
Structural Program Development: If, If-Else
Presentation transcript:

Control Structures Selection Dr. Khizar Hayat Associate Prof. of Computer Science

Control structures A program usually follows the sequence in which it’s statements are written. However it may deviate from linear sequence and may either repeat a given block of code or take decision to execute or skip certain block. C++ provides control structures for these two purposes. A block or a compound statement is a group of statements inside the delimiters ‘{}’, i.e. { statement 1; statement 3; . } As already discussed, there are three control structures Sequence (programs execute sequentially, by default) Selection – the if, if else, switch Repetition – while , do… while, for

Algorithm to input two numbers and print the smaller number //using if-else #include<iostream> using namespace std; int main() { int num1, num2; cout<<"Enter the 1st Number"<<endl; cin>>num1; cout<<"Enter the 2nd Number"<<endl; cin>>num2; if(num1<num2) { //the then part cout<<num1<<“ is minimum"<<endl; } else //the else part cout<<num2<<“ is minimum"<<endl; return(0); Start Pseudocode begin get num1 get num2 if num1 < num2 then display num1 as smaller else display num2 as smaller end if end Read Num1 Read Num2 Num1 < Num2? No Yes Display Num1 as min Display Num2 as min Stop

Algorithm to input two numbers and print the smaller number //using the ternary operator (?:) #include<iostream> using namespace std; int main() { int num1, num2, min; cout<<"Enter the 1st Number"<<endl; cin>>num1; cout<<"Enter the 2nd Number"<<endl; cin>>num2; num1<num2?(min=num1):(min=num2); cout<<min<<“ is minimum"<<endl; return(0); } Start Pseudocode begin get num1 get num2 if num1 < num2 then display num1 as smaller else display num2 as smaller end if end Read Num1 Read Num2 Num1 < Num2? No Yes Display Num1 as min Display Num2 as min Stop

Structure of if statement if (logical condition)//evaluates to true or false //executed only if logical condition is true { //one or more statements here } else //executed only if logical condition is false //optional part

Example 2 #include<iostream> using namespace std; int main() { // for a single statement you can ignore the delimiters ({}) of if and else #include<iostream> using namespace std; int main() { float number; cout<<"Enter the Value for Number"<<endl; cin>>number; if(number>=0) //never use semicolon (;) here cout<<"The given number is Positive"<<endl; else //never use semicolon (;) here cout<<"The given number is Negative"<<endl; return(0); }

Example 2 // for a single statement you can ignore the delimiters ({}) of if and else #include<iostream> using namespace std; int main() { float number; cout<<"Enter the Value for Number"<<endl; cin>>number; if(number>=0) //never use semicolon (;) here cout<<"The given number is Positive"<<endl; } else //never use semicolon (;) here cout<<"The given number is Negative"<<endl; return(0); /*It is strongly recommended to use the delimiters ({}), even if a single statement Advantage: READIBILITY*/

Example 3 #include<iostream> using namespace std; int main() { int number; cout<<"Enter the Value for Number"<<endl; cin>>number; if(number%2==0) //never use semicolon (;) here cout<<"The given number is EVEN"<<endl; } else //never use semicolon (;) here cout<<"The given number is ODD"<<endl; return(0);

Example 4 //The if block is possible without else #include<iostream> using namespace std; int main() { float maqsoom, maqs_alaih, quotient; cout<<"Enter the Value of Dividend"<<endl; cin>>maqsoom; cout<<"Enter the Value of Divisor"<<endl; cin>>maqs_alaih; if(maqs_alaih==0) //cout<<"The divisor must not be zero"<<endl; maqs_alaih= 0.000000001; //just a small number in place of zero } quotient= maqsoom/maqs_alaih; cout<<"The result is "<<quotient<<endl; return(0);

Example 5 #include<iostream> using namespace std; int main() { char letter; cout<<"Enter the character"<<endl; cin>>letter; if((letter=='a')||(letter=='A')||(letter=='e')||(letter=='E')||(letter=='i')||(letter=='I')||(letter=='o')||(letter=='O') ||(letter=='u')||(letter=='U')) cout<<"The given character is Vowel"<<endl; } else cout<<"The given character is not vowel"<<endl; return(0);