The third check-in of C++ (basic operation type)


foreword

Ah, sorry, I skipped it because it was too simple, let me consolidate self-increment and self-decrement here


#include<iostream>
using namespace std;
main()
{
    
    
	char c='A';
	cout<<"c="<<++c<<endl;
	cout<<"++c会在运算之前进行运算"<<endl;
	cout<<"c="<<c++<<endl;
	cout<<"c++会在运算之后进行运算"<<endl;
	cout<<"c="<<c<<endl;
}

Ok, the consolidation is over, let's start the main content #wink

1. Sizeof finds the number of bytes (different from 'len()')

sizeof can find the character length of the data

#include<iostream>
using namespace std; 
main()
/*
sizeof求数据的字节数
strlen求数据的长度,别搞混了 
*/

{
    
    
	cout<<"sizeof('$')= "<<sizeof("$")<<endl;
	cout<<"sizeof('s')= "<<sizeof("s")<<endl;
	cout<<"字符型(char)长度为1字节"<<endl; 
	//字符型(char)长度为1字节
	cout<<"sizeof('abcde')= "<<sizeof("abcde")<<endl;
	cout<<"5个字符的字符串字节数为6 ,以此类推"<<endl; 
	//5个字符的字符串字节数为6 ,以此类推 
	cout<<"sizeof(9)= "<<sizeof(9)<<endl; 
	cout<<"sizeof(1234)= "<<sizeof(1234)<<endl;
	cout<<"整形int无论多大都是4字节"<<endl;
	//整形int无论多大都是4字节 
	cout<<"sizeof(9.0)= "<<sizeof(9.0)<<endl;
	cout<<"sizeof(3.1415926)= "<<sizeof(3.1415926)<<endl;
	cout<<"浮点型float 长度为8"<<endl;
	//浮点型float 长度为8  
 } 

sizeof can also find the character length of the variable!

#include<iostream>
using namespace std; 
main()
{
    
    
    int int1=15;float float1=3.1415926;
    double double1=1.23456789;
	char char1='MIAO A' ;
	cout<<"sizeof(int1)= "<<sizeof(int1)<<endl;
	//整形int无论多大都是4字节 
	cout<<"sizeof(float1)= "<<sizeof(float1)<<endl;
	//字符型float 长度为8  
	cout<<"sizeof(double1)= "<<sizeof(double1)<<endl;
	//字符型double 长度为16  
	cout<<"sizeof(char1)= "<<sizeof(char1)<<endl;
	//字符型(char)长度为1字节
}

2. Conditional statement exercises

1. Character variable conversion

Character variable conversion is the application of int() and float() ---------------#same python

2. if-else statement

The if else statement is the same as the C language. -----------------#Remember to bring braces

3. Ternary operator

(This is a bit interesting~)

Format: [E1?E2:E3]
Calculate the Boolean value of E1, if it is True, output E2;
if E1 is judged as False, then output E3;

#include<iostream>
using namespace std;
main()
{
    
    
	int a,b,max;
	//输入数据操作:cin>>
	cout<<"a=";
	cin>>a; 
	cout<<"b=";
	cin>>b;
	//三目运算符简化if else 结构 
	max=a>b?a:b;
	/*
	  格式:【E1?E2:E3】 
	  计算E1的布尔值,若为True,输出E2
	  若E1判断为False,则输出E3
	*/
	cout<<"Max="<<max<<endl;   
}

-------------------------------------------------- ---------------Separation line la la la ------------------------------ --------

switch switch statement

switch(E) { case constant 1: statement group 1; break; case constant 1: statement group 1; break; default: statement group n; break; }



E is the judged variable, if it is equal to the constant, then output the corresponding statement.
If none match, output the statement after default

#include<iostream>
using namespace std;
main()
{
    
    
//功能:查询前三个月的英文全称 
char m1='january',m2='Feburay',m3='March';
//待解决问题:char字符变量只能用单引号,实际储存只为最后一个字符 
int ask;
cout<<"亲输入月份数字:";
cin>>ask;
switch(ask)	{
    
    
	case 1:cout<<endl<<m1<<endl;break;
	//如果不加break,则输出判断正确语句后的所有语句
	case 2:cout<<endl<<m2<<endl;break;
	case 3:cout<<endl<<m3<<endl;break;
	default:cout<<"输入错误";break;
} 
	 
}

/

3. My favorite (loop)

.

1. for loop

for ([expression 1]: [expression 2]: [expression 3])
[expression 1] is the initial value condition, [expression 2] is the judgment condition, [expression 3] is the loop control condition
(and C language is exactly the same)

#include<iostream>
using namespace std;
main()//成功打印出来了字母表和99乘法表
{
    
    
	for(char c='A';c<='Z';c++)
	{
    
    
		cout<<c<<"\t";
    }
    cout<<endl<<endl;
	for(int i=1;i<10;i++)
	{
    
    
		for(int j=i;j<10;j++)
		{
    
    
			cout<<i*j<<"\t";
		}
		cout<<endl;
	}
}

2. while loop

while (expression)
{ statement group; } run the statement block if the expression is true


#include<iostream>
using namespace std;
main()//我有许多种打印99乘法表的方式!
{
    
    
	int i=1,j;
	while(i<10)
	{
    
    
		j=i;
		while(j<10)
		{
    
    
		cout<<j*i<<"\t";
		j++	;
		}
		i++;
		cout<<endl;
	}
}

3.do while loop

do
{ statement group } while (expression); must remember that there is a semicolon after this, and there is nothing after do



#include<iostream>
using namespace std;
main()//诶嘿!又打印了一份乘法表
{
    
    
	int i=1,j;
	do
	{
    
    
		j=i;
		do
		{
    
    
		cout<<j*i<<"\t";
		j++	;
		}
		while(j<10);
		i++;
		cout<<endl;
	}
	while(i<10);
}

The cycle ends here


Summarize

Here is a summary of the article:
This time we have learned the loop part, and the new part that we are not familiar with before took a lot of effort, but the latter loop is surprisingly familiar. In the next one, we will start to complete the loop keyword and start learning the combined data structure. Stay tuned!
PS: Why char data can only store one letter QAQ! ! !

Guess you like

Origin blog.csdn.net/m0_53590279/article/details/115379003