Wednesday, April 2, 2014

[Tutorial C++] Basics of Programming Part 4

Conditional operators, if and if-else statements

Conditional operators
All the below operators are classified as conditional operators. These operators fall under this and category and are so called because they are used to check for conditions in conjunction with if (also it is used when ever we are making an expression we will see this later)  and then a desired outcome is set for them when it true.

  • ==  (Equal to)
  • !=   (Not equal to)
  • <    (Less than)
  • >    (Greater than)
  • <=  (Less than or equal to)
  • >=  (Greater than or equal to)

We can use these to check all kinds of useful things in our program like check for which student has the highest mark or which has the lowest mark or even check for who has the average marks. These operations though cannot be done on its own and we have to use something new.

if and if-else statements
"if" statements are one of the most useful things in programming languages and almost all languages have the same line of command. You compare something and then set a desired result if the condition is met. Though in "if-else" we compare something and then if the conditions fails we can decide on whether we want to set another condition or we can just let it go. This is how you would normally write an if and if-else statement.

For if
if (condition)
{
perform this
}

For if-else
if (condition)
{
perform this
}
else 
      if(condition)
{
perform this}

Another way to write if-else but the usage is in a different way
if (condition)
{
perform this}
else 
{
perform this}
 
Notice that there are two ways to write an if-else statement, in the first way if one condition fails then you can write up another condition this can go on till the number of conditions you want but in the second one if the condition fails then you don't check for anything rather than you just tell the program to just do this. Now here's an example on how you would use these.

#include<iostream> //Declaring header file
#include<conio.h>  //Declaring header file

using namespace std;

int main() //main function
{
int marks[5]; //declaring an integer array

cout<<"Enter the marks of the five students."<<endl; //Displaying a message
cout<<"Enter student 1 marks"<<endl;  //Displaying a message
cin>>marks[0]; //taking input from user
cout<<"Enter student 2 marks"<<endl;  //Displaying a message
cin>>marks[1]; //taking input from user
cout<<"Enter student 3 marks"<<endl;  //Displaying a message
cin>>marks[2]; //taking input from user
cout<<"Enter student 4 marks"<<endl;  //Displaying a message
cin>>marks[3]; //taking input from user
cout<<"Enter student 5 marks"<<endl;  //Displaying a message
cin>>marks[4]; //taking input from user

if (marks[0]>90) //if statement to check whether student 1 got first position
cout<<"First position goes to student 1"; //if true then display this
 else
  if (marks[1]>90) //if statement to check whether student 2 got first position
  cout<<"First position goes to student 2"; //if true then display this

   else
    if (marks[2]>90) //if statement to check whether student 3 got first position
    cout<<"First position goes to student 3"; //if true then display this

    else
     if (marks[3]>90) //if statement to check whether student 4 got first position
     cout<<"First position goes to student 4"; //if true then display this
     else
      if (marks[4]>90) //if statement to check whether student 5 got first position
      cout<<"First position goes to student 5"; //if true then display this


getch();
}

 After seeing this and if you got the concept properly or by just testing it you will see that if for example both student 1 and student 2 get marks greater than 90 then the message that is shown tells first position goes to student 1 it doesn't tells that it goes to both. This is due to how if-else works, it works in a step-wise procedure so if first "if" becomes true then it shall skip the rest "if-else" statements otherwise if second "if" becomes true then it will skip the rest "if-else" statements after it and so on. If you don't want this and want to show for all students who got greater than 90 marks then remove all "else" from the code. Also you might have noticed that I haven't written the "perform this" code in square brackets well if you have only a single statement then you don't need the square brackets otherwise you do. I hope you got everything that I was trying to tell and like always here's how the program ran.

 

Widgets

 

Copyright @ 2014 CPP Fuzz.