C/Language Basics/If

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

A simple example of the if statement

#include <stdio.h>
void main()
{
   int number = 0;
   printf("\nEnter an integer between 1 and 10: ");
   scanf("%d",&number);
   if (number > 7)
     printf("You entered %d which is greater than 7\n", number);
   if (number < 3)
     printf("You entered %d which is less than 3\n", number);
}


Block if else statement

  
#include <stdio.h>
int main(void)
{
  int answer, i;
  int again;
  for(i = 1; i < 11; i++) {
    printf("What is %d + %d? ", i, i);
    scanf("%d", &answer);
    if(answer == i + i) 
        printf("Right!\n");
    else {
      printf("Wrong\n");
      printf("Try again.\n ");
      printf("\nWhat is %d + %d? ", i, i);
      scanf("%d", &answer);
      /* nested if */
      if(answer == i + i) 
          printf("Right!\n");
      else
          printf("Wrong, the answer = %d\n", i + i);
    }
  }
  return 0;
}


if command

#include <stdio.h>
int main() {
    float cost, tax, luxury, total;
    luxury = 0.0;
    
    printf("Enter the cost: ");
    scanf("%f", &cost);
    
    tax = cost * 0.06;
    if( cost > 40000.0)
        luxury = cost * 0.005;
    total = cost + tax + luxury;
    
    printf("the total cost is %0.2f", total);
}


If else

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int magic; /* magic number */
  int guess; /* user"s guess */
  magic = rand(); /* generate the magic number */
  printf("Guess the magic number: ");
  scanf("%d", &guess);
  if(guess == magic) printf("** Right **");
  else printf("Wrong");
  return 0;
}


If else if and else

/* Magic number program #4. */
#include <stdio.h>
#include <stdlib.h>
int main(void) 
{
  int magic; /* magic number */
  int guess; /* user"s guess */
  magic = rand(); /* generate the magic number */
  printf("Guess the magic number: ");
  scanf("%d", &guess);
  if(guess == magic) {
    printf("** Right ** ");
    printf("%d is the magic number", magic);
  }
  else if(guess > magic)
    printf("Wrong, too high");
  else printf("Wrong, too low");
  return 0;
}


If else statement inside a for loop

