Switch Statement Summary

How does switch detail the use of the switch statement?

The switch statement is a multi-branch selection structure that is often used to execute different blocks of code depending on the different values of a variable. The following is a detailed explanation of how switch statements are used:

Basic syntax of a switch statement

The basic syntax of a switch statement is as follows:

Where expression is a variable or expression that specifies the values to be compared, and constant after case is a constant expression that is used to compare with expression. If the value of expression is equal to the value of the constant following a case, the corresponding statement statement is executed until a break statement is encountered. If the value of expression is not equal to the value of the constant following any of the cases, the default statement is executed, and if there is no default statement, nothing is done.

Notes on the use of switch statements

Each case must be followed by a break statement, otherwise the program continues to execute the next case statement until it encounters a break.

Constant values following a case must be of integer or character type, not floating point or string type.

The type of the expression in the switch statement and the constant value following the case must be the same.

The default statement can be placed anywhere, but is usually placed last.

If more than one case is followed by a constant with the same value, you can omit the break statement so that the program executes the statements for those cases in turn until it encounters a break.

Sample code for a switch statement

The following is a simple example of a switch statement that outputs the corresponding day of the week based on a number entered by the user:

In the sample code above, the user enters a number (1-7), and the program outputs the corresponding day of the week based on that number. If the user enters a number that is not in the range 1-7, the program outputs “input error”.

switch syntax

The basic syntax format of a switch statement is as follows:

switch(expression){

case target value 1:

Execute statement 1

break;

case target value 2:

Execute statement 2

< p>break;

......

case target value n:

Execute statement n

break;

default:

Execute statement n+1

break;

}

In the above format, the switch statement matches the value of the expression with the value in each case’s In the above format, the switch statement matches the value of the expression with the target value in each case, and if it finds a match, it executes the statement after the corresponding case, and if it doesn’t find any match, it executes the statement after default.The break keyword in the switch statement serves to jump out of the switch statement.

It should be noted that the expression in the switch statement can only be a value of type byte, short, char, or int, and the program will report an error if any other type of value is passed in. However, the above statement is not rigorous, in fact, in JDK5.0 introduced a new feature enum enumeration can also be used as the value of the switch statement expression, in JDK7.0 also introduced a new feature, the switch statement can receive a String type of value.

In the process of using the switch statement, if multiple case conditions are followed by the same executing statement, the executing statement only needs to be written once, which is a shorthand way of writing. For example, to determine whether a day of the week is a working day, the same use of numbers 1 to 7 to indicate Monday to Sunday, when the input number is 1, 2, 3, 4, 5 is considered a working day, otherwise it is considered a rest day. Next through a case to realize the above described situation, as follows.SwitchDemo02.java

publicclassSwitchDemo02{

publicstaticvoidmain(String[]args){

intweek= 2;

switch(week){

case1:

case2:

case3:

case4:

case5:

/// When week satisfies any of the values 1, 2, 3, 4, or 5, it is handled the same way

System.out.println(“Today is a working day”);

break;

case6:

case7:

System.out.println( “Today is a day off”);

break;

}

}

}

packagecn.ceshi.demo3;

importjava.util.Scanner;

/*

*switch (){case} has a pass-through, so as long as break is not used after the conditional case, the condition holds

*Any other cases that follow it will be executed.

*/

publicclassSwitchCaseDemo{

publicstaticvoidmain(String[]args){

Scannersc=newScanner(System.in);

Stringa=sc.nextLine();

switch(a){

case “1”:

System.out.println(1);

break;

case “2”:

System.out. println(2);

case “3”:

System.out.println(3);

case “4”:

System.out.println(4);

case “5”:

System. out.println(5);

case “6”:

System.out.println(6);

default:

System.out.println(7);

}

}

}

Format and function of switch statements

Format and function of switch statements

switch(expression){

case constant expression 1:statement 1;

case constant expression 2:statement 2;

case constant expression n:statement n;

default:statement n+1;

}

Generally, this statement is used together with the loopback statement, where a break statement is added after each case statement to force it to jump out of the loopback; if it is not there, the following statements are implemented one after the other, and you do not get the desired result!

Function: Judge (expression) which (constant expression)

execute which case later statement.

Format and function of switch statement

+Language Programming

School Syllabus

1. Nature, Tasks and Objectives of the Course

The course on C++ Language Programming is a study of high-level language programming for computers. The main task is to introduce the data type, arithmetic, statement structure and its basic method of programming in C++. The purpose is to enable students to master a high-level programming language, understand the basic concepts and methods of object-oriented programming, and then learn to use C++ to learn to solve general application problems, and lay the foundation of programming for subsequent courses.

2. Teaching Media

Textbook: “C++ Language Fundamentals” (with self-study CD-ROM), edited by Xu Xiaokai, Tsinghua University Press

Exercise solutions: put on the Tsinghua University Publishing House of the spring in the world, a hundred flowers spitting colorful blessing of the yard four seasons always safe and joyful in the Spring Festival

select statement format and meaning

select statement In VB selectcase straight (numeric representation, literal representation)

case straight

caseelse

endselect

select*from table name where condition in sql database like: selectuserfromdywhereuser=abcde(lookup records in table dy where user is abcde)

select is also available in C. I won’t go into details here. If you want to know, you can go to Baidu to search for the use of select statement in C

switch statement role, function, and meaning

1. general form

switch (expression)

{case constant expression 1: statement 1

case constant expression 2: statement 2

case constant expression n: statement n

default:statement n+1

}

