To get more capabilities we add decision making (selection). The primary selecti
ID: 3623707 • Letter: T
Question
To get more capabilities we add decision making (selection). The primary selection tool that we will use is the if/else structure. Here is a simple if/else example (using pseudocode):if (myint is equal to 5)
{
Display a message telling the user that the value was five
}
otherwise
{
Display a message telling the user that the value was not five
}
Notice a couple of things:
In the if statement the test is placed inside parenthesis.
The test for the if statement must return a true/false (yes/no) result.
The equality operator is == not =. The = operator would first assign the value 5 to myint, then always return true since we just changed myint so it equals 5.
Write a C program that will prompt the user to enter an integer value
Store that value in the int variable myint
Use the if/else structure from above (with needed modifications to make it C) to test the value of myint. If myint is equal to 5, tell the user it equaled five, otherwise (else) tell the user it did not equal five.
Add a second if/else that duplicates the first except use = (assignment) instead of == (equality).
Explanation / Answer
#include int main(void){ int i; printf("please enter the number "); scanf("%d",&i); printf(" "); if(i == 5){ printf(" the value of i is five "); } else { printf("the value of i is not five "); } if(i=5){ printf(" the value of i is five "); } else { printf(" the value of i is not five "); } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.