用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在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值