Easy Tutorial: Computer Programming for DUMMIES (not language specific) -- very helpful for beginners
Hey guys! I want to note that this tutorial doesn't just apply to C++. This tutorial will be covering if(), else, and else if() statements. These statements are in pretty much every programming language, and look just about the same in a lot of languages. This tutorial is part 4 of my programming tutorial series. If you need to catch up, here are the others:
Before we look at these statements, we need to understand operators.
Relational Operators
'==' Checks if the values of two operands are equal or not. If yes, then the condition becomes true
'!=' Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true
'>' Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true
'<' Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true
'>=' Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true
'<=' Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true
Logical Operators
'&&' Called Logical AND operator. If both the operands are non-zero, then the condition becomes true
'||' Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true
'!' Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false
Examples
(3 < 6) evaluates to true
(5 <= 5) evaluates to true
(5 == 6) evaluates to false
(5 != 1) evaluates to true
(!(5 == 6)) evaluates to true -- (5 == 6) evaluates to false, then '!' negates it to true
((4 == 6) || (4 > 2)) evaluates to true -- only one side has to be true
((4 < 6) && (4 > 50)) evaluates to false -- only one side is true
For more information on operators:
http://www.tutorialspoint.com/cprogramming/c_operators.htm
if()
if() statements run a segment of code only if the parameters in the parenthesis evaluate to true (1). Examples:
if(true)
{
/* do something */
}
more specifically...
if(x == 1)
{
cout << "x is equal to one" << endl;
}
else
else statements are placed right after if() statements. If the if() statement evaluated to false, the if() statement will be skipped, and the else statement will run. If the if() statement evaluated to true, then the if() statement will run, then skip the else statement. Examples:
if(true)
{
/* this segment runs */
}
else
{
/* and this segment does not */
}
if(false)
{
/* this segment does not run */
}
else
{
/* and this segment does */
}
else if()
This is placed after an if() statement. It only runs if the above if() statement was false, and if it evaluates its conditions true just like an if statement. Examples:
if(true)
{
/* this segment runs */
}
else if(true)
{
/* and this segment does not */
}
if(false)
{
/* this segment does not run */
}
else if(true)
{
/* this segment does */
}
else
{
/* this segment doesn't run because the else if() statement above was true */
}
if(false)
{
/* this segment does not run */
}
else if(false)
{
/* this segment does not run either */
}
else
{
/* this segment runs */
}
You can place as many else if() statements after an if() statement as you want. You can only place one else statement after an if() or an else if() statement.
Here is a program I wrote to give you a better idea on how these statements work:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int x, y, z;
x = 1;
y = 2;
z = 3;
//////////////////////////////////////////////////////
if(x == y)
{
cout << "x is equal to y\n\n";
}
else
{
cout << "x is NOT equal to y\n\n";
}
///////////////////////////////////////////////////////
if(x > z)
{
cout << "x is greater than z\n\n";
}
else if(y > z)
{
cout << "y is greater than z\n\n";
}
else if(x < y && x < z)
{
cout << "x is less than both y and z\n\n";
}
else
{
cout << "y is less than x\n\n";
}
return 0;
}
And here is the output:
[cmw4026@omega test]$ g++ if.cpp
[cmw4026@omega test]$ ./a.out
x is NOT equal to y
x is less than both y and z
[cmw4026@omega test]$
I hope this was helpful! Leave criticisms and questions in the comments!