Use c language if statement

 if statements are generally three basic forms, other forms of expansion, it is also based on the following three forms the basis of the individual learn and use it.

  1, if ...... form

  The general format: if (expression) statement;

  Semantics: If the expression is true, the statement after the execution, or do not execute the statement. Statement may be a single statement, may be used together include braces {} of a compound statement. Examples are as follows:

#include <stdio.h>

int main()

{
   int  a,b,max;
   printf ( "\n input two numbers: " );
   scanf ( "%d%d" ,&a,&b);
  max=a;
   if  (max<b) max=b;
   printf ( "max=%d\n" ,max);
   return  0;
}

2、if……else形式

  The general format:

  if (expression)

  Statement 1;

  else

  Statement 2;

 

  Semantics: If the expression evaluates to true, the statements 1, 2 or execute a statement. Sentence 1 and sentence 2 may be a compound statement.

  Examples are as follows:

#include <stdio.h>

int  main()
{
  int  a, b;
  printf ( "input two numbers: " );
  scanf ( "%d%d" ,&a,&b);
  if (a>b)
  printf ( "max=%d\n" ,a);
  else
  printf ( "max=%d\n" ,b);
  return  0;
}

 

  3、if……else……if形式

  The general form is:

  if (Expression 1)

  Statement 1;

  else if (Expression 2)

  Statement 2;

  else if (Expression 3)

  Statement 3;

  …

  else if (expression m)

  Statement m;

  else

  Statement n;

  Semantics: sequentially determining the value of the expression, when there is a true value, the corresponding statement is executed. Then skip the entire sentence than if the program is continued. If all expressions are false, the statement is executed n. Then continue with follow-up procedures. Examples are as follows:

#include <stdio.h>

int  main()
{
   char  c;
   printf ( "input a character: " );
  c= getchar ();
   if (c<32)
   printf ( "This is a control character\n" );
   else  if (c>= '0' &&c<= '9' )
   printf ( "This is a digit\n" );
   else  if (c>= 'A' &&c<= 'Z' )
   printf ( "This is a capital letter\n" );
   else  if (c>= 'a' &&c<= 'z' )
   printf ( "This is a small letter\n" );
   else
   printf ( "This is an other character\n" );
   return  0;
}

4, extended form

  if conditions for control statements of many forms. For example if nested, if ...... if a similar form, but can not do without the above-described three basic forms. Individuals can learn and use.

Guess you like

Origin www.cnblogs.com/zhangdemingQ/p/12115966.html