C++ Tutorial/Operators statements/if statement

Материал из C\C++ эксперт
Версия от 10:30, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A block of code in if statement

#include <iostream> 
using namespace std; 
 
int main() { 
  double result, n, d; 
 
  cout << "Enter value: "; 
  cin >> n; 
 
  cout << "Enter divisor: "; 
  cin >> d; 
 
  if(d != 0) { 
    cout << "d does not equal zero so division is OK" << "\n"; 
    result = n / d; 
    cout << n << " / " << d << " is " << result; 
  } 
 
  return 0; 
}
Enter value: 12
Enter divisor: 21
d does not equal zero so division is OK
12 / 21 is 0.571429"

A nested if tatement

#include <iostream>
 
 int main()
 {
     int firstNumber =2;
     int secondNumber = 3;
 
     if (firstNumber >= secondNumber)
     {
         if ( (firstNumber  secondNumber) == 0) // evenly divisible?
         {
             if (firstNumber == secondNumber)
                 std::cout << "They are the same!\n";
             else
                 std::cout << "They are evenly divisible!\n";
         }
         else
             std::cout << "They are not evenly divisible!\n";
     }
     else
         std::cout << "Hey! The second one is larger!\n";
     return 0;
 }
Hey! The second one is larger!

An if-else-if ladder.

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int x; 
 
  for(x=0; x<6; x++) { 
    if(x==1) cout << "x is one\n"; 
    else if(x==2) cout << "x is two\n"; 
    else if(x==3) cout << "x is three\n"; 
    else if(x==4) cout << "x is four\n"; 
    else cout << "x is not between 1 and 4\n"; 
  } 
 
  return 0; 
}
x is not between 1 and 4
x is one
x is two
x is three
x is four
x is not between 1 and 4

If else statement with block code

#include <iostream> 
#include <cstdlib> 
 
using namespace std; 
 
int main() 
{ 
  int magic;  // magic number 
  int guess;  // user"s 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; 
}
Enter your guess: 3
...Sorry, you"re wrong. Your guess is too low.

If statement with else

#include <iostream> 
#include <cstdlib> 
using namespace std; 
 
int main() 
{ 
  int magic;  // magic number 
  int guess;  // user"s guess 
 
  magic = rand(); // get a random number 
   
  cout << "Enter your guess: "; 
  cin >> guess; 
 
  if(guess == magic) 
      cout << "Right"; 
  else 
      cout << "...Sorry, you"re wrong."; 
 
  return 0; 
}
Enter your guess: 3
...Sorry, you"re wrong."

if statement with variable logic operators

#include <iostream> 
using namespace std; 
  
int main() {  
  int a, b, c;  
  
  a = 2;  
  b = 3;  
  
  if(a < b) 
     cout << "a is less than b\n"; 
 
  if(a == b) 
     cout << "you won"t see this\n";  
 
  cout << "\n"; 
 
  c = a - b;
 
  cout << "c contains -1\n"; 
  if(c >= 0) 
     cout << "c is non-negative\n"; 
  if(c < 0) 
     cout << "c is negative\n"; 
 
  cout << "\n"; 
 
  c = b - a; // c now contains 1 
  cout << "c contains 1\n"; 
  if(c >= 0) 
     cout << "c is non-negative\n"; 
  if(c < 0) 
     cout << "c is negative\n"; 
 
  return 0; 
}
a is less than b
c contains -1
c is negative
c contains 1
c is non-negative

if with else

#include <iostream>
 
 int main()
 {
     int firstNumber, secondNumber;
     std::cout << "Please enter a big number: ";
     std::cin >> firstNumber;
     std::cout << "\nPlease enter a smaller number: ";
     std::cin >> secondNumber;
     if (firstNumber > secondNumber)
         std::cout << "\nThanks!\n";
     else
         std::cout << "\nOops. The second is bigger!";
     return 0;
 }
Please enter a big number: 123
Please enter a smaller number: 12
Thanks!

Proper use of braces with an if statement

#include <iostream>
 
 int main()
 {
     int x = 12;
  
     if (x > 10)
     {
         if (x > 100)
             std::cout << "More than 100, Thanks!\n";
     }
     else        // now works as intended!
         std::cout << "Less than 10, Thanks!\n";
     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; 
}
Enter numerator: 2
Enter denominator: 4
Result: 0