Control Structures

A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done  by our program, when and under which circumstances.    

Here, we will discuss about if else statement, loops  , break or continue statement and switch statement.

If statements :


The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The  if  statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user entered password, your program can decide whether a user is allowed access to the program.




When programming, the aim of the program will often require the checking of one value stored by a variable against another value to determine whether one is larger, smaller, or equal to the other.

There are a number of operators that allow these checks.

Here are the relational operators, as they are known, along with examples:
 
>     greater than              5 > 4 is TRUE
<     less than                 4 < 5 is TRUE
>=    greater than or equal     4 >= 4 is TRUE
<=    less than or equal        3 <= 4 is TRUE
==    equal to                  5 == 5 is TRUE
!=    not equal to              5 != 4 is TRUE

It is highly probable that you have seen these before,  probably with 
slightly different symbols. They should not present any hindrance to 
understanding. Now that you understand TRUE and FALSE in computer 
terminology as well as the comparison operators, let us look at the 
actual structure of if statements.

The structure of an if statement is as follows:
 
if ( True )
Execute the next statement

Here is a simple example that shows the syntax:

 
#include<iostream>
using namespace std;
int main(){
int a = 10 , b = 6; 
if ( a < b )
cout<<"You may be a human !! "<<endl;
 
} 

Else Statement:

Sometimes when the condition in an if statement evaluates to false, it would be nice to execute some code instead of the code executed when the statement evaluates to true. The "else" statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.

It can look like this:
 
if ( TRUE ) {
  // Execute these statements if TRUE
}
else {
  // Execute these statements if FALSE
}

Here is a simple example that shows the syntax:
 
#include<iostream>
using namespace std;
int main(){
int a = 10 , b = 6; 
if ( a < b ) {
cout<<"You may be a human !! "<<endl; 
}
 
else {
cout<<" May be, you don't have mathematical knowledge"<<endl; 
}
return 0;
} 

Else If Statement:

Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement's body to execute. You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed.

if ( <condition> ) {
  // Execute these statements if <condition> is TRUE
}
else if ( <another condition> ) {
  // Execute these statements if <another condition> is TRUE and
  // <condition> is FALSE
}


Let's look at a simple program for you to try out on your own.
 
#include <iostream> 

using namespace std;
  
int main()                            // Beginning of the program!
{
  int age;                            // Need a variable...
  
  cout<<"Please input your age: ";    // Asks for age
  cin>> age;                          // The input is put in age
                       
  if ( age < 100 ) {                  // If the age is less than 100
     cout<<"You are pretty young!\n"; // Just to show you it works...
  }
  else if ( age == 100 ) {            // I use else just to show an example 
     cout<<"You are old\n";           // Just to show you it works...
  }
  else {
    cout<<"You are really old\n";     // Executed if no other statement is
  }
 return 0;
}
 
 
 
 
 
 
 
 
 

No comments:

Post a Comment