If Else If – Age of Friend problem bettered – CSKC
Sign In

Search

Code Learning #CPP#1917    siteicon   siteicon  

If Else If – Age of Friend problem bettered

Simple if-else-if in a ladder style comparison to resolve age of friends problem which was not able to compare two friends with equal age.

Learning Objectives

  • Using if-else-if in a ladder form, which means checking for more if conditions in the else part of the selection.
  • Bettering the friend’s age problem which was done using the simple if-else only.

Source Code

TC++ #1917

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

Code Understanding

int sa,fa; cout<<“Enter your age : “;cin>>sa; cout<<“Enter your friend’s age : “;cin>>fa;
Here we collect your and your friend’s age.

if(sa==fa) cout<<“You both friends are equal in age.”<<endl;
We first compare if the age is equal. This we were not doing earlier when the age of two friends were assumed to be different.
else if(sa>fa) cout<<“You are older than your friend.”<<endl;
Here we compare your age to friend’s age. Mark the use of additional if in the else section. Here we are using the if ladder technique to create another level of comparison.

else cout<<“You friend is older than you.”<<endl;
Here we are doing the last condition assuming the other conditions are not met.

Notes

Common Errors

  • New programmers often confuse between =  and == operator. Remember that in if condition check it will always be == or conditional equal to check operator. The assignment operator = used only in statements where left hand side has to be made equal to the right hand side.


Suggested Filename(s): agecomparison-better.cpp, age-comparison.cpp



Share

Created: 26-Dec-2017 23:25 | Updated: 15-Sep-2018 11:47|