Basic Statement of C language

Source: Internet
Author: User
Tags case statement

1 Basic form if (expression) statement

The semantics are: if the expression is true, the subsequent statement is executed, and if the expression is false, the statement is not executed.

Example 1 compares two integers, max is the maximum number.

1 void Main () 2 {  3   int  4  5   max=A; 6 7  if (max<B) 8 {  9    max=b; Ten } one}

2 If ... else form.

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

If the expression is true, execute statement 1, and if it is not true, execute statement 2

Example 2 compares two integers, max of which is the maximum number, and use the If.....else statement to discriminate the size of a/b.

void Main () {     int'    if(a>b) {  =A;} Else {   =b;}}

3 switch-case statement.

switch (expression)

{

case constant Expression 1: statement 1;

Case constant Expression 2: statement 2;

Case constant Expression 3: statement 3;

Default: Statement n+1;

}

Evaluates the value of an expression and compares it to the value of the constant expression thereafter, when the value of the expression is equal to the value of a constant expression, that is, executes the subsequent statement, and then ceases to judge, and proceeds to the statement following all the subsequent case;

When the value of an expression is not equal to the constant expression following all of the case, the statement after the default is executed.

Example 4 Switch Statement instance

#include <stdio.h>voidMain0{   Chardat; Day=3; Switch(DAT) { Case 0:p rintf ("sun\t");  Case 1:p rintf ("mon\t");  Case 2:p rintf ("tues\t");  Case 3:p rintf ("wed\t");  Case 4:p rintf ("thurs\t");  Case 5:p rintf ("fri\t");  Case 6:p rintf ("satur\t"); defualt:printf ("This is a worng number\n");}}

The result of the operation is Wed Thurs Fri Satur

Basic Statement of C language

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.