For example, it is required to print out the percentile bands according to the grades of the test scores

switch(grade)

{case ‘A’. printf(“85 to 100\n”);break;

case’B’:printf(“70 to 84\n”);break;

case’C’:printf(“60 to 69\n”);break;

case’D’:printf(“&lt. 60\n”);break;

default:printf(“error!\n”);

}

2. Explanations

(1) The “expression” in parentheses after switch is allowed by the ANSI standard to be of any type;

(2) the execution process;

(3) the order of occurrence of each case and default does not affect the execution result;

(4) the value of each case’s constant expression must be different from each other;

(5) after executing the statement after a case, the flow control is transferred to the next case, and the execution can be continued with a break statement. continue to execute, can be terminated by break statement.

(6) multiple cases can share a common set of execution statements, such as:

case’A’:

case’B’:

case’C’:prinf(“>60\n”);break;

SWITCH statement of the basic format, basic meaning?

xxhelforever retarded thing case followed by a colon

To VF SQL statement format

VF commonly used SQL statements

SQL is a structured query language, querying is an important part of the SQL language, but not all, SQL also includes data definition, data manipulation and data control functions. SQL also includes data definition, data manipulation and data control functions. Nowadays SQL has become the standard information language of relational database, so now the relational database management system all support SQL. FOXPRO from 2.5FORDOS version has begun to support SQL, now VISUALFOXPRO of course in the aspect of the more perfect, the following is the VF often use SQL statements.

–Statement Functions

— Data Manipulation

SELECT–Retrieve rows and columns from the database table

INSERT–New rows of data are added to the database table

DELETE–Delete rows of data are deleted from the database table

UPDATE–Update rows of data in the database table

Update rows of data in the database table

–DATA DEFINITION

CREATETABLE–Creates a database table

DROPTABLE–Deletes a table from the library

ALTERTABLE–Modifies the structure of the library table

CREATEVIEW–Creates a view

DROPVIEW – removes a view from the repository

CREATEINDEX – creates an index for a repository table

DROPINDEX – removes an index from the repository

CREATEPROCEDURE – creates a stored procedure

DROPPROCEDURE -removes a stored procedure from the repository

CREATETRIGGER – creates a trigger

DROPTRIGGER – removes a trigger from the repository

CREATESCHEMA – adds a new schema to the repository

DROPSCHEMA – removes A schema

CREATEDOMAIN – creates a data-value domain

ALTERDOMAIN – changes the domain definition

DROPDOMAIN – removes a domain from the repository

– DATA CONTROL

GRANT – grants the user access authorization

DENY–deny user access

REVOKE–release user access

–Transaction control

COMMIT–end the current transaction

ROLLBACK–abort the current transaction

SETTRANSACTION–define the current transaction data access Feature

-Programmed SQL

DECLARE-Sets a cursor for a query

EXPLAN-Describes the data access plan for a query

OPEN-Retrieves query results to open a cursor

FETCH-Retrieves one row of query results

CLOSE-Closes the Cursor

PREPARE – prepares SQL statement for dynamic execution

EXECUTE – dynamically executes SQL statement

DESCRIBE – describes the prepared query

— regional variable

declare@idchar(10)

— set@id=10010001

select@id=10010001

—area-wide variable

—must start with @@

—IFELSE

declare@xint@yint@zint

select@ x=1@y=2@z=3

if@x>@y

printx>y–print string x>y

elseif@y>@z

printy>z

elseprintz>y

— CASE

usepangu

updateemployee

sete_wage=

case

whenjob_level=’1’thene_wage* 1.08

whenjob_level=’2’thene_wage*1.07

whenjob_level=’3’thene_wage*1.06

elsee_wage*1.05

end

–WHILECONTINUEBREAK

declare@xint@yint@cint

select@x=1@y=1

while@x<3

begin

print@x – print the value of variable x

while@y<3

begin

select@c=100*@x+@y

print@c – print the value of variable c

select@y=@y +1

end

select@x=@x+1

select@y=1

end

–WAITFOR

–Example of waiting for 1 hour, 2 minutes, and 3 seconds before executing a SELECT statement

waitfordelay ’01:02:03′

select*fromemployee

–Example wait until 11:08:00 PM before executing the SELECT statement

waitfortime ’23:08:. 00′

select*fromemployee

ifthenelse conditional statement format is

c# does not have this kind of statement oh, only

if (conditional)

operation;

else< /p>

{

Operation

}

The format and location of the waitwindow statement inside VFP.

wait[emessagetext][tovarname][window[atnrow,ncolumn]][nowait]

[clear|noclear][timeoutnseconds]

Let’s see the column

waitwindow “Please wait…” nowait&& is displayed at the top right of the active window, and the program continues, but still displays “Please wait…” It doesn’t go away until waitclear

What is the format of the lnsert and select statements in vf?

inseinto table name value(column 1, column 2 …..)

selefrom table name intotable table name

What is the format of the nested if statement?

If there are if statements inside if statements, they are called nested if statements. For example, we start by determining whether a person is on Earth – the condition is met – nested if determines whether the person is in the Northern Hemisphere – the condition is met – nested if determines whether the person is a man!

This is a simple 3-tier nested if judgment:

if(a person is on Earth==true)

{

if(the person is in the Northern Hemisphere==true)

{

if(the person is a man==true)

{

Nested complete! , only if all 3 conditions are met. Nested mainly plays this role.

}

}

}