Question

C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone...

C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone please explain how we can get to that conclusion by just reading the code, can it be explained to me because when i try to do it I get a different answer for i and j I got 1 and 4.

#include <iostream>

using namespace std;

int main()

{

int i = 1;

int j = 0;

if (i++ == 1)

{

j = ++ i;

}

else

{

j+=4;

}

cout << i<< " and " << j <<endl;

return 0;


}

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

i=1 and j=0
if(i++ ==1 ) i++ is post increment so here i is taken as 1 so 1==1 true,
now i++ so i=2 and j=0
j=++i so here ++i is pre increment so 1st it will increment so i=3
so j=3


Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone...
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
  • C++, Will Upvote: A) Try to get the following programs to run in your environment. B)...

    C++, Will Upvote: A) Try to get the following programs to run in your environment. B) If the programs will run, document each line of code with comments and describe any changes you had to make to the original code to get it to work. If the programs are unable to be modified to run with desirable results, explain why you think so. C) Description your solution and a synopsis of how your code is intended to work and the...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know w...

    Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know what the 8 Queens problem is, "The eight queens puzzle is the problem of placing eightchess queens on an 8×8 chessboard so that no two queensthreaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal." This is the code: #include <iostream> #include <cmath> using namespace std; bool ok(int q[], int c) {     ...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • This is C++ I only can change the lines highlighted in white (i can add lines...

    This is C++ I only can change the lines highlighted in white (i can add lines to it with enter though) Set hasDigit to true if the 3-character passCode contains a digit. 1 #include <iostream» 2 #include <string> 3 #include <cctype> 4 using namespace std; 6 int main 7 bool hasDigit; 8 string passCode; 10 hasDigit false; 11 cin passCode; 12 13 Your solution goes here 14 15 if ChasDigit) 16 cout << "Has a digit." < endl; 17 18...

  • C++ Can someone please help me with this problem- commenting each line of code so I...

    C++ Can someone please help me with this problem- commenting each line of code so I can understand how to solve this problem using the C++ programming language? I really need help understanding how to create a file for the program to read. Do I create the file in Visual basic or create a text file? I have the code, just need to know how to create the file for it to read. #include<fstream> #include<iostream> using namespace std; int main()...

  • In addition to including an exceptions library, you can also create exceptions by using the throw...

    In addition to including an exceptions library, you can also create exceptions by using the throw keyword. (1pt) Create a function called int throwsInt() that will throw an integer (1pt) Create a function called double throwsDouble() that will throw a double (2pt) Using try/catch statements, expand the code stubs to call each function in it's own try block, and catch and report the thrown value. Provided code: #include <iostream> using namespace std; int throwsInt(); double throwsDouble(); int main() { cout...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible...

    Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible would be really grateful to a quick solution. would give a thumbs up. Thank you! Q3: Question # 3 [20] Will the following code compile? If it does not, state the errors. If it does compile, write the output. //Function.cpp #include <iostream> using namespace std; void printData (long i) cout<<"In long print Data "«<i<<endl; } void printData(int i) cout<<"In int printData "<<i<<endl; ) void...

  • How can I modify the program below with this prompt asking "How many minutes from now...

    How can I modify the program below with this prompt asking "How many minutes from now do you expect to be home?", and output a sentence saying "You will get home at HH:MM". ?? It's very URGENT!! Thank you #include <iostream> #include <ctime> using namespace std; int main) time t t struct tm *now t-time (e) now-localtime(&t); int hour = now->tm.hour; // retrieve current hour int min = now->tm..min; // retrieve current min // get current time // adjust for...

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
Active Questions
ADVERTISEMENT