C language if the branch structure

A relational operators

Before speaking if conditional branch structure, to understand the relational operators (Relational
Operators), for determining the condition to decide the program flow.

relationship Mathematical Representation C language representation
Less than < <
Less than or equal <=
more than the > >
greater or equal to >=
equal = ==
not equal to !=

The basic data types in C are char, int, double, we do believe that only char and int applicable to the relational operators, and discuss the future double strings (character arrays) relational operators.

Attention:

1) "=" is assigned, "==" is determined whether the two numbers are equal, it can not be mixed.

2) C language is not "between", "middle", "within", or relational operators "within a range", for example, between the ages of 25-30 years, can be expressed as: the age of 25 years or greater and younger than or equal to 30 years of age .

Second, the condition determination

In our lives, things are not all in sequence, you need to make judgments and choices based on objective conditions, such as Super Girl draft, limited the age of 16 to participate, under the age of 16 are unable to participate. This time the program will need to make a judgment as to whether applicants aged over 16 years, and gives tips.

In the C language, if and else condition is determined keywords, look at the following code.

Example (book20.c)

/*
  *  程序名:book20.c,此程序演示条件分支控制语句if else的使用。
  *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int    age=0;   // 定义一个整数变量age,存放用户输入的年龄

  printf("请输入您的年龄:");    // 提示用户输入
  scanf("%d",&age);              // 接受从键盘输入的年龄数据

  if (age >= 16)
  {
    // 如果用户已满18岁,执行这段代码
    printf("亲,您已成年,可以报名超女选秀,祝您好运!\n");
  }
  else
  {
    // 如果用户小于16岁,执行这段代码
    printf("这位童鞋,不好意思哦,等您16岁之后再来吧!\n");
  }
}

running result

Here Insert Picture Description

In this code, age> = 18 is necessary to determine conditions,> = means "not less than", equivalent to the mathematics ≥.

if and else is a C language keywords, if that, "if", else it is, "otherwise", is used to determine the conditions and execute different statements based on the result. To sum up, if else structure is:

  if (判断条件)
  {
    语句块1
  }
  else
  {
    语句块2
  }

Meaning that, if the judgment condition is satisfied, then execute a block of statements 1, 2 or execute a block of statements. Its execution can be expressed as the following figure:

Here Insert Picture Description
The so-called block of statements (Statement Block), {} is a set of one or more statements surrounded. If only one statement statement block, {} may be omitted, for example:

 if (age >= 16)
    printf("亲,您已成年,可以报名超女选秀,祝您好运!\n");
  else
    printf("这位童鞋,不好意思哦,等您16岁之后再来吧!\n");

or

  if (age >= 16)  printf("亲,您已成年,可以报名超女选秀,祝您好运!\n");
  else  printf("这位童鞋,不好意思哦,等您16岁之后再来吧!\n");

Because if else statements can execute different code depending on the circumstances, it is also called branched structure or a structure selected, the above code, there are two branches.

Be careful not to add a semicolon after the if (judgment condition), the following is wrong, easy for beginners make this mistake.

  if (age >= 16);

If so written, it represents the condition is true, execute an empty statement.

Third, only if statement

Sometimes, when we need to meet certain conditions in some operations without any operation is not performed when the condition is satisfied, at this time we can only use an if statement. In other words, if else does not have to occur simultaneously.

Use a separate form if statement:

if (判断条件)
  {
    语句块
  }

Meaning that, if the judgment condition is satisfied on the implementation of a block of statements, otherwise skip. Its execution can be expressed as the following figure:

Here Insert Picture Description

Such as Super Girl draft, was eliminated in the Super Girl, activity organizers to be eliminated Super Girl payment of travel expenses, the standard is 2,000 yuan / person, however, the program's sponsors have a rich second, decided to give color values ​​beautiful super girl multiple 500 yuan.

Example (book21.c)

/*
  *  程序名:book21.c,此程序演示条件分支控制语句只有if没有else的情况。
  *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int    yz=0;   // 定义一个整数变量存放超女的颜值:1-漂亮;2-一般
  int    money=2000;  // 发放路费的基本金额

  printf("请问超女的颜值(1-漂亮;2-一般):");
  scanf("%d",&yz);              // 接受用户从键盘输入的数据

  if (yz == 1)
  {
    // 如果超女颜值漂亮,多给500元的路费
    money=money+500;
  }

  printf("此超女发放金额为%d元。\n",money);
}

running result

Here Insert Picture Description

Fourth, more if else statements

if else statements can also be used a plurality of simultaneously form a plurality of branches, the following form:

 if (条件表达式一)
  {
    语句块一
  }
  else if (条件表达式二)
  {
    语句块二
  }
  else if (条件表达式三)
  {
    语句块三
  }
  else if (条件表达式n)
  {
    语句块n
  }
  else
  {
    全部的条件都不符时执行此语句块x
  }

Means that the determination condition is detected sequentially from top to bottom, when a determination condition is satisfied, executing the corresponding block of statements, and then out beyond the entire statement if else continue with other codes. If all conditions are not judged true, then the statement is executed block x.

In other words, once the judge can set up the conditions encountered, it is no longer execute other statements block, so in the end there can be only one statement block is executed.

For example, if else statement to determine a plurality of weeks input digital representation.

Example (book22.c)

/*
  *  程序名:book22.c,此程序演示条件分支控制语句有多个 if else的情况。
  *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int    day=0;   // 定义一个整数变量用于接受键盘输入的数字

  printf("请输入0-6之间的数字:");    // 提示用户输入
  scanf("%d",&day);              // 接受用户从键盘输入的数据
  if (day == 0)
    printf("星期天\n");
  else if (day == 1)
    printf("星期一\n");
  else if (day == 2)
    printf("星期二\n");
  else if (day == 3)
    printf("星期三\n");
  else if (day == 4)
    printf("星期四\n");
  else if (day == 5)
    printf("星期五\n");
  else if (day == 6)
    printf("星期六\n");
  else
    printf("输入错误。\n");
}

running result

Here Insert Picture Description

We recommend caution else if this program structure, program structure because it is easy to get confused people, to solve this kind of demand there are other better ways, such as in later chapters to learn more appropriate than switch else if.

Fifth, nested if statements

nested if statements may be, for example:

Example (book23.c)

/*
  *  程序名:book23.c,此程序演示条件分支控制语句有多个if嵌套的情况。
  *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int a,b;   // 定义两个整数变量,用于比较大小

  printf("请输入两个数字:");    // 提示用户输入
  scanf("%d %d",&a,&b);              // 接受用户从键盘输入的数据

  if (a == b)      // 两个数相等的情况
  {
    printf("a(%d) 等于 (%d)\n",a,b);
  }
  else             // 两个数不相等的情况
  {
    if (a > b)     // a大于b的情况
    {
      printf("a(%d) 大于 b(%d)\n",a,b);
    }
    else           // 剩下的就是a小于b的情况了
    {
      printf("a(%d) 小于 b(%d)\n",a,b);
    }
  }
}

running result

Here Insert Picture Description

Sixth, to maintain a good programming practice

When nested if statements, pay attention to the issue of if and else pairing, C language provisions, else is always paired with the nearest if front of it. You must maintain a good programming practice, spaces, indents, alignment, {} pairs such as writing, poor coding practices will engage in their own halo.

Seven, logical operators

In daily life, to make a decision, you need to determine the condition is often more than one, you need to determine a number of conditions, such as Super Girl talent, the girls involved in the draft have the following basic data:

Age: 16-50 years old values.
Height: values 150cm-190cm.
Figure: hot 1-; 2- general; 3- airport.
Yan Found: 1- beautiful; 2- ships; 3- crooked melon split date.

Goddess criteria are: 1) the age of 25-30 years; 2) height 165cm-175cm; 3) hot body; 4) beautiful face value.

Brain logic is this: the age of 25-30 years old and height 165cm-175cm and hot body and face value beautiful .

Reduce point demand it, color values can be selected beautiful and generally , if it is a general , full capacity is not a problem.

So a logic change to: Age 25-30 years and height. 1 65cm-175cm and hot body and ( color value beautiful or color value in general ).

C program to express something like:

  if ( (年龄 >= 25) 并且 (年龄 <= 30) )
  {
    if ( (身高 >= 165) 并且 (身高 <= 175) )
    {
      if (身材 == 火辣)
      {
        if ( (颜值 == 漂亮) 或者 (颜值 == 一般) )
        {
          printf("超女选秀合格,送往后宫。\n");
        }
      }
    }
  }

With the C language, "&&" indicates "and" with "||" indicates "or", the official language is the logical operator.

Code changes as follows:

 if ( (年龄 >= 25) && (年龄 <= 30) )
  {
    if ( (身高 >= 165) && (身高 <= 175) )
    {
      if (身材 == 火辣)
      {
        if ( (颜值 == 漂亮) || (颜值 == 一般) )
        {
          printf("超女选秀合格,送往后宫。\n");
        }
      }
    }
  }

Example (book25.c)

/*
  *  程序名:book25.c,此程序演示条件分支控制语句if与逻辑运算符。
  *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int  age=0;        // 年龄
  int  height=0;     // 身高,单位:厘米cm
  int  sc=0;         // 身材,1-火辣;2-普通;3-飞机场。
  int  yz=0;         // 颜值,1-漂亮;2-一般;3-歪瓜裂枣。

  printf("请输入年龄、身高、身材(1-火辣;2-普通;3-飞机场):");
  scanf("%d %d %d",&age,&height,&sc);
  printf("请输入颜值(1-漂亮;2-一般;3-歪瓜裂枣):");
  scanf("%d",&yz);

  if ( (age >= 25) && (age <= 30) )    // 年龄在25-30之间
  {
    if ( (height >= 165) && (height <= 175) )  // 身高在165-175之间
    {
      if (sc == 1)    // 身材,1-火辣
      {
        if ( (yz == 1) || (yz == 2) )   // 颜值,1-漂亮或者2-一般
        {
          printf("超女选秀合格,送往后宫。\n"); return 0;
        }
      }
    }
  }
  printf("超女选秀不合格,发放五两银子后送回家。\n");
}

running result
Here Insert Picture Description

Writing eight logical expression

In book25.c, using a nested approach if, in fact, may be employed if a complex logical expressions of all at once is determined, using the brackets are aligned, spaces, line help more clearly express complex logical expression formula.

Example (book26.c)

/*
  *  程序名:book26.c,此程序演示条件分支控制语句if与逻辑运算符。
  *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int  age=0;        // 年龄
  int  height=0;     // 身高,单位:厘米cm
  int  sc=0;         // 身材,1-火辣;2-普通;3-飞机场。
  int  yz=0;         // 颜值,1-漂亮;2-一般;3-歪瓜裂枣。

  printf("请输入年龄、身高、身材(1-火辣;2-普通;3-飞机场):");
  scanf("%d %d %d",&age,&height,&sc);
  printf("请输入颜值(1-漂亮;2-一般;3-歪瓜裂枣):");
  scanf("%d",&yz);}

  if ( ( (age >= 25) && (age <= 30)         ) &&     // 年龄在25-30之间
       ( (height >= 165) && (height <= 175) ) &&     // 身高在165-175之间
       ( (sc == 1)                          ) &&     // 身材,1-火辣
       ( (yz == 1) || (yz == 2)             ) )      // 颜值,1-漂亮或者2-一般
  {
    printf("超女选秀合格,送往后宫。\n");
  }
  else
  {
    printf("超女选秀不合格,发放五两银子后送回家。\n");
  }
}

book26.c if using a logical expression on the realization of all the judgments, and effects book25.c same, if it can not understand, on the use of the expression book25.c in it, do not worry, take your time.

Nine, have to say the non-logic

There are three logical operators, in the above sections, we talked about && and ||, there is a! Did not say, in my opinion, this operator should not exist, it is easy to confuse people's thinking.

We can take a look at how it is, you can write code to test its use, then forget it.

! Is wrong, take the opposite meaning.

E.g:

if  (age==25)         // 判断年龄是25岁

Equal

if  (!(age!=25))        // 判断年龄不是25岁再取反

This is fed propped.

X. ternary operator

Ternary operator would have been content operator chapters, I believe that is described in this section is more appropriate.

Ternary operator also called ternary operator, the operator and a question mark symbol is followed by a colon.

grammar:

 表达式1 ? 表达式2 : 表达式3;

Semantics:

1 expression is executed first, if the result of the expression 1. If true, then the implementation of the expression 2, and the overall result of the expression is the result of the expression 2; 1 if the result of the expression if false, the expression is executed 3, the result is a result of expression of the expression 3.

int a,b,c;
a=7;
b=6;
c=(a>b)?a:b;

Equivalent to

if (a>b) c=a;
else c=b;

I do not recommend using ternary operator, overly complex three head operation difficult to understand.

Such determination is not a leap year, it returns 1, instead of returning 0.

year=(year%100==0)?(year%400==0?1:0):(year%4==0?1:0)

Halo bar.

XI homework

1) noted the following error code.

int a,b;
a=30;
b=20;
if (a > b);
{
  printf("a 大于 b\n");
}
if (a=b)
{
  printf("a 等于 b\n");
}

2) value written expression test program

  int a,b,c;

  a=30;
  b=20;

  c=(a > b);
  printf("c=%d\n",c);

  c=(a < b);
  printf("c=%d\n",c);
  printf("c=%d\n",(a < b));

  c=(a=50);
  printf("c=%d\n",c);

3) The following expression is what does this mean?

if (0) What does this mean?

if (1) What does this mean?

if (20) What does it mean?

4) The following expression is what does this mean?

  35;    // 是什么意思?

5) ultra female talent in the end, after the input data in the interface Super Girl, elected according to the following requirements concubine, ladies and fatigues.

Concubine: (1) the age of 18-25 years of age; (2) Height 165-178cm; (3) hot body; (4) the value of beautiful color.

Ladies: (1) age 18-30 years; (2) height 160-165cm; (3), or ordinary hot body; (4) the value of the general color.

Mother: (1) the age of 35-40 years of age; (2) Height 155-165cm; (3) the general body or the airport; (4) color values ​​in general.

XII copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Guess you like

Origin www.cnblogs.com/wucongzhou/p/12498996.html