How do I use if else statement in C++.
What if else statement does? it is very useful because this is how we call the code to do the work for us, if this is not equal or equal or less and greater, then the else will do it's following.
Creating a integer to hold the variable guess.
Print on the screen ask user what suppose to do.
cout << "How old is Laurand? " << endl;
Input for user to enter the guess number.
cin >> guess;
if(guess == 35)
User will be asked to guess the age and if user enters the correct age number, on the screen will be printed nice guess.
cout << "Nice guess, Laurand is 35 old." << endl;
If the user didn't guess then the else statement prints on the screen as not a nice guess.
else cout << "Ops not a nice guess!" << endl;
guess == 35
Guess == 35 tells if it is equal to 35, it is correct otherwise any number greater or less than 35 will follow with the else statement.
So this is how usefull the if else statement is.
Watch live in action creating the program, or scroll below to copy the entire code.
Feel free to use the code.
Last update .