Question

In: Computer Science

c programming Explain the difference between the statements on the left and the statements on the...

c programming

  1. Explain the difference between the statements on the left and the statements on the right for each group of statements, give the final value of x if the initial value of x is 1

If (x>=0)

x =x +1;

Else if (x>=1)

x = x+2;

If ( x>=0)

x = x+1;

if (x>= 1)

x=x+2;

Solutions

Expert Solution

# Answer :

GIven : x = 1

Statements on the Left :

If (x>=0)
x =x +1;
else if (x>=1)
x = x+2;

Evaluation : x>=0 1>=0 true . So the first If statement is true . Therefore x =x +1 x =1+1 x = 2 . All other conditions are skipped.

Final Result : x = 2

Explanation :

  • This is if-elseif ladder. Here if the specified condition is not true, it will continue to check the next condition in the ladder (next else if) until it satisfies any condition.
  • The format of the if else if ladder is :

if(condition 1){
   // if condition 1 is true execute this block
}
else if(condition 2){
   // if condition 1 is false and condition 2 is true execute this block
}
else if(condition 3){
   // if condition 1 is false, condition 2 is false and condition 3 is true execute this block
}
else{
   // if all the conditions are false then execute this block (optional block)
}

  • Here, it will continue to check the next condition in the ladder (next else if) until it satisfies any condition.
  • else is an optional block, it executes when none of the conditions are true.
  • Once it finds the first condition which it satisfies, it executes its block and skips all the other conditions after it in the else ladder even if they are satisfied.
  • In the above, the problem both the conditions satisfy the given value of x, but only the first one which comes in the ladder is being executed and others which are after it are skipped.
  • Hence, if(x>=0) condition block is only executed because it is the earliest condition block.

Statements on the Right :

If ( x>=0)
x = x+1;
if (x>= 1)
x=x+2;

Evaluation :

The first condition : x>=0 1>=0 true . Therefore, x = x+1 x = 1+1 x = 2.

The value of x is now 2. (x =2)

The second condition : x>= 1 2>=1 true . Therefore, x = x+2 x = 2+2 x = 4

The value of x is now 4. (x =4)

Final Result : x = 4

Explanation :

  • There are two separate simple if statements.
  • The format of simple if  :

if(condition){
   // if condition is true this block will be executed otherwise skipped
}

  • Above problem (Statements on the right) both the if statements will be executed as they are separate.
  • FIrstly If ( x>=0) is executed which will update the value of x if the condition is true. It evaluates true and sets value of x to 2 (x = 2).
  • Secondly If ( x>=1) is executed which will update the value of x if the condition is true. It evaluates true and sets value of x to 4 (x = 4).
  • So the final value of x here is 4.

Difference between the Left and Right Statements :

  • Left statements form an if-elseif ladder. Here the first condition which satisfies is executed and other conditions are skipped even though they are satisfied.
  • While the right statements have two separate if statements. They are executed one by one updating the value of x.

Related Solutions

Explain the difference between array and structure based on their usage in C++ programming. Declare a...
Explain the difference between array and structure based on their usage in C++ programming. Declare a structure called studentScore which contains name of student, registration number of students, course code and marks. Declare structure variable named studentCS680 based on the structure in (b) to store fifty (50) students’ data. Write a program that prompts a user to enter data for 50 students in a structure variable declared in (b) and calculate the average mark.
C++ Programming Simply explain what the difference between "including" a header file and using a compiled...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled library file during linking? Where are the C++ header files and compiled C++ library files on your computer?
Explain the difference between the two circuits, C-M-C and M-C-M.
Explain the difference between the two circuits, C-M-C and M-C-M.
Explain the difference between the modulo operator and the division operator for C#
Explain the difference between the modulo operator and the division operator for C#
Explain the difference between a necessary and a sufficient condition, and their relation to conditional statements....
Explain the difference between a necessary and a sufficient condition, and their relation to conditional statements. Provide an example, and give it in both standard form and symbolic form. philosophy critical thinking class
Which of the following statements is false regarding a bargain sale? explain        The difference between...
Which of the following statements is false regarding a bargain sale? explain        The difference between the fair market value of the asset and the consideration received in exchange for the asset is considered a gift.        The gift portion of a bargain sale will qualify for the annual exclusion.        A bargain sale is generally inappropriate if the buyer of the property is a family member.        If the property is sold by the seller for more than the...
What is the difference between Fact and Query in a logic programming language?
What is the difference between Fact and Query in a logic programming language?
Discuss in a group of two persons on the following statements. ( C++ programming ) (a)...
Discuss in a group of two persons on the following statements. ( C++ programming ) (a) A compiler is the same as an interpreter. (b) We can only use high-level language and not low-level language to write a computer program. (c) Low-level language is close to machine language and high-level language is close to human language. (d) Algorithm design is not important in software development process. (e) When the computer is on, the first loading program is the Operating System.
Explain the difference between a call option and a put option. Explain the difference between an...
Explain the difference between a call option and a put option. Explain the difference between an American option and European option. Find the value of a call option using the binomial option pricing formula for single period when given the following information: you have an option with 6 months until expiration, the payoff in the up scenario is $12, and the payoff in the down scenario is $0, the risk-free rate is 5%, the weight for the up scenario is...
(c) Explain TWO (2) differences between int and Integer in the context of Java programming using...
(c) Explain TWO (2) differences between int and Integer in the context of Java programming using suitable example,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT