用c++中的if语句求绝对值、字母大小转换、简单函数

if语句求X的绝对值、字母大小写转换、简单函数

**X的绝对值

使用if语言的简单分支

**

#include <iostream.h>
void main()
{int x;
cin>>x;
if(x<0) x=-x;
cout<<"|x|="<<endl;
}

字母大小写转换

#include <iostream.h>
void main();
{char c;
cin>>c;
if(c>='A'&&c<='Z') c=c+32;
cout<<c<<endl;
}

if语句的嵌套

  1. if-else
    `
  2. else if

在这里插入图片描述
编写程序计算函数

else if语句

#include <iostream.h>
void main()
{int x,y;
cout<<"piease enter the number:";
cin>>x;
if(x>3)
    y=x*x+4else if(x>0)
    y=x;
else
   y=x*x-4;
 cout<<"y="<<y<<endl;
 }


if-else式

#include <iostrean.h>
void main()
{int x,y;
 cout<<"please enter a number:";
 cin>>x;
 if(x>0)
     if(x<=3)
         y=x;
     else
         y=x*x+4;
 else
    y=x*x-4;
 cout<<"y="<<y<<endl;
 }

三个并列if结构

#include <iostream.h>
void main()
{int x,y;
cout<<"please enter a number:";
cin>>x;
if(x>3) y=x*x+4;
if(x>0&&x<=3) y=x;
if(x<=0) y=x*x-4;
cout<<"y="<<y<<endl;
}

在这里插入图片描述if-else在这里插入图片描述

发布了1 篇原创文章 · 获赞 1 · 访问量 47

猜你喜欢

转载自blog.csdn.net/nuojiacoming/article/details/104639837
今日推荐