MCU C language tutorial: C51 compound statements and conditional statements

Publisher:二进制游侠Latest update time:2012-09-21 Source: 21ic Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A friend once asked me on the BBS what does {} mean? What is its function? There are many brackets in C, such as {}, [], (), etc., which really confuse some beginners. In some languages ​​such as VB, the same () sign can have different functions. It can be used to combine several statements to form a function block, and can be used as an array subscript, etc. In C, the division of labor of brackets is more obvious. The {} sign is used to combine several statements together to form a function block. This kind of statement composed of several statements is called a compound statement. Compound statements are separated by {}, and each statement inside it still needs to end with a semicolon ";". Compound statements are allowed to be nested, that is, the {} in {} is also a compound statement. When the compound statement is running, the single statements in {} are executed in sequence. In the C language of the microcontroller, the compound statement can be regarded as a single statement, that is, it is grammatically equivalent to a single statement. For a function, the function body is a compound statement. You may know that compound statements can be composed not only of executable statements, but also of variable definition statements. It should be noted that the variables defined in a compound statement are called local variables. The so-called local variables mean that their effective scope is only in the compound statement. Functions are also compound statements, so the effective scope of variables defined in a function is also only within the function. The following is a simple example to illustrate the use of compound statements and local variables.

#include

#include

void main(void)

{

unsigned int a,b,c,d; //Will this definition be in the entire main function?

SCON = 0x50; //Serial port mode 1, allow receiving TMOD = 0x20; //Timer 1 timing mode 2

TH1 = 0xE8; //11.0592MHz 1200 baud rate TL1 = 0xE8;

TI = 1;

TR1 = 1; //Start the timer

a = 5; b = 6; c = 7;

d = 8; //This will be valid throughout the function

printf("0: %d,%d,%d,%dn",a,b,c,d);

{ //Compound statement 1

unsigned int a,e; //Only valid in compound statement 1

a = 10,e = 100;

printf("1: %d,%d,%d,%d,%dn",a,b,c,d,e);

{ //Compound statement 2

unsigned int b,f; //Only valid in compound statement 2

b = 11,f = 200;

printf("2: %d,%d,%d,%d,%d,%dn",a,b,c,d,e,f);

}//Compound statement 2 ends

printf("1: %d,%d,%d,%d,%dn",a,b,c,d,e);

}//Compound statement 1 ends

printf("0: %d,%d,%d,%dn",a,b,c,d);

while(1);

}

Operation results:

0:5,6,7,8

1: 10,6,7,8,100

2: 10,11,7,8,100,200

1: 10,6,7,8,100

0:5,6,7,8 Based on the above explanations, think about why the result is like this.

After reading the previous article, everyone will have a general understanding of the concept of conditional statements, right? Yes, just like learning conditional statements in Chinese, C language is also "if XX then XX" or "if XX then XX otherwise XX". That is, when the condition is met, the statement is executed. Conditional statements are also called branch statements, and some people call them judgment statements. The keyword is composed of if, which is basically the same in most high-level languages. C language provides 3 forms of conditional statements:

1: if (conditional expression) statement: When the result of the conditional expression is true, the statement is executed, otherwise it is skipped. For example, if (a==b) a++; when a is equal to b, a is increased by 1

2: if (conditional expression) statement 1

else statement 2

When the conditional expression is true, statement 1 is executed, otherwise statement 2 is executed, such as if (a==b)

a++;

else

a--;

When a is equal to b, a increases by 1, otherwise a decreases by 1.

3: if (conditional expression 1) statement 1

else if (conditional expression 2) statement 2

else if (conditional expression 3) statement 3

else if (conditional expression m) statement n else statement m

This is a nested if else statement, used to implement multi-directional conditional branches. When using it, you should pay attention to the pairing of if and else. If one is missing, there will be a syntax error. Remember that else is always paired with the closest if. Generally, conditional statements are only used for a single condition or a small number of branches. If there are more branches, the switch statement in the next article will be used more. If you use conditional statements to write a program with more than 3 branches, it will make the program less clear and easy to read.

Keywords:MCU Reference address:MCU C language tutorial: C51 compound statements and conditional statements

Previous article:MCU C language tutorial: C51 switch branch statement
Next article:Microcontroller C language tutorial: C51 expression statement and simulator

Recommended ReadingLatest update time:2024-11-16 16:23

How to make voice broadcast on microcontroller? Voice module principles and programming ideas
What I like most is making products with voice broadcasts. The process of writing programs is too boring. Some products take several months to develop, and the entire process is inevitably a bit lonely when dealing with cold LEDs, buzzers, buttons, and LCD screens. It's different when you add voice. Recording a nice g
[Microcontroller]
How to make voice broadcast on microcontroller? Voice module principles and programming ideas
Serial port sending and receiving in interrupt mode of AVR microcontroller
#include avr/interrupt.h  #include util/delay.h  int k=0,i=0,j=0;  char s ;  void usart0_init()  {    UCSR0A=0X00;    UCSR0B = 0x00;    UCSR0C=0X00;    UCSR0C=(1 UCSZ01)|(1 UCSZ00);//Eight-bit data bits without parity    UBRR0L=51;//Baud rate 8MHZ 9600    UBRR0H=0;    UCSR0B=(1 TXCIE0)|(1 RXCIE0)|(1 R
[Microcontroller]
System design of multiple signal sources based on AT89S51 microcontroller
1 Introduction With the rapid development of test equipment, its performance has attracted more and more attention. After a period of time, the test equipment must be tested. As an important component of the test equipment, the main test board plays an important role in its performance. When testing its performance, s
[Microcontroller]
System design of multiple signal sources based on AT89S51 microcontroller
PIC OTP microcontroller program burning method
MCU minimum system and programming pins Programming pin connection: Here we should pay special attention to the processing of Vpp pin; The burning tool used PIC K150 programmer; Here the author initially purchased Kit 3.5 because he saw that MPLAB X IDE v5.50 supported the chip he used. When he bought it, he fo
[Microcontroller]
PIC OTP microcontroller program burning method
MCS-51 Series MCU Parallel P1 Port Features
     The parallel P1 port of the MCS-51 series microcontroller is a quasi-bidirectional port and can only be used as a general I/O port to transmit data. Each bit of it can be defined as an input line or an output line, that is, the user can use some bits of the P1 port as output lines and other bits as input lines.  
[Microcontroller]
MCS-51 Series MCU Parallel P1 Port Features
51 MCU bit addressing instructions and programming
The 80C51 microcontroller has a bit processing function and can operate on data bits, so there is a corresponding bit addressing mode. The so-called bit addressing is to directly set a bit in the internal RAM or the bit-addressable special function register SFR to 1 or reset it to 0. The scope of bit addressing, tha
[Microcontroller]
C51 Programming 8- Digital Tube (Working Principle 2)
From the perspective of common cathode and common anode digital tubes, the control of the digital tube is the same as that of the LED. You only need to control the high and low levels at both ends of the digital tube (the levels of the bit select pin and the segment select pin) and the digital tube can be lit. Stati
[Microcontroller]
C51 Programming 8- Digital Tube (Working Principle 2)
PIC microcontroller realizes music playback
Source program: The following program needs to use two timer resources. Any PIC microcontroller with two timers can implement it. The MCU required for this example is MICROCHIP PIC16C62 INCLUDE "D:PICP16XX.EQU" ; This file can be found in the MICROCHIP CD ; ********************************************
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号