小程序 || 語句
Program 1:
程序1:
#include <iostream>
using namespace std;
int main()
{
switch (printf("Hello World")) {
case 0x09:
cout << " India";
break;
case 0x0A:
cout << " Australia";
break;
case 0x0B:
cout << " USA";
break;
case 0x0C:
cout << " England";
break;
}
return 0;
}
Output:
輸出:
Hello World USA
Explanation:
說明:
The above code will print "Hello World USA" on the console screen.
上面的代碼將在控制臺屏幕上打印“ Hello World USA” 。
As we know that printf() function prints data on the console screen and returns the total number of characters printed on the screen.
我們知道, printf()函數在控制臺屏幕上打印數據,并返回屏幕上打印的字符總數。
printf("Hello World")
The above printf() function returns 11. And the hexadecimal equivalent of 11 is 0x0B, then case 0x0B will be executed.
上面的printf()函數返回11 。 并且十六進制的11等于0x0B ,那么將執行情況0x0B 。
And the final output is "Hello World USA".
最后的輸出是“ Hello World USA” 。
Program 2:
程式2:
#include <iostream>
using namespace std;
int main()
{
int A = 10, B = 5, C = 2;
switch (A * ++B + C - 8) {
default:
cout << "Pakistan ";
case 0x09:
cout << "India ";
break;
case 0x0A:
cout << "Australia ";
break;
case 0x0B:
cout << "USA ";
break;
case 0x0C:
cout << "England ";
break;
}
return 0;
}
Output:
輸出:
Pakistan India
Explanation:
說明:
The above code will print "Pakistan India " on the console screen. Here we pass an expression in the switch block. Based on the expression result, the case will be executed.?
上面的代碼將在控制臺屏幕上打印“ Pakistan India” 。 在這里,我們在switch塊中傳遞一個表達式。 根據表達式結果,將執行大小寫。
Evaluate the expression step by step:
逐步評估表達式:
int A=10,B=5,C=2;
=A*++B+C-8
=10*6+2-8
=60+2-8
=54
The result of the expression is 54 and there is no such case is defined. Thus, the default case will be executed and then the case 0x09 will be executed (Because break statement is missing after in default case)
表達式的結果為54 ,沒有定義這種情況。 因此,將執行默認情況 ,然后執行情況0x09 (因為在默認情況下缺少break語句 )
Then the final output "Pakistan India" will be printed on the console screen.
然后,最終輸出“巴基斯坦印度”將打印在控制臺屏幕上。
Recommended posts
推薦的帖子
C++ Switch Statement | Find output programs | Set 2
C ++轉換語句| 查找輸出程序| 套裝2
C++ Operators | Find output programs | Set 1
C ++運算符| 查找輸出程序| 套裝1
C++ Operators | Find output programs | Set 2
C ++運算符| 查找輸出程序| 套裝2
C++ const Keyword | Find output programs | Set 1
C ++ const關鍵字| 查找輸出程序| 套裝1
C++ const Keyword | Find output programs | Set 2
C ++ const關鍵字| 查找輸出程序| 套裝2
C++ Reference Variable| Find output programs | Set 1
C ++參考變量| 查找輸出程序| 套裝1
C++ Reference Variable| Find output programs | Set 2
C ++參考變量| 查找輸出程序| 套裝2
C++ Conditional Statements | Find output programs | Set 1
C ++條件語句| 查找輸出程序| 套裝1
C++ Conditional Statements | Find output programs | Set 2
C ++條件語句| 查找輸出程序| 套裝2
C++ goto Statement | Find output programs | Set 1
C ++ goto語句| 查找輸出程序| 套裝1
C++ goto Statement | Find output programs | Set 2
C ++ goto語句| 查找輸出程序| 套裝2
C++ Looping | Find output programs | Set 1
C ++循環| 查找輸出程序| 套裝1
C++ Looping | Find output programs | Set 2
C ++循環| 查找輸出程序| 套裝2
C++ Looping | Find output programs | Set 3
C ++循環| 查找輸出程序| 套裝3
C++ Looping | Find output programs | Set 4
C ++循環| 查找輸出程序| 套裝4
C++ Looping | Find output programs | Set 5
C ++循環| 查找輸出程序| 套裝5
翻譯自: https://www.includehelp.com/cpp-tutorial/switch-statements-find-output-programs-set-1.aspx
小程序 || 語句