Question

In c++, can someone explain and provide an example of a nested if and else, using...

In c++, can someone explain and provide an example of a nested if and else, using the for loop? Thank you very much

0 0
Add a comment Improve this question Transcribed image text
Answer #1

An example of a nested if and else, using the for loop :

Syntax :

//for loop

for(initialization; condition; increment/decrement)

{

//nested if ..else

if(condition)

{

true statement;

}

else if(condition)

{

true statement;

}

.

.

.

else

{

false statement

}

}

Here, I am going to explain nested if-else using for loop with one simple example where I am going to accept multiple numbers using for loop (assume 10 numbers) so here nested if-else will check whether the entered number is positive, negative or zero.

If the entered number is greater than 0 then it's a positive number

If the entered number is less than 0 then it's a negative number

If the entered number is equal to 0 then it's a zero number

Program :

#include<iostream>
using namespace std;

int main()
{
int num,i;
for(i=1;i<=10;i++)
{
cout<<"\nEnter a number: ";
cin>>num;
if(num>0)
{
cout<<num <<" is a Positive number."<<endl;
}
else if(num<0)
{
cout<<num <<" is a Negative number."<<endl;
}
else
{
cout<<num <<" is a Zero number."<<endl;
}
}
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
In c++, can someone explain and provide an example of a nested if and else, using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Provide an example of a nested block-structure control-flow structure consisting of   at least one for loop...

    Provide an example of a nested block-structure control-flow structure consisting of   at least one for loop at least one signed and unsigned comparison which don't check for (in)equality. (<, <=, >, or >= are all fine) at least one other block-structured control-flow idiom (i.e., if/else, while, or do/while) And a linearization of it to goto-c, including clear annotation as to why it is correct.

  • Can you please explain what does each sentence mean and give an example if it’s possible?....

    Can you please explain what does each sentence mean and give an example if it’s possible?. I need to have answers for all of them please Assignment Statements- Input- cin >> Output--cout << endl<"n" Output formatting Arithmetic operators +-'* / % Relational Operators-=-= ++- Logical Operators ! && II Evaluating Expressions of mixed type Evaluating Boolean Expressions Type Conversion, Implicit Coersion Explicit-Type Casting Control Structure: Sequence, decision (branching), looping (repetition) if, if-else, if/else-if/else, nested ifs switch statement conditional statement while...

  • Could someone provide me general java script statements that would answer these questions? These can be...

    Could someone provide me general java script statements that would answer these questions? These can be very general, I just need to know what statements to use to perform these functions so I can develop an outline. How to implement concatenated if/else statements. How to implement while loops, do while loops, and for loops. Be able to perform error checking using while loops. Define and implement classes: properties, accessors, mutators, constructors (defaults and with arguments), methods, toString method Create objects...

  • I'll rate any incomplete answer or if you give me someone else answer 1. Strategy implementation...

    I'll rate any incomplete answer or if you give me someone else answer 1. Strategy implementation is always difficult. Many organizations never successfully implement their strategies. What are some of the blocks to successfully implementing a strategy. Please provide an example of an organization that was not successful in implementing a new strategy. 2.  The link between organizational culture and strategy is very strong. Changing the culture is very difficult and can be very risky. This is especially true in an...

  • Hello! I am hoping that someone can provide me with an example for this question. I...

    Hello! I am hoping that someone can provide me with an example for this question. I am supposed to write a confidence interval problem (not solve it) using this option-- "Think about a population mean that you may be interested in and propose a confidence interval problem for this parameter. Your data values should be approximately normal. For example, you may want to estimate the population mean number of times that adults go out for dinner each week. Your data...

  • Provide an example of the dangling-else problem in Python and explain why it is not possible in t...

    Provide an example of the dangling-else problem in Python and explain why it is not possible in the language. Draw the activation record for the following C function: void random(int first, float index) { static int count; int cursor; char buffer[2]; /* ... */ }

  • 9. For each of the following, provide a suitable example, or else explain why no such...

    9. For each of the following, provide a suitable example, or else explain why no such example exists. [2 marks each]. a) A function f : C+C that is differentiable only on the line y = x. b) A function f :C+C that is analytic only on the line y = x. c) A non-constant, bounded, analytic function f with domain A = {z | Re(z) > 0} (i.e., the right half-plane). d) A Möbius transformation mapping the real axis...

  • can someone please explain how to do this with as much detail as possible. someone tried...

    can someone please explain how to do this with as much detail as possible. someone tried to explain it earlier in relation to (a,e) and (e,a) but im not sure even what that means Bccurance of Methyl shift o. The following two elimination reactions are found to have very different rates of reaction. The cis compound produces two products and it reacts much faster than the trans isomer, which produces only one product. Explain these observations (the erence in rate...

  • can you please give ?% right and detailed answer or pass to someone else... Please don't...

    can you please give ?% right and detailed answer or pass to someone else... Please don't do it if you are not sure...Thank in advance. -(a) State Cauchy's integral theorem for contour integration. Apply Cauchy's integral theorem to evaluate the integral: Ic 2 + 4 where C is a circle z = 1.5 taken anticlockwise. (Marks 6)

  • Explain why in Python, writing a multiple selection using else-if clauses (elif) is more readable than...

    Explain why in Python, writing a multiple selection using else-if clauses (elif) is more readable than using nested if then-else statements. Use some Python code to illustrate your answer.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT