C/Language Basics/If

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

A simple example of the if statement

<source lang="cpp">

  1. 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);

}


      </source>


Block if else statement

<source lang="cpp">

  1. 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;

}

      </source>


if command

<source lang="cpp">

  1. 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);

}


      </source>


If else

<source lang="cpp">

  1. include <stdio.h>
  2. 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;

}

      </source>


If else if and else

<source lang="cpp"> /* Magic number program #4. */

  1. include <stdio.h>
  2. 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;

}


      </source>


If else statement inside a for loop

<source lang="cpp">

  1. 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;

}

      </source>


IF statement 3

<source lang="cpp">

  1. 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);

}

      </source>


If statement for different choice

<source lang="cpp">

  1. 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;

}

      </source>


If statement inside for loop

<source lang="cpp">

  1. include <stdio.h>
  2. 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;

}

      </source>


More if else

<source lang="cpp">

  1. 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;

}

      </source>


More statement in if else

<source lang="cpp"> /* Magic number program #3. */

  1. include <stdio.h>
  2. 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;

}


      </source>


Move more statements into if and else

<source lang="cpp">

  1. 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;

}

      </source>


Use calculation in if statement

<source lang="cpp">

  1. 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;

}

      </source>


Use if else

<source lang="cpp">

  1. 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;

}

      </source>


Use int as if condition

<source lang="cpp">

  1. 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;

}


      </source>


Use result as the if condition

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 int answer;
 printf("What is 110 + 14? ");
 scanf("%d", &answer);
 if(answer == 110+14) 
     printf("Right!");
 return 0;

}

      </source>


Use statement inside if else

<source lang="cpp">

  1. 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;

}

      </source>


Use two if statements

<source lang="cpp">

  1. 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;

}

      </source>


Use user input as the if condition

<source lang="cpp">

  1. 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;

}

      </source>


Using ifs to decide on a discount

<source lang="cpp">

  1. 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); 

}


      </source>