Explain the use of conditional judgment statements if and switch in C language _c language

Source: Internet
Author: User
Tags case statement

If statement

The IF statement can be used to form a branch structure that is judged according to the conditions given to determine which branch segment to execute.

There are three basic forms of if statements in C language

The first form of:

if (conditional expression) 
{
  statement 1
}

if (conditional expression) 
{
  statement 1;
}

This form of operation is: when the conditional expression is true, execute statement 1, otherwise, skip statement 1 directly, execute the following statement.

Example 1:

BOOL result = YES;
if (result)
{
  printf (' result is true\n ');
}

BOOL result = YES;
if (result)
{
  printf (' result is true\n ');
}

The output results are:

Result is True

If you change the value of result to No, then nothing is exported.

Example 2:

int a = 5;
int b = 6;
if (a >= b)
{
  printf ("A is greater than b\n");

int a = 5;
int b = 6;
if (a >= b)
{
  printf ("A is greater than b\n");


The output results are:

It's not going to output anything.

If you change this:

if (a <= b)
{
  printf ("A is less than b\n");

if (a <= b)
{
  printf ("A is less than b\n");


The output result is: A is less than B.

Second form:

if (conditional expression)
{statement
  1;}
Else 
{
  Statement 2;
}

if (conditional expression)
{
  statement 1;
}
Else 
{
  statement 2;
}

The execution order of this structure is: when the conditional expression is true, execute statement 1, or EXECUTE statement 2.

Example 1:

BOOL result= YES;
if (result)
{
  printf ("result is true \ n");
}
else 
{
  printf ("result is false \ n");
}

BOOL result= YES;
if (result)
{
  printf ("result is true \ n");
}
else 
{
  printf ("result is false \ n");
}

The two output statements here will never be output at the same time.

The third form of:

if (conditional expression 1)
{
  statement 1;
}
else if (conditional expression 2)
{
  statement 2;
}
Else 
{
  statement 3;
}

if (conditional expression 1)
{
  statement 1;
}
else if (conditional expression 2)
{
  statement 2;
}
Else 
{
  Statement 3;
}

The execution order of this structure is: when the conditional expression 1 is set up, execute statement 1, if it is not tenable, see if the conditional expression 2 is set up, if the conditional expression 2 is established, then the statement 2 is executed, and if the conditional expression 2 is not established this will execute statement 3.

Example 1:

int age =;
if (age <)
{
  printf ("You are a child \ n")
;
else if (age >=)
{
  printf ("You are an old man \ n");
}
else 
{
  printf ("You are a young man, lad!") \ n ");
}

int age =;
if (age <)
{
  printf ("You are a child \ n")
;
else if (age >=)
{
  printf ("You are an old man \ n");
}
else 
{
  printf ("You are a young man, lad!") \ n ");
}

The output results are:

You are a young man, lad!

Switch statement
We talked about the if statement of the branch statement in C, and today we're going to learn another branch statement: a switch statement.

If the three forms of the IF statement, you certainly remember, you recall if the last form, if we have a lot of branches how to write? Is it like this?

if (conditional expression 1)
{
  statement 1
}
else if (conditional expression 2)
{
  statement 2;
}
else if (conditional expression 3)
{
  Statement 3;
}
else if (conditional expression 4)
{
  statement 4
}
else if (conditional expression 5)
{
  statement 5;
}
Else
{
  Statement 6;
}

If this is the case, then the design of C language is too smart, so the C language also has another branch of the statement, is the switch statement. The structure is as follows:

switch (cosmetic expression)
{case
  value 1:
  {
    statement 1;
    break;
  Case Value 2:
  {
    Statement 2;
    break;
  Case Value 3:
  {
    Statement 3;
    break;
  ...
  Defaults:
    default statement;
    break;


switch (cosmetic expression)
{case
  value 1:
  {
    statement 1;
    break;
  Case Value 2:
  {
    Statement 2;
    break;
  Case Value 3:
  {
    Statement 3;
    break;
  ...
  Defaults:
    default statement;
    break;

Description

1, first of all pay attention to switch syntax structure, can not have errors

2. The result value of the shaping expression is equal to the value 1, executes statement 1, if equal to 2, executes statement 2, and if the value of the expression is not the same as any case, then the default statement, either without the default statement, or the entire switch statement, is executed.

3, pay attention to each statement after the break, if no break,break will jump out of the current case statement, thereby jumping out of the entire switch statement.

4, the label behind the case can only be cosmetic constants or shape constant expressions, can not use variables as a case label.

Example:

int day = 0;
printf ("Please enter a 1-7 number \ n");
scanf ("%d", &day);
    Switch (day) {Case 1: {printf ("Today is Monday \ n");
  Break
    Case 2: {printf ("Today is Tuesday \ n");
  Break
    Case 3: {printf ("Today is Wednesday \ n");
  Break
    Case 4: {printf ("Today is Thursday \ n");
  Break
    Case 5: {printf ("Today is Friday \ n");
  Break
    Case 6: {printf ("Today is Saturday \ n");
  Break
    Case 7: {printf ("Today is Sunday \ n");
  Break
    } default: {printf ("Input error \ n");
  Break
int day = 0;
printf ("Please enter a 1-7 number \ n");
scanf ("%d", &day);
    Switch (day) {Case 1: {printf ("Today is Monday \ n");
  Break
    Case 2: {printf ("Today is Tuesday \ n");
  Break
    Case 3: {printf ("Today is Wednesday \ n");
  Break
    Case 4: {printf ("Today is Thursday \ n");
  Break
    Case 5: {printf ("Today is Friday \ n");
  Break
    Case 6: {printf ("Today is Saturday \ n");
  Break
    Case 7: {printf ("Today is Sunday \ n");
  Break
    } default: {printf ("Input error \ n");
Break  }
}

 

Console output: (We enter the number 2)

Please enter a 1-7 number

2

today is Tuesday

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.