C++/Language/If

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Add an else part to the if statement

#include <iostream>
using namespace std;
int main(void)
{
   int num;
   cout << "Enter a whole number: ";
   cin >> num;
   if ( num % 2 == 0 )
      cout << "The number is even" << endl;
   else
      cout << "The number is odd" << endl;
   return 0;
}


Conditional Expressions

#include <iostream>
using namespace std;
int main()
{
   float x, y;
   cout << "Type two different numbers:";
   if( !(cin >> x && cin >> y) ) {                             
      cout << "\nInvalid input!" << endl;
   }else {
      cout << "\nThe greater value is: " << (x > y ? x : y)  << endl;
   }
   return 0;
}


if /else if /else statement in action

#include <iostream>
using namespace std;
int main(void)
{
   int testScore;
   cout << "Enter your test score: ";
   cin >> testScore;
   if (testScore >= 90 )
      cout << "Your grade is an A" << endl;
   else if (testScore >= 80 )
      cout << "Your grade is a B" << endl;
   else if (testScore >= 70 )
      cout << "Your grade is a C" << endl;
   else if (testScore >= 60 )
      cout << "Your grade is a D" << endl;
   else 
      cout << "Your grade is an F" << endl;
   return 0;
}


Nested if statement

#include <iostream> 
#include <cstdlib> 
 
using namespace std; 
 
int main() 
{ 
  int magic;   
  int guess;    
 
  magic = rand();                           // get a random number 
   
  cout << "Enter your guess: "; 
  cin >> guess; 
 
  if (guess == magic) { 
    cout << "** Right **\n"; 
    cout << magic << " is the magic number.\n"; 
  } 
  else { 
    cout << "...Sorry, you"re wrong."; 
    if(guess > magic) 
       cout <<" Your guess is too high.\n"; 
    else 
       cout << " Your guess is too low.\n"; 
  } 
 
  return 0; 
}


Testing if Both Boolean Expressions Are True

#include <iostream>
using namespace std;
int main(void)
{
   int age;
   char choice;
   bool citizen;
   cout << "Enter your age: ";
   cin >> age;
   cout << "Are you a citizen (Y/N): ";
   cin >> choice;
   if (choice == "Y")
      citizen = true;
   else
      citizen = false;
   if (age >= 18)
      if(citizen == true)
         cout << "You are eligible to vote";
      else
         cout << "You are not eligible to vote";
   else
      cout << "You are not eligible to vote";
   return 0;
}


Testing if Either Boolean Expression Is True

#include <iostream>
using namespace std;
int main(void)
{
   int age;
   cout << "Enter your age: ";
   cin >> age;
   if (age > 7)
      if (age >= 70)
         cout << "free";
      else
         cout << "You have to pay";
   else
      cout << "free";
   return 0;
}


The use of if-else statements

#include <iostream>
using namespace std;
int main()
{
   float x, y, min;
   cout << "Enter two different numbers:\n";
   if( cin >> x && cin >> y)  
   {                          
     if( x < y )              
        min = x;
     else
       min = y;
     cout << "\nThe smaller number is: " << min << endl;
   }
   else
     cout << "\nInvalid Input!" << endl;
   return 0;
}


Use an int value to control the if.

 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int a, b; 
 
  cout << "Enter numerator: "; 
  cin >> a; 
  cout << "Enter denominator: "; 
  cin >> b; 
 
  if(b) 
     cout << "Result: " << a / b << "\n"; 
  else 
     cout << "Cannot divide by zero.\n"; 
 
  return 0; 
}