📜  如果将 print() 语句写入 if() 内,例如 if(print()) 会发生什么

📅  最后修改于: 2022-05-13 01:54:33.283000             🧑  作者: Mango

如果将 print() 语句写在 if() 之类的 if(print()) 中会发生什么

先决条件: if-else

本文重点讨论在 if-else 条件语句中使用 print 语句时会发生什么。

例如:考虑下面的代码并预测输出。

C
// C program
 
#include 
 
// Driver code
int main()
{
    // if statement
    if (printf("I'm Awesome!\n"))
 
        // calling main() function
        main();
 
    // else-if statement
    else if (printf("I'm Not Awesome\n"))
 
        // calling main() function
        main();
}


C++
// C++ program
 
#include 
using namespace std;
 
// Driver code
int main()
{
    // if statement
    if (printf("I'm Awesome!\n"))
 
        // calling main() function
        main();
 
    // else-if statement
    else if (printf("I'm Not Awesome\n"))
 
        // calling main() function
        main();
    return 0;
}


Java
// Java program to implement the above approach
class GFG {
 
    public static void main(String[] args) {
 
        // if statement
        if (System.out.printf("I'm Awesome!\n") != null)
 
            // calling main() function
            main(null);
 
        // else-if statement
        else if (System.out.printf("I'm Not Awesome\n") != null)
 
            // calling main() function
            main(null);
    }
}
// This code contributed by Princi Singh


Python3
# Python program
# Driver code
# if statement
if(print("I'm Awesome!")):
     
   # calling main() function
   def main():
        
# else-if statement
elif(print("I'm Not Awesome")):
             
   # calling main() function
   def main():
 
# This code is contributed by shivanisinghss2110


C#
// C# program to implement the above approach
using System;
class GFG {
 
    public static void Main(String[] args) {
 
        // if statement
        if (Console.WriteLine("I'm Awesome!\n"))
 
            // calling Main() function
            Main(null);
 
        // else-if statement
        else if (Console.Write("I'm Not Awesome\n"))
 
            // calling Main() function
            Main(null);
    }
}
 
// This code is contributed by shivanisinghss2110


Javascript


C
// C program to implement
// the above approach
#include 
 
// Driver code
int main()
{
    // Nested printf function
    printf("%d", printf("GFG!\n"));
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
    // nested printf function
    printf("%d", printf("GFG!\n"));
    return 0;
}


Java
// Java program to implement
// the above approach
 
import java.util.*;
 
