What Is The Value Of Ans After The Following Code Has Been Executed?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What will be the value of ans after the following code has been executed?
int ans = 10;
int x = 65;
int y = 55;
if (x >= y)
ans = x + y;
A) 10
B) 120
C) 100
D) No value, there is a syntax error.
None
Answer: Option B 120
Explanation: The code consists of three variables ans, x, and y, and the values held by them are 10,65, and 55 respectively. If the variable x has a greater value than variable y, then if statement will run ans=x + y. The running of the statement will sum x with y ->65+55 and the already given value of ans is overwritten by the sum of the result which is 120.
Conditional statement
The purpose of the conditional statement is to decide based on the given conditions. in case of no condition, the conditional statement will automatically execute, And if some of the condition is present then the flow of execution is changed and the result is according to it and the whole process is known as the decision making. conditional statements have different forms such as:
If statement
The first condition of the operating system is the If statement. the syntax is easy it starts with IF followed by the braces and the condition which needs to be met is a place between the braces. the If condition works by checking whether the condition in the expression is true or false. If the answer is true then the statement runs otherwise it skips to another block.
Syntax : if(condition) {}
Example : if (today==Monday)
{
Cout<< “good morning”;
}
Else If statement
Moreover, if condition checks only one condition but else if check multiple conditions .the else if have an almost same pattern such as if start and close with brace along with the condition, and the condition is not true then else if and the next result will display to the screen
Syntax: if(condition)
{}
Else if(condition){
}
Example: if (today== “rainy”)
{
Cout<<” read at the home”;
}
Else if (today== “sunny”)
{
Cout<<” read in the library”;
}
Then else if check the condition from the start if the first condition is true then other ones will terminate and if it is false then the code moves to the second condition and continuously runs until the condition is true.
Switch statement
The switch statement makes the code more understandable rather than if, else if statements. The block is terminated by the break keyword in the switch statements. the statements in the switch are shown with the cases.
Syntax: switch {condition)
{
case 1:
break;
case 2:
break;
default:
break;
}
Example:
Int today=2;
Switch(today)
{
Case 1:
Cout<<” read in the library”;
Break;
Case 2:
Cout<<” read at home”;
Break;
Default:
Cout<<” get some sleep”;
}
The switch statement runs till the true value is returned and the code is terminated with the break keyword. And in case of false the default statement will run.