#include <iostream> using namespace std; int main(int argc, char * argv[]) { int
ID: 639333 • Letter: #
Question
#include <iostream>
using namespace std;
int main(int argc, char * argv[]) {
int i=1000;
bool k=10;
int x;
cout << " Hello! This program attempts to use a switch statement!! ";
i = i+k;
cout << "The value of i is: " << i << endl;
x = -13;
cout << "The value of x is: " << x << endl;
i = printf(" From prnt: %i ", i) , (++i)++ ;
cout << " After double incrementation the value of i is: " << i << endl;
i = (++i)++ , printf(" From prnt: %i ", i);
cout << " After double incrementation the value of i is: " << i << endl;
// functions can be used in expressions, if return value
~k ? printf(" expression is true ") : printf(" expression is false ");
!k ? printf(" expression is true ") : printf(" expression is false ");
const int a = 5; // declaration of a constant
for (int i=0; i<7; i++) {
switch (i) {
case 0*a:
cout << "From switch the value of case is: " << i << endl;
break;
case 1:
cout << "From switch the value of case is: " << i << endl;
break;
case 2:
cout << "From switch the value of case is: " << i << endl;
break;
default:
cout << "From default clause..." << endl;
}
}
}
can someone please explain to me how the ternary operator works for this code and also what exacctly is going on within this switchstatement the case 0 being multiplied by a is confusing me...
Explanation / Answer
//EXPLAINED IN COMMENTS< PLEASE READ COMMENTS
#include <iostream>
using namespace std;
int main(int argc, char * argv[]) {
int i=1000;
bool k=10;
int x;
cout << " Hello! This program attempts to use a switch statement!! ";
i = i+k;
cout << "The value of i is: " << i << endl;
x = -13;
cout << "The value of x is: " << x << endl;
i = printf(" From prnt: %i ", i) , (++i)++ ;
cout << " After double incrementation the value of i is: " << i << endl;
i = (++i)++ , printf(" From prnt: %i ", i);
cout << " After double incrementation the value of i is: " << i << endl;
// functions can be used in expressions, if return value
/*
* k is a boolean value, so 0 is treated as false and stored as zero
* while any other number is stored will be treated as TRUE and stored as 1
*
* now, k=10 is TRUE and stored as 1
* now ~k = compliment of k = compliment of 1 = -2
*
* Now in first ternary operator, -2 is treated as TRUE as mentioned above,
* so -2 is true and as per ternary operator the first statement is executed which is "expression is true"
*/
~k ? printf(" expression is true ") : printf(" expression is false ");
/*
* we know k was true which is 1,
* now !k is opposite value of TRUE which is false,
* hence ternary operator executes the false statement which is "expression is false"
*/
!k ? printf(" expression is true ") : printf(" expression is false ");
const int a = 5; // declaration of a constant
for (int i=0; i<7; i++) {
switch (i) {
case 0*a: //here 0*a = 0*5 = 0 so no problem , it actually means > "case 0: " only and treated as that so it is printed when i = 0
cout << "From switch the value of case is: " << i << endl;
break;
case 1: //it is printed when i = 1
cout << "From switch the value of case is: " << i << endl;
break;
case 2://it is printed when i = 2
cout << "From switch the value of case is: " << i << endl;
break;
default: // it is printed when i = 3,4,5,6
cout << "From default clause..." << endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.