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...

  • Can someone help me write a retirement calculator program in c code. I want to write...

    Can someone help me write a retirement calculator program in c code. I want to write a program that the user can enter their age, amount they wish to save each month, the interest rate, and the age they wish to retire. I would like to use a switch loop, if else loop or do while loop. the output should show them how much they should have in a retirement savings account at the age they wish to retire at....

  • 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...

  • 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]; /* ... */ }

  • Please explain nested for loops in Javascript like I'm 5 years old. No matter how many...

    Please explain nested for loops in Javascript like I'm 5 years old. No matter how many examples I look at of nested for loops, I can't really understand what they're doing, or when I'd actually want to use them. Every time I look at an example of someone using them, I figure I can just call on the items in a single loop. I can't find many concrete examples where I'd say ''ahh, I definitely need to nest for loops...

  • 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...

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