C++ for Beginners Chapter-4 (Flow of control: Conditional statements)

[Introduction]
Generally a program a executes its statements from beginning to end. But not many programs execute its statements in the same order.Most programs decide the order of their execution and such programs uses control statements to decide the order of their execution.Control statements are of two types:1)Selection statements2)Iteration statementsin this tutorial we will learn about the selection statements, statements are the instructions given to the computer to perform a specific type of action.also read:C++ for beginners chapter-3

[Selection Statements]
The selection statement allow to choose the set of instructions for execution depending upon a certain condition. C++ provides two types of selections statements:if and switch.The selection statements are also called conditional statements or decision statements.Now let's first study about the if statement of C++
[The if statement]
An if statement tests a particular condition; if the condition evaluates to true a particular action is performed otherwise it is ignored, the syntax for if statement is shown below
if(expression){statement;}
The expression must be enclosed in parentheses.Let us see an example base on the if statement

#include<iostream.h> #include<conio.h> void main() { clrscr(); char a; cout<<"Enter a space or digit"; cin>>a; if(a==' ') { cout<<"space is entered"; } if(a<=9 && a>=0) { cout<<"a digit is entered"; } getch(); } 

The above example reads a character. If the character input is a space, it flashes a message specifying it, if it is a digit, it flashes a message specifying it.In the above example we have used some new operators «==» «<=» «>=» and «&&»Now let us first see what each of these operator means.


Equality operator(==):The equality operator, never gets confused between the «=» and «==» operator the «=» operator is used for assigning values to the variables while the «==» is used for finding the equality. For example 2 == 2 which means the 2 is equal to 2 so the condition is true now lets say 2 == 3 here 2 is not equals to 3 so the condition is false.
Greater than equals to(>=):This operator is used to check whether a given value is greater than or equals to a set value or not for ex: 4 >= 3 which is true or 3 >-3 which is also true now lets say 4>=6 which is false.
Smaller than equals to(<=):This operator is used to check whether a given value is smaller than or equals to a set value or not for ex: 2 <= 3 which is true or 3 <=3 which is also true now lets say 7<=6 which is false.
Logical and operator(&&):This operator is used to combine two conditions for exampleif(3>=2 && 2<=1){cout<<«hello»;}in the above condition two conditions are combined using the && operator the statement will not get executed until both the conditions are true if any of them is false the statement will not get executed for exampleif(1>=2 && 2<=1){cout<<«hello»;}the above statement will not get executed because one of the condition in the above example is false, this is how the && operator works this operator only makes the condition true when all the sub conditions are true, if any condition is false then it will not allow them to be executed.
All the examples of if you have seen so far allow you to execute a set of statements if a condition is true.What if there is another course of action to be followed if the expression evaluates to false for this problem we use an another clause called else, the syntax of the if-else statement is the following:if(expression){statement 1;}else{statement2;}The else clause do not come with any condition, if the condition is true then statement 1 gets executed otherwise statement 2 gets executed.following example illustrate the working of the if-else statement:

#include<iostream.h> #include<conio.h> void main() { clrscr(); char a,x,y; cout<<"Enter two digits"; cin>>x>>y; cout<<"Press 1 for addition"; cout<<"Press 2 for subtraction"; cin>>a; if(a == 1){ cout<<x+y; } else{ cout<<x-y; } getch(); } 


[Nested if's]
A nested is a if in another if statement in it's body or in else's body nested can have the following three forms:1.)if(expression 2)statement 1;elsestatement 2;
elsebody-of-else;
2.)if(expression 1)body-of-if;else{ if(expression 2) statement 1; else statement 2;}|
3.)if(expression 1){ if(expression 2) statement 1; else statement 2;}else{ if(expression 3){ statement-3; else statement 4;}but don't you think that sometimes nested if's can be confusing, so to solve this problem we have the 'else if' statement we use this statement when we have more than two possibilities for given problem, let's see the syntax of the else if statement


if(expression 1){statement 1;}else if(expression 2){statement 2;}……….……….else if(expression n)[statement n;}else{statement;}you can add as many else if statement as you can in your program.Let's see a program to understand the else if statement

#include<iostream.h> #include<conio.h> void main() { clrscr(); char a,x,y; cout<<"Enter two digits"; cin>>x>>y; cout<<"Press 1 for addition"; cout<<"Press 2 for subtracrion"; cin>>a; if(a == 1){ cout<<x+y; } else if(a == 2){ cout<<x-y; } else{ cout<<"Wrong Entry"; } getch(); } 

[The switch statement]C++ provides a multi branch selection statement known as switch.This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found , the statements with that constant are executed. The syntax of switch statement is as follows:
switch(expression){case constant 1:statement sequence 1; break;case constant 2:statement sequence 2; break;…………... …. .case constant n:statement sequence n; break;default: statement sequence; break;}The expression is evaluated and its value are matched against the values of the constants specified in the case statement.When a match is found,the statement sequence associated with that case is executed until the break statement or the end of switch statement is reached.The break statement is a C++ jump statement (you will learn about the jump statements in later chapters of this series)When a jump statement is encountered in a switch statement , program execution jumps to the line of code following the switch statement outside the body of switch statement.Let us see a program based on switch statement.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a; cout<<"Enter a number(1-7)"; cin>>a; switch(a) { case 1: cout<<"day is monday"; break; case 2: cout<<"day is tuesday"; break; case 3: cout<<"day is wednesday"; break; case 4: cout<<"day is thursday"; break; case 5: cout<<"day is friday"; break; case 6: cout<<"day is saturday"; break; case 7: cout<<"day is sunday"; break; default: cout<<"Wrong entry"; break; } getch(); } 

[The switch Vs if-else]
1.The switch statement differ from the if statement in that switch can only test for equality whereas if can evaluate a logical or relational expressions multiple conditions. 2.switch statement select its branch by testing the value of same variable whereas the if else construction let you use a series of multiple expression3.If-else can handle ranges while switch cannot.4.If-else can handle floating points while switch cannot.
So that's it for now,in the next chapter we will learn about the iteration statements.Till then GoodBye.If you like the article please share it and help us grow.

Понравилась статья? Поделиться с друзьями:
Добавить комментарий