有谁有C语言条件语句的例子吗?

发布于 2024-09-19 01:54:26 字数 154 浏览 3 评论 0原文

像这样的东西,我想看看完整的语法。

伪代码:

var = user_input

if var > 5:
    output = 'var > 5'
else:
    output = 'var < 5'

Something like this, I'd like to see the full syntax.

Pseudo Code:

var = user_input

if var > 5:
    output = 'var > 5'
else:
    output = 'var < 5'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

戏舞 2024-09-26 01:54:26

怎么样:

#include <stdio.h>
#include <string.h>

int main (void) {
    int var;
    char buff[100];

    printf ("Enter number> ");
    fflush (stdout);
    if (fgets (buff, sizeof(buff), stdin) == NULL) {
        printf ("\nfgets() failed\n");
        return 1;
    }
    if (sscanf (buff, "%d", &var) != 1) {
        printf ("\nsscanf() failed\n");
        return 1;
    }

    if (var > 5)
        printf ("%d is greater than 5\n", var);
    else
        printf ("%d is less than 6\n", var);

    return 0;
}

进行几次测试运行:

pax> testprog
Enter number> 99
99 is greater than 5

pax> testprog
Enter number> -5
-5 is less than 6

How about something along the lines of:

#include <stdio.h>
#include <string.h>

int main (void) {
    int var;
    char buff[100];

    printf ("Enter number> ");
    fflush (stdout);
    if (fgets (buff, sizeof(buff), stdin) == NULL) {
        printf ("\nfgets() failed\n");
        return 1;
    }
    if (sscanf (buff, "%d", &var) != 1) {
        printf ("\nsscanf() failed\n");
        return 1;
    }

    if (var > 5)
        printf ("%d is greater than 5\n", var);
    else
        printf ("%d is less than 6\n", var);

    return 0;
}

with a couple of test runs:

pax> testprog
Enter number> 99
99 is greater than 5

pax> testprog
Enter number> -5
-5 is less than 6
世界等同你 2024-09-26 01:54:26
  1. 变量和数据类型
  2. Scanf
  3. 条件语句

希望这能帮助您入门。

  1. Variables and datatypes
  2. Scanf
  3. Conditional statements

Hope this will help you get started .

情绪操控生活 2024-09-26 01:54:26

这似乎符合您的需求:

int var;
scanf("%d", &var);

if (var > 5)
    printf("var > 5\n");
else
    printf("var <= 5\n");

关于使用 scanf() 的注意事项 - 我通常不喜欢它的弹性代码,但它很快给出了最小的答案。

您必须将其包装在 main()#include 中才能使其可执行:

#include <stdio.h>
int main()
{
    int var;
    scanf("%d", &var);
    if (var > 5)
        printf("var > 5\n");
    else
        printf("var <= 5\n");
    return 0;
}

This seems to correspond to what you'd like:

int var;
scanf("%d", &var);

if (var > 5)
    printf("var > 5\n");
else
    printf("var <= 5\n");

With caveats about using scanf() - I generally don't like it for resilient code, but it gives a minimal answer swiftly.

You'd have to wrap it in a main() and #include <stdio.h> to make it executable:

#include <stdio.h>
int main()
{
    int var;
    scanf("%d", &var);
    if (var > 5)
        printf("var > 5\n");
    else
        printf("var <= 5\n");
    return 0;
}
听闻余生 2024-09-26 01:54:26

除了其他两个答案之外,总是有三元运算符 ?: ,可以这样使用:

printf("var %s 5\n", var > 5 ? ">" : "<=");

In addition to the other two answers, there's always the ternary operator ?: which can be used like this:

printf("var %s 5\n", var > 5 ? ">" : "<=");
最丧也最甜 2024-09-26 01:54:26
if(condition)
    doThis();
else
    doThat();

这几乎就是你所拥有的。您的示例:

if(var > 5)
    output = "var > 5";
else
    output = "var < 5";

唯一的区别是语句后面需要分号,条件表达式周围需要括号,而冒号不是必需的。

您还可以使用花括号来表示在给定特定条件下执行的命令块。然而,当只执行一行时,则不需要大括号。但这相当于:

if(var > 5)
{
    output = "var > 5";
}
else
{
    output = "var < 5";
}

您可以在 if 之后或在 else 之后使用大括号,或者两者都使用,或者两者都不使用。但请记住,对于多个语句,需要使用大括号。

还值得注意的是,换行符是可选的。这可以写成

if(var > 5) output = "var > 5";
else output = "var < 5";

甚至

if(var > 5) output = "var > 5"; else output = "var < 5";

但是这段代码的可读性要差得多。第一种和第二种形式是更好的练习。

if(condition)
    doThis();
else
    doThat();

It's pretty much what you've got there. Your example:

if(var > 5)
    output = "var > 5";
else
    output = "var < 5";

The only difference is that you need semicolons after the statements and parentheses around the conditional expression, and the colons are not required.

You can also use curly braces to denote a block of commands to execute given a certain condition. When there's only one line being executed, however, the braces are not necessary. But this is equivalent to:

if(var > 5)
{
    output = "var > 5";
}
else
{
    output = "var < 5";
}

You can have braces just after the if or just after the else, or both, or neither. Remember, though, that with multiple statements the braces are required.

It's also worth noting that the line breaks are optional. This could be written

if(var > 5) output = "var > 5";
else output = "var < 5";

Or even

if(var > 5) output = "var > 5"; else output = "var < 5";

But this code is far less readable. The first and second forms are better practice.

银河中√捞星星 2024-09-26 01:54:26
char var = getchar();

if (atoi(var) > 5)
{
  printf("var > 5 \n");
}
else
{
  printf("var < 5 \n");
}
char var = getchar();

if (atoi(var) > 5)
{
  printf("var > 5 \n");
}
else
{
  printf("var < 5 \n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文