Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures Selection

Similar presentations


Presentation on theme: "Control Structures Selection"— Presentation transcript:

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

2 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

3 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

4 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

5 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

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

7 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*/

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

9 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= ; //just a small number in place of zero } quotient= maqsoom/maqs_alaih; cout<<"The result is "<<quotient<<endl; return(0);

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


Download ppt "Control Structures Selection"

Similar presentations


Ads by Google