小程序 || 语句
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
小程序 || 语句