Lệnh switch case trong c

In life there are many things we have sầu to lớn choose, such as choosing between love and love sầu people we, choose lớn listen to the heart or mind,… If today is a school holiday, would stay or go play, if people love sầu it or go buy ice cream ZIPhường sit together to eat outside the school gates, etc. và cloudy cloudy. And programming is going from life, service life, it must have sầu such things. This lesson we will learn about the command to serve such options, these commands called branch instruction.

Bạn đang xem: Lệnh switch case trong c

Before learning about the structure of the branch instruction, we should learn a little about the thực đơn commands và command blochồng.

1. The command & the block

Order a task, expression, jaw, control structure ... any single.


x = x + 2; // đây là một lệnh đơn printf("Day la mot lenh "); // đó cũng là một lệnh đơn.block command: is a series of statements surrounded by braces .


//dau khoi a = 78; b = 26; printf("Tong %d + %d = %d", a, b, a+b); //cuoi khoiWhen you want to persize a series of consecutive commands that we use the command bloông chồng, ie put them into the braces và write indented 1 tab for easy look.

Noted: When an order is placed in brackets it is also regarded as the commvà blochồng.

2. If và if else

2.1 The if statement

The if statement translated means if this one vì chưng that. For example if you have a lover then you"ll be going out with you rather watch your posts…

The syntax of an if statement


Syntax, flowcharts, and how it works if statement – else

Example: Similar examples. Initially you 100 (billion), wife you 50 (billion), Please enter an amount. If the amount is positive, the plus in your wallet half, Your wife"s half, vice versa, only added to lớn your wallet (nature is minus because the amount can be negative or equal 0). Print out the amount you & your wife have sầu after typing.

#include int main() float x = 100, y = 50; // So tien ban va vo ban teo float z; // so tien nhap moi printf("Nhap so tien = "); scanf("%f", &z); if( z > 0 ) // neu so tien nhap vao lon hon 0 thi cong vao vi x = x + z / 2; y = y + z / 2; else x = x + z; printf("So tien cua ban = %.2f ", x); printf("So tien cua vo ban = %.2f ", y); return 0;Pretty simple. You try to lớn run and view results nhé. (In this article I used a float because the division can be the amount of retail).

Xem thêm: Star Up Là Gì - Những Vấn Đề Mà Startup Việt Nam Đang Gặp Phải

2.3 Else nested if statements & if-else-if

In many cases, we need to treat many conditions, not only 1 condition. Therefore we can nest multiple else if statements together.

Example: The company is implementing a balanced so balanced pairs serve balanced lovers, & notice the heavier boyfriover or girlfriend or two you weigh heavier equal.

So we need to handle 3 case, not simply if và bachồng again. We have the following code:

#include int main(){ float a; // can nang cua ban trai float b; // can nang cua ban gai printf("Nhap vao can nang ban trai va ban gai: "); scanf("%f%f", &a, &b); if( a > b ) printf("Ban trai nang hon! "); else { if( a Ok. So we use them interlocking. But this way will make the code a bit tangled and expensive sầu ink. You can use if-else-if statement is as follows:

#include int main(){ float a; // can nang cua ban trai float b; // can nang cua ban gai printf("Nhap vao can nang ban trai va ban gai: "); scanf("%f%f", &a, &b); if( a > b ) printf("Ban trai nang hon! "); else if( a How to code và also very clear sense, understandable :).

2.4 The conditional operator – If else shortened

We have sầu a shorthand operator else if statement is as follows:

điều kiện ? biểu máy 1: biểu thức 2;

If true, the expression conditions 1 is performed và the value of the expression 1 is the value of the entire command. If the condition is false, then the expression 2 is performed & the value of the expression 2 becomes the value of the entire command.


#include int main() int x = 3; int y = 4; int z; z = x > y ? x : y; printf("z = %d ", z); x = x > y ? x : 100; printf("x = %d ", x); return 0;Results after running the program:

z = 4 x = 100

Thus we see the expression 2 & expression 3 maybe 1 variable values, constant, hoặc một hàm nào đó tất cả trả về giá trị.

2. A switch case

The switch statement is similar if structure, but it is softer & more flexible than using if. However, it also has drawbacks as a result of the expression must be integer constant value (specific value). Another problem is the comm& switch can also use if, whereas it also depends on the algorithms of problem.

Switch case structure

with a, b, c typing.

Xem thêm: Come Down With Là Gì ? Come Down Trong Tiếng Tiếng Việt

Write a program to lớn 3 a positive sầu integer value, b, c. Cheông chồng if a, b, c Is 3 edge of the triangle is not? If 3 edge of the triangle, the triangle area calculation formula:

where p is half the perimeter of the triangle.

Write a program that includes charges for electrithành phố following conditions:

Subscription galvanometer: 1000e / monthThe electriđô thị usage per household: 50 KW for 230D / KWIf the excess amount If 50KW

If the excess amount> = 100KW then charged to lớn 900 dong / KW

New and old index is entered from the keyboard. In the old index screen, new index, pay norms, payments beyond the norm, the total amount payable.