仅对if语句使用循环,然后打印else语句 [英] using loop for only if statement and then print else statement

查看:64
本文介绍了仅对if语句使用循环,然后打印else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查输入的字符是辅音还是元音?

我几乎得到了结果,但我只需要 else 语句即可退出循环.是否可以循环使用 else ?在这里,我尝试添加 {} ,但没有帮助我.我该怎么做?我已经以多种方式解决了该程序,但是我想清除 if 和循环的概念.

  #include< iostream>使用命名空间std;int main(){字符x;char vowel [10] = {'a','e','i','o','u','A','E','I','O','U'};cout<<"输入" ;;cin> x;for(int i = 0; i <10; i ++){{if(x ==元音[i]){cout<<"这是元音"}}别的{cout<<"这是辅音"}}返回0;} 

解决方案

循环 for(语句表达式;操作)主体循环只是一个while循环

  {陈述;而(表达式){身体label_for_continue:行动;}} 

因此,您可以看到内部声明的所有内容的生命都将终止于 for .您可以在执行 action 后使用 break 提前停止循环,并使用 continue 跳回到循环开始(等于 goto label_for_continue ),这使它比 while 更具通用性,而 continue 直接跳转到开始".

可以省略或以语言语法允许的任何方式使用任何或所有部分,从而导致使用外部计数器,即永远".循环 for(;;)或预递增循环 for(int i = MAX; i-> 0;).for循环使用的一种特殊情况是基于范围的循环./p>

您需要做的是使用一些外部变量来标记您的当前状态.默认情况下将其设置为 false ,因为如果您的循环正常退出,则意味着您没有找到匹配项.如果找到匹配项,则将短路".将flag设置为true后,按 break 循环:

  bool is_vowel = false;对于(int i = 0; i< 10; i ++){如果(x ==元音[i]){is_vowel = true;休息;}} 

Check whether if the entered character is consonant or vowel?

I almost got the result but I just need else statement to be out of loop. Is it possible to use else out of loop? Here I tried adding {} but didn't help me. How do I do so ? I have solved this program in many ways but I want to clear my concepts of if and loop.

#include <iostream>
    
using namespace std;

int main(){
    char x ;
    char vowel[10] ={'a','e','i','o','u','A','E','I','O','U'};
    cout<<"Enter ";
    cin>>x ;
    for(int i = 0 ; i<10 ; i++)
    {
        {    
            if(x ==vowel[i])
            {
                cout<<"This is a vowel";
            }
        }   
        else
        {
            cout<<"This is a consonant";
        }   
    }
    return 0 ;
}

解决方案

Loop for( statement expression; action ) body loop is just a while loop

{
   statement;
   while ( expression )
   {
     body

label_for_continue:
     action;
   }
}

So you can see that everything declared inside will have its life ending outside of for. You can use break to stop loop prematurely and continue to jump back to beginning of loop after performing action ( equals to goto label_for_continue), this makes it a little more versatile than while where continue jumps directly to "beginning".

Any or all parts may be omitted or used in ways allowed by syntax of language, resulting in use of external counter, a "forever" loop for(;;) or pre-increment loop for(int i = MAX; i-->0;). A special case of for loop use is range-based loop.

What you need to do is to use some external variable to mark your current state. Let it be false by default, because if your loop exits normally, it means that you didn't found a match. If you found match, you would "short-circuit" loop by break after setting flag to true:

bool is_vowel = false;
for (int i = 0 ; i<10 ; i++) {
    if (x == vowel[i]) {
        is_vowel = true;
        break; 
    }
}

这篇关于仅对if语句使用循环,然后打印else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