1 Convert each ifelse if statement into a ternary expression. a) if (pressureRea
ID: 3890074 • Letter: 1
Question
1 Convert each ifelse if statement into a ternary expression. a) if (pressureReading > 59) reducePressure true else reducePressure false: b) if(x - 2) y-1 else 2. Convert the following if statements into an assignment statement. a) if (pressureReading > 59) reducePre sure-true i else reducePressure-false; b) if(x - 2) y-1 else 3. Convert the following ifelse if statement into a switch statement. if (grade =- ' A' grade =- ' a ' ) else if (grade 'B' " grade-'b') else if (grade 'C'II grade 'c else if (grade-"D' grade-' d' ) aCount++i bCount+ti cCount++ dCountt+i fCount+t elseExplanation / Answer
1(a).
//ternary operator x = a>b ? a : b; if condition is true then x = a else x = b
reducePressure = (pressureReading>59) ? true : false;
1(b).
y = (x == 2) ? 1 : 2;
2(a).
//conditional operation(a>b,a<b,a==b,a>=b,a<=b) return boolean value so we can assign them to a boolean variable
boolean reducePressure;
int pressureReading = 60;
reducePressure = (pressureReading>59);
2(b).
int x=1; //assign any value to x
boolean bool;
int i;
bool = (x == 2); // x = 2 then bool will be true otherwise false
i = (bool) ? 1 : 0;
System.out.printf("%d",i);
3.//by using switch statement we can convert a if-else codition in switch statement
char ch; //char type variable for defining grade
printf("Enter grade : ");
scanf("%c",&ch);
switch(ch)
{
case 'A':
case 'a':
aCOunt++;
break;
case 'B':
case 'b':
bCOunt++;
break;
case 'C':
case 'c':
cCOunt++;
break;
case 'D':
case 'd':
dCOunt++;
break;
default:
fCount++;
berak;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.