Learn the "if" statement

Published on: June 4, 2010

This tutorial will allow you to easily master the "if" statement in C++. The "if" statement is the basis of logic in all programming. It allows you to say whether a specific piece of code should execute based on specific conditions, if the conditions aren't met, either move on to the next condition, or don't execute anything at all. Usually, all "if" statements are completed with an "else" statement.

It's a short program and only takes a few minutes. Be sure to check out my other Easy Programming videos for more beginner C++ Practice. I will post more complex tutorials as time goes by. I hope you enjoy it!

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.


//Program: Nazmus
//Program: Shirt Count
//Website: EasyProgramming.net

#include <iostream>
using namespace std;

void main()

 int shirts;
 float dollar;

 cout << "Please enter number of shirts: ";
 cin >> shirts;

 if(shirts <= 5) dollar = shirts * 10;
 else dollar = shirts * 8;

 cout << shirts << " will cost you $" << dollar << '.' << endl;