C Tutorial/Statement/If statement — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:21, 25 мая 2010

Checking for a Range of Values

#include <stdio.h>
main()
{
  int iResponse = 0;
  printf("Enter a number from 1 to 10: ");
  scanf("%d", &iResponse);
  if ( iResponse < 1 || iResponse > 10 )
    printf("\nNumber not in range\n");
  else
    printf("\nThank you\n");
}
Enter a number from 1 to 10: 2
      
      Thank you

if Comparisons and Their Opposites

if Comparison else Statement Executed By This Condition < >= == != > <= <= > >= < != ==

if statement

To conditionally execute statements, you can use the if or the if...else statement.

The general form of the if statement is


if(expr) {
    s1 ;
    s2 ;
    ....
  }

if statement: compare the value from user input

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

if statement with two else statements

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char num[2];
    int number;
 
    printf("Enter a number from 0 to 9:");
    gets(num);
    number=atoi(num);
 
    if(number<5)
    {
        printf("That number is less than 5!\n");
    }
    else if(number==5)
    {
        printf("You typed in 5!\n");
    }
    else
    {
        printf("That number is more than 5!\n");
    }
 
    return(0);
}
Enter a number from 0 to 9:2
      That number is less than 5!

If with else statement

The general format for an if-else statement is


if (condition)
   simple or compound statement  
else
   simple or compound statement.

Operators Used in if Comparisons

Comparison Meaning or Pronunciation "True" Example < Less than 1 < 5, 8 < 9 == Equal to 7 == 7, 1 == 1 > Greater than 7 > 5, 10 > 0 <= Less than or equal to 4 <= 5, 8 <= 8 >= Greater than or equal to 6 >= 5, 2 >= 2 != Not equal to 1 != 0, 1 != 3.99

The if-else-if statement

The general format for the if-else if statement is:


if (condition 1)
   simple or compound statement  
else if (condition 2)
   simple or compound statement  
else if ( condition 3)
   simple or compound statement  
   .....
else if ( conditon n )
   simple or compound statement

The if statement: a compound statement

If a compound statement is used, it must be enclosed in opening and closing braces.


#include<stdio.h>
main()
{
   int i=5, j =1;
   
    if (i > j){
        i = i + 1;
        printf("%d",i);
    }   
    else
        j = j +1;
}
6

To execute only one statement, opening and closing braces are not required

#include <stdio.h>
main(){
  int i = 5;
  if(i > 0)
     printf(" i > 0. \n");
     
}
i > 0.

Use compound conditions to check for upper and lowercase letters

#include <stdio.h>
main(){
    char cResponse = "\0";
    printf("Enter the letter A: ");
    scanf("%c", &cResponse);
    if ( cResponse== "A" || cResponse == "a" )
       printf("\nCorrect response\n");
    else
       printf("\nIncorrect response\n");
}
Enter the letter A: 1
      
      Incorrect response

Use isdigit Function in if statement

#include <stdio.h>
#include <ctype.h>
main()
{
  char cResponse = "\0";
  printf("\nPlease enter a letter: ");
  scanf("%c", &cResponse);
  if ( isdigit(cResponse)== 0 )
    printf("\nThank you\n");
  else
    printf("\nYou did not enter a letter\n");
}
Please enter a letter: 1
      
      You did not enter a letter

Using nested ifs

#include <stdio.h>
#include <limits.h>
int main(void)
{
  long test = 0L;              

  if( test % 2L == 0L) {
    printf("The number %ld is even", test);
    if ( (test/2L) % 2L == 0L)
    {
      printf("\nHalf of %ld is also even", test);
      printf("\nThat"s interesting isn"t it?\n");
    }
  }
  else
    printf("The number %ld is odd\n", test);
  return 0;
}
The number 0 is even
     Half of 0 is also even
     That"s interesting isn"t it?