c語言if else語句
Find the output of the following programs,
查找以下程序的輸出,
Program 1)
程序1)
#include <stdio.h>
int main()
{
int x = 400, y, z;
if (x >= 500)
y = 400;
z = 300;
printf("%d %d\n", y, z);
return 0;
}
Output
輸出量
32766 300
Explanation:
說明:
In the code, the condition x>=500 is false, so the variable y will not be assigned and the statement z=300 is written after the conditional statement, so it will be assigned with 300. Thus, the value of y will be a garbage value and value of z will be 300.
在代碼中,條件x> = 500為假,因此將不分配變量y,并且在條件語句之后寫入語句z = 300 ,因此將其分配為300。因此, y的值將為垃圾值和z的值為300。
Program 2)
程序2)
#include <stdio.h>
int main()
{
int p = 800, q, r;
if (p >= 700)
q = 600;
r = 500;
printf("%d %d\n", q, r);
return 0;
}
Output
輸出量
600 500
Explanation:
說明:
In the code, the condition p>=700 is true, so the variable q will be assigned with the value 600 and the statement r=500 is written after the conditional statement, so it will be assigned with 500. Thus, the value of q will be a 600 value and value of r will be 500.
在代碼中,條件p> = 700為true,因此將為變量q分配值600,并在條件語句后寫入語句r = 500 ,因此將其分配為500。因此, q將是600的值,而r的值將是500。
Program 3)
程序3)
#include <stdio.h>
int main()
{
int a = 30, b = 40;
if (a == b);
printf("%d %d\n", a, b);
return 0;
}
Output
輸出量
30 40
Explanation:
說明:
In the code, the condition if(a==b); is terminated with semicolon so the statement printf("%d %d\n",a,b); will not be consider as a body of the if statement. Thus, the program will print the value of a and b.
在代碼中,條件為if(a == b); 以分號終止,因此語句printf(“%d%d \ n”,a,b); 不會被視為if語句的主體。 因此,程序將打印a和b的值。
Program 4)
程序4)
#include <stdio.h>
int main()
{
int e = 4;
float f = 4.0;
if (e == f) {
printf("E and F are equal\n");
}
else {
printf("E and F are not equal");
}
return 0;
}
Output
輸出量
E and F are equal
Explanation:
說明:
In the code, variable e is an integer type and variable f is a float type, while comparing them with an if statement (if(e==f)), the value of f will be truncated to an integer (due to implicit type conversion). Thus, the condition will be true and "E and F are equal" will be printed.
在代碼中,變量e是整數類型,變量f是浮點類型,將它們與if語句( if(e == f) )進行比較時, f的值將被截斷為整數(由于隱式類型)轉換)。 因此,條件為真, 并且將打印“ E和F相等” 。
Program 5)
程序5)
#include <stdio.h>
int main()
{
int p = 4, q, r;
q = p = 15;
r = p < 15;
printf("p = %d q = %d r = %d\n", p, q, r);
return 0;
}
Output
輸出量
p = 15 q = 15 r = 0
Explanation:
說明:
In the code, the statement q = p = 15; is assigning 15 to the variables p and q and the statement r = p<15; is assigning 0 to the variable r because p is not less than 15 (condition is false). Thus, the value of p, q and r will be 15, 15, and 0.
在代碼中,語句q = p = 15; 將變量p和q賦值為15,并且語句r = p <15; 因為p不小于15(條件為假),所以將0賦給變量r 。 因此, p , q和r的值將分別為15、15和0。
Program 6)
計劃6)
#include <stdio.h>
int main()
{
int i = 65;
char j = 'A';
if (i == j) {
printf("This place is beautiful\n");
}
else {
printf("This place is not beautiful\n");
}
return 0;
}
Output
輸出量
This place is beautiful
Explanation:
說明:
The character variable stores the ASCII code of the given character (it's also a number type of variable). Thus, the value of j will be 65 (The ASCII Code of 'A' is 65). So the condition if(i==j) will be true and "This place is beautiful" will be printed.
字符變量存儲給定字符的ASCII碼(它也是變量的數字類型)。 因此, j的值為65(“ A”的ASCII碼為65)。 因此條件if(i == j)將為true,并且將打印“這個地方很漂亮” 。
Program 7)
計劃7)
#include <stdio.h>
int main()
{
float p = 13.25, q = 14.5;
if (p = q) {
printf("Hello\n");
}
return 0;
}
Output
輸出量
Hello
Explanation:
說明:
In the statement if(p=q), p=q is not a comparison operation, we used = (assignment operator), thus the value of q will be assigned into p and the statement will be evaluated to 14.5 which is a non-zero value and conditional will be true.
在語句if(p = q) , p = q不是比較運算的情況下,我們使用= (賦值運算符),因此q的值將分配給p,并且該語句的計算結果為14.5 ,這不是零值和條件將為真。
Recommended posts
推薦的帖子
Find output of C programs (if else statement) | set 2
查找C程序的輸出(如果為else語句)| 設置2
Find output of C programs (Bitwise Operators) | Set 1
查找C程序的輸出(按位運算符)| 套裝1
Find output of C programs (Bitwise Operators) | Set 2
查找C程序的輸出(按位運算符)| 套裝2
Find output of C programs (Strings) | Set 1
查找C程序的輸出(字符串)| 套裝1
Find output of C programs (Strings) | Set 2
查找C程序的輸出(字符串)| 套裝2
Find output of C programs (Structures) | Set 1
查找C程序的輸出(結構)| 套裝1
Find output of C programs (Mixed topics) | Set 1
查找C程序的輸出(混合主題)| 套裝1
Find output of C programs (Mixed topics) | Set 2
查找C程序的輸出(混合主題)| 套裝2
Find output of C programs (Mixed topics) | Set 3
查找C程序的輸出(混合主題)| 套裝3
Find output of C programs (Character) | Set 1
查找C程序的輸出(字符)| 套裝1
Find output of C programs (floating point) | Set 1
查找C程序的輸出(浮點數)| 套裝1
Find output of C programs (For loops) | Set 1
查找C程序的輸出(用于循環)| 套裝1
Find output of C programs (Arrays) | Set 1
查找C程序的輸出(數組) 套裝1
翻譯自: https://www.includehelp.com/c/if-else-find-output-of-programs-c-set-1.aspx
c語言if else語句