Conditional statement, if statement

Source: Internet
Author: User

Conditional statement, if statement

If statement

The three formats of the if statement are as follows:

1st forms:

If (expression) Statement

The syntax is: if the expression value is true, the subsequent statement is executed; otherwise, the statement is not executed.

2nd forms:

If (expression)

Statement 1

Else

Statement 2

The syntax is: if the expression value is true, Statement 1 is executed; otherwise, Statement 2 is executed.

3rd forms:

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

The syntax is: judge the value of the expression in sequence. When a value is true, execute the corresponding statement, and then jump beyond the entire if statement to continue executing the program. If all expressions are false, execute statement n and continue to execute subsequent programs.

 

Note:

(1) There are "expressions" after the if statement in the three forms, which are generally logical expressions or relational expressions. When the if statement is executed, the expression is first solved. if the expression value is 0, it is processed as "false". if the expression value is not 0, it is processed as "true, run the specified statement.

(2) The else clause cannot be used as a separate statement. It must be part of the if statement and used together with the if statement.

(3) if and else can be followed by one or more embedded operation statements. When multiple operation statements are used, use "{}" to enclose these statements into a composite statement.

(4) The if statement can be nested. That is, the if statement contains one or more if statements. When using the statement, note that else is always paired with the nearest unpaired if statement.

 

Else clause pairing

If an else clause is used for nesting an if statement, the else clause uses the proximity principle to pair it with the nearest if statement. If you want to pair the else clause with other if statements, you need to enclose the content between the current else clause and the if statement in braces, in this way, the program uses the content in braces as a composite statement to implement matching between the else clause and the if statement.

 

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int a, B, c, t; printf ("Enter 3 numbers: \ n"); scanf_s ("% d", & a, & B, & c); if (a <B) {t = a; a = B; B = t;} if (a <c) {t = a; a = c; c = t;} if (B <c) {t = B; B = c; c = t;} printf ("sorted as follows: \ n"); printf ("% d, % d, % d \ n ", a, B, c); system ("Pause"); return 0 ;}

 

Logical operators

& Logic and, a & B. If a and B are true, a & B is true.

| Logic or, a | B. If one of a and B is true, a | B is true.

! Non-logical ,! A. If it is true, then! A is false.

The priority of the three items is :! -> &-> |, That is "! "Is the highest among the three.

 

The difference between "=" and "="

"=" Is a relational operator, and the combination direction is "from left to right"; "=" is a value assignment operator, and the combination direction is "from right to left ".

 

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int year; printf (" Enter the year: \ n "); scanf_s (" % d ", & year); if (year % 4 = 0 & year % 100! = 0) | (year % 400 = 0) printf ("% d year is a leap year \ n", year ); else printf ("% d is not a leap year \ n", year); system ("Pause"); return 0 ;}

 

# Include "stdafx. h "# include <iostream> using namespace std; int Weight (int iArray [], int iNum) {int iRet; int iVar = iNum/3; int jVar, mVar, nVar, kVar; jVar = mVar = nVar = kVar = 0; for (int I = 0; I <iVar; I ++) {jVar + = iArray [I]; mVar + = iArray [I + iVar]; nVar + = iArray [I + iVar * 2];} int * pArray = new int [iVar]; if (jVar> mVar) {kVar = 0;} else if (jVar <mVar) {kVar = 1;} else if (jVar = mVar) {kVar = 2;} if (iVar = 1) {iRet = iVar * kVar;} else {for (int j = 0; j <iVar; j ++) {pArray [j] = iArray [j + iVar * kVar];} iRet = Weight (pArray, iVar) + iVar * kVar;} delete [] pArray; return iRet ;} int _ tmain (int argc, _ TCHAR * argv []) {int Ball [] = {2, 1, 1, 1, 1, 1}; int iWeightBall = Weight (Ball, 9); cout <"relatively heavy ball number:" <endl; cout <iWeightBall <endl; system ("Pause"); return 0 ;}

 

Break statement

A break statement is a jump statement controlled by a program. It can jump out of statement blocks and is often used in switch and loop statements. When a Program reaches a certain condition, it uses the break statement to jump out of the execution of the current statement.

 

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int bccd = 98; printf (" what is the unit price? \ N "); int Price; while (1) {scanf_s (" % d ", & Price); if (Price> bccd) {printf ("you guess the price is high! \ N ");} else if (Price <bccd) {printf (" you guess the Price is low! \ N ");} else {printf (" correct answer! \ N "); break ;}} system (" Pause "); return 0 ;}

 

Switch statement

The switch statement can be used to test a set of ordered data (such as integer, String, enumeration, and Boolean). When a matching constant is found, the statement associated with the constant is executed. The syntax of the switch statement is as follows:

Switch (expression)

{

Case constant 1:

Statement;

Break;

Case constant 2:

Statement;

Break;

......

Case constant n:

Statement;

Break;

Default:

Statement;

}

The expression must be of an ordered type, not a real number or a string type. The expression matches the constants in the case statement one by one. If a constant matches the expression, the statement in the current case is executed until the break statement is encountered, or the end of the switch statement (without the break statement ). When the expression does not find a constant that matches it, the code of the default part is executed. The default statement is optional. If no default statement is provided in the Code and no constant matches the expression, the switch statement does not execute any action.

 

Case statement usage tips

In a switch statement, if multiple case constants need to be processed in the same way, you can organize multiple case statements together without the break statement in the middle, enables multiple case statements to execute the same statement block.

# Include "stdafx. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {printf (" Enter the consumption amount: \ n "); float dMoney; scanf_s ("% f", & dMoney); int iMoney = dMoney; switch (iMoney/500) {case 0: printf ("your consumption has no discounts, amount: % 0.2f \ n ", dMoney); break; case 1: printf (" You can enjoy a discount on consumption. The amount is % 0.2f. the discount amount is: % 0.2f \ n ", dMoney, dMoney * 0.9); break; case 2: case 3: printf (" You can enjoy an 8-fold discount for your consumption. The amount is % 0.2f, the discount amount is % 0.2f \ n ", dMoney, dMoney * 0.8); break; case 4: case 5: printf (" You can enjoy a discount for your consumption. The amount is: % 0.2f, the discount amount is: % 0.2f \ n ", dMoney, dMoney * 0.7); break; case 6: case 7: printf (" You can enjoy a discount on your consumption, the discount amount is % 0.2f. the discount amount is % 0.2f \ n ", dMoney, dMoney * 0.6); break; default: printf (" You can enjoy a discount on your consumption, amount: % 0.2f. the discount amount is % 0.2f \ n ", dMoney, dMoney * 0.5); break;} system (" Pause "); return 0 ;}

 

Enumeration type

The enumerated type can associate a group of enumerated constants with an enumerated type name. The enumerated type is like a constant concentration camp. In this concentration camp, constants are named as enumeration constants. If a parameter defines the parameter type as an enumeration type, when you call this function, only the enumerated type of the concentration camp is passed as a parameter. Although the values of other constants can be the same as those of the enumerated constants, the values of other constants do not belong to the current enumerated type concentration camp, therefore, you cannot call a function as a parameter. Use the keyword enum to define an enumeration type.

When defining an enumeration type, you can provide an integer for each enumeration constant. If no integer is provided, the first constant value is 0 by default, and the second constant value is 1, and so on.

 

Assign a default value to an enumerated constant

Enum RecordsetState {RS_OPEN = 3, RS_WAIT, RS_CLOSE = 6 };

The preceding statement sets the enumerated constant RS_OPEN to 3 and the enumerated constant RS_CLOSE to 6 without providing the default value for RS_WAIT. The value of RS_WAIT is 1 for the previous enumerated constant value, therefore, the RS_WAIT value is 4.

 

#include "stdafx.h"#include <iostream>using namespace std;typedef enum NUMBER {ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN};int _tmain(int argc, _TCHAR* argv[]){    NUMBER num = FIVE;    switch (num)    {    case SEVEN:        cout << "    *************    " << endl;    case SIX:        cout << "     ***********    " << endl;    case FIVE:        cout << "      *********        " << endl;    case FOUR:        cout << "       *******        " << endl;    case THREE:        cout << "        *****        " << endl;    case TWO:        cout << "         ***        " << endl;    case ONE:        cout << "          *            " << endl;    }    system("Pause");    return 0;}

 

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.