class GFG{
 
// Driver code
public static void main(String[] args)
{
    // nested printf function
    System.out.printf("%s", System.out.printf("GFG!\n"));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program to implement
# the above approach
 
# Nested printf function
print(print("GFG!\n"))
     
# This code is contributed by shivanisinghss2110


C#
// C# program to implement
// the above approach
 
using System;
 
class GFG{
 
// Driver code
public static void Main(String[] args)
{
    // nested printf function
    Console.Write("GFG!\n");
}
}
  
 
// This code is contributed by shivanisinghss2110


Javascript


C
// C program to implement
// the above approach
#include 
 
// Driver code
int main()
{
    // printf function inside if statement
    if (printf(""))
 
        // printf function
        printf("Condition is True");
 
    // else statement
    else
 
        // printf function
        printf("Condition is False");
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
    // printf function inside if statement
    if (printf(""))
 
        // printf function
        printf("Condition is True");
 
    // else statement
    else
 
        // printf function
        printf("Condition is False");
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG
{
 
// Driver code
public static void main (String[] args)
{
   
    // printf function inside if statement
    if (System.out.print(""))
 
        // printf function
        System.out.print("Condition is True");
 
    // else statement
    else
 
        // printf function
        System.out.print("Condition is False");
}
}
 
// This code is contributed by shivanisinghss2110.


Python3
# Python program to implement
# the above approach
 
    # print function inside if statement
    if (print("")):
 
        # print function
        print("Condition is True")
 
    # else statement
    else:
 
        # print function
        print("Condition is False")
 
# This code is contributed by shivanisinghss2110.


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
 
// Driver code
public static void Main (String[] args)
{
   
    // printf function inside if statement
    if (Console.Write(""))
 
        // printf function
        Console.Write("Condition is True");
 
    // else statement
    else
 
        // printf function
        Console.Write("Condition is False");
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


猜测预期输出:对该程序输出的最普遍猜测是某种错误。



实际输出:但它的实际输出有点奇怪,而不是预期的输出。

输出#1

解释:让我们讨论一下原因。

  • 好像每次都满足条件并且由于 main()函数的递归调用,这个函数调用将理想地无限进行,但在实际场景中,一旦允许的函数调用堆栈被填满,它就会停止。
  • 所以,首先,让我们了解为什么if条件中的打印函数没有给出错误以及它在内部是如何工作的。
  • 这种行为基于一个简单的事实,即 C/C++ 等中的printf等打印函数返回一个整数值,该值等于它已成功打印的字符数。

让我们通过下面的程序来理解这一点——

C

// C program to implement
// the above approach
#include 
 
// Driver code
int main()
{
    // Nested printf function
    printf("%d", printf("GFG!\n"));
    return 0;
}

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
    // nested printf function
    printf("%d", printf("GFG!\n"));
    return 0;
}

Java

// Java program to implement
// the above approach
 
import java.util.*;
 
class GFG{
 
// Driver code
public static void main(String[] args)
{
    // nested printf function
    System.out.printf("%s", System.out.printf("GFG!\n"));
}
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

# Python program to implement
# the above approach
 
# Nested printf function
print(print("GFG!\n"))
     
# This code is contributed by shivanisinghss2110

C#

// C# program to implement
// the above approach
 
using System;
 
class GFG{
 
// Driver code
public static void Main(String[] args)
{
    // nested printf function
    Console.Write("GFG!\n");
}
}
  
 
// This code is contributed by shivanisinghss2110

Javascript


输出
GFG!
5

说明:当遇到 printf 语句时,它转到外部printf函数并尝试打印其值,当程序尝试打印其值时,它遇到另一个printf并转到内部printf (如递归调用)以执行它然后它执行它并且内部printf函数返回5,因为该函数已成功打印 5 个字符('G'、'F'、'G'、'!' 和 '\n'),因此返回一个整数值等于在这种情况下为 5。

如果总是返回true,是否会在内部使用print()?

从上面的例子,很容易理解printf函数在if语句中是如何工作的,以及条件是如何满足的。条件将始终满足,直到并且除非打印空字符串,如下面的代码所示 -

这个例子背后的唯一想法是演示如果printf函数返回“0”,那么if条件就会变成假。在所有其他情况下,字符串的长度是 1 还是 9999 都是正确的。

C

// C program to implement
// the above approach
#include 
 
// Driver code
int main()
{
    // printf function inside if statement
    if (printf(""))
 
        // printf function
        printf("Condition is True");
 
    // else statement
    else
 
        // printf function
        printf("Condition is False");
    return 0;
}

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
    // printf function inside if statement
    if (printf(""))
 
        // printf function
        printf("Condition is True");
 
    // else statement
    else
 
        // printf function
        printf("Condition is False");
    return 0;
}

Java

// Java program to implement
// the above approach
import java.io.*;
 
class GFG
{
 
// Driver code
public static void main (String[] args)
{
   
    // printf function inside if statement
    if (System.out.print(""))
 
        // printf function
        System.out.print("Condition is True");
 
    // else statement
    else
 
        // printf function
        System.out.print("Condition is False");
}
}
 
// This code is contributed by shivanisinghss2110.

蟒蛇3

# Python program to implement
# the above approach
 
    # print function inside if statement
    if (print("")):
 
        # print function
        print("Condition is True")
 
    # else statement
    else:
 
        # print function
        print("Condition is False")
 
# This code is contributed by shivanisinghss2110.

C#

// C# program to implement
// the above approach
using System;
 
class GFG
{
 
// Driver code
public static void Main (String[] args)
{
   
    // printf function inside if statement
    if (Console.Write(""))
 
        // printf function
        Console.Write("Condition is True");
 
    // else statement
    else
 
        // printf function
        Console.Write("Condition is False");
}
}
 
// This code is contributed by shivanisinghss2110

Javascript



输出#2
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程