#include <stdio.h>
int main(void)
{
  int answer, i;
  int right, wrong;
  right = 0;
  wrong = 0;
  for( i = 1; i < 11; i = i + 1) {
    printf("What is %d + %d? ", i, i);
    scanf("%d", &answer);
    if(answer == i + i) {
      printf("Right!  ");
      right++;
    }
    else {
      printf("Sorry, you"re wrong. ");
      printf("The answer is %d.  ", i + i);
      wrong++;
    }
  }
  printf("You got %d right and %d wrong.", right, wrong);
  return 0;
}


IF statement 3

  
#include <stdio.h>
main() {
     int i,j,big;  //variable
     scanf("%d%d",&i,&j); 
     
     big = i;
     if(big < j) {
         big = j;
     }
     
     printf("bigger one is %d \n",big);
     if(i < j) {
      big = j;
   } else {
      big = i;
   }
     printf("bigger one (using else) is %d \n",big);
}


If statement for different choice

  
#include <stdio.h>
int main(void)
{
  float num;
  int choice;
  printf("1: Change Feet to Meters, 2: Change Meters to Feet. ");
  printf("Choice: ");
  scanf("%d", &choice);
  if(choice == 1) {
    printf("Enter number of feet: ");
    scanf("%f", &num);
    printf("Meters: %f", num / 3.28);
  } else {
    printf("Enter number of meters: ");
    scanf("%f", &num);
    printf("Feet: %f", num * 3.28);
  }
  return 0;
}


If statement inside for loop

  
#include <stdio.h>
#include <conio.h>
int main(void)
{
  int i;
  char ch;
  /* display all numbers which are multiples of 6 */
  for(i = 5; i < 10000; i++) {
    if(!( i % 6 )) {
      printf("%d, more number? (Y/N)", i);
      
      ch = getche();
      
      if(ch == "N") 
           break; /* stop the loop */
      
      printf("\n");
    }
  }
  return 0;
}


More if else

  
#include <stdio.h>
int main(void)
{
  int a, b;
  char ch;
  printf("Choice:\n");
  printf("(A) Add, (S) Subtract, (M) Multiply, or (D) Divide?\n");
  ch = getchar();
  printf("\n");
  printf("Enter a: ");
  scanf("%d", &a);
  printf("Enter b: ");
  scanf("%d", &b);
  if(ch=="A") printf("%d", a+b);
  else if(ch=="S") printf("%d", a-b);
  else if(ch=="M") printf("%d", a*b);
  else if(ch=="D" && b!=0) printf("%d", a/b);
  return 0;
}


More statement in if else

/* Magic number program #3. */ 
#include <stdio.h>
#include <stdlib.h> 
int main(void) 
{
  int magic; /* magic number */
  int guess; /* user"s guess */
  magic = rand(); /* get a random number */
  printf("Guess the magic number: ");
  scanf("%d", &guess); 
  if (guess == magic) {
    printf("** Right **");
    printf(" %d is the magic number\n", magic);
  }
  else {
    printf("Wrong, ");
    if(guess > magic) printf("too high\n"); /* nested if */
    else printf("too low\n");
  }
  return 0;
}


Move more statements into if and else

#include <stdio.h>
int main(void)
{
  int a, b, op;
  printf("Enter 0 to add, 1 to subtract: ");
  scanf("%d", &op);
  if(op == 0) { /* add */
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("%d", a+b);
  }
  else { /* subtract */
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("%d", a-b);
  }
  return 0;
}


Use calculation in if statement

  
#include <stdio.h>
int main(void)
{
  int answer;
  printf("What is 110 + 14? ");
  scanf("%d", &answer);
  if(answer == 110+14) 
      printf("Right!");
  else {
    printf("Wrong. ");
    printf("The answer is 124.");
  }
  return 0;
}


Use if else

#include <stdio.h>
int main(void)
{
  int a, b, op;
  printf("Enter first number: ");
  scanf("%d", &a);
  printf("Enter second number: ");
  scanf("%d", &b);
  printf("Enter 0 to add, 1 to multiply: ");
  scanf("%d", &op);
  if(op == 0) 
      printf("%d", a + b );
  else 
      printf("%d", a * b );
  return 0;
}


Use int as if condition

#include <stdio.h>
int main(void)
{
  int num;
  printf("Enter an integer: ");
  scanf("%d", &num);
  if(num < 0) 
      printf("Number is negative.");
  else 
      printf("Number is non-negative.");
  return 0;
}


Use result as the if condition

  
#include <stdio.h>
int main(void)
{
  int answer;
  printf("What is 110 + 14? ");
  scanf("%d", &answer);
  if(answer == 110+14) 
      printf("Right!");
  return 0;
}


Use statement inside if else

#include <stdio.h>
int main(void)
{
  int i;
  printf("Enter a number: ");
  scanf("%d", &i);
  
  if( ( i % 2) == 0) 
      printf("Even");
  else 
      printf("Odd");
  return 0;
}


Use two if statements

  
#include <stdio.h>
int main(void)
{
  int num;
  printf("Enter an integer: ");
  scanf("%d", &num);
  if(num < 0) 
      printf("num is negative.");
  
  if(num > -1) 
      printf("num is non-negative.");
  return 0;
}


Use user input as the if condition

  
#include <stdio.h>
int main(void)
{
  float num;
  int choice;
  printf(" Meter or Feet: ");
  scanf("%f", &num);
  printf("1: Feet to Meters, 2: Meters to Feet. ");
  printf("Enter choice: ");
  scanf("%d", &choice);
  if(choice == 1) 
      printf("%f", num / 3.28);
  if(choice == 2) 
      printf("%f", num * 3.28);
  return 0;
}


Using ifs to decide on a discount

#include <stdio.h>
void main() {
  const double price = 3.50;                /* price*/
  
  int quantity = 0;
  
  printf("Enter the number that you want to buy:");     /* Prompt message */
  
  scanf(" %d", &quantity);                             /* Read the input */
   /* Test for order quantity qualifying for a discount */
   if( quantity>20)                                     /* 5% discount */                
   
     printf("The price for %d is $%.2f\n", quantity, quantity * price * 0.95);
      
   else
                                                    /* No discount */
     printf("The price for %d is $%.2f\n", quantity, quantity * price); 
}