-3

I wanted to make a simple enhanced if statement (that is the correct definition right?) when a == 1 OR b > 2 it prints out how to say how are you sir in Arabic.

Is it possible to compare two different variables in an if and else if statement? I got myself in a Chinese finger trap with all the ('s and )'s and the different types of logical operators.

Here is my c++ code:

#include <iostream>

using namespace std;

//If / Else statement is basically a enhanced if statement.

int main()
{
int a = 1;
int b = 2;

if ((a==1)||(b>2)){
    cout << "Kevak chala komm?" << endl;
}

if (((else)) (a == 1) && (b == 2)))) {
    cout << "Louis C.K. is back my brothers!" << endl;
}
else{
cout << "Jek shi mash? " << endl; // How are you in polish.
}
return 0;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    `if (((else)) (a == 1) && (b == 2))))` is simply invalid syntax. – πάντα ῥεῖ Oct 03 '18 at 07:57
  • 1
    Do you simply want `else if`? Also it's spelled *jak się masz*. – Bartek Banachewicz Oct 03 '18 at 07:57
  • Yea that is what I want, and thanks for the correct spelling :) I just put it down how I heard it from the polish guy when I was doing my postal route. – Mapacherama Oct 03 '18 at 07:58
  • What do you mean by "enhanced if"? – melpomene Oct 03 '18 at 07:58
  • The tutorial I followed set enhanced if was the same as a else if statement. I think either he or I made a mistake, I'm sorry for the confusion. – Mapacherama Oct 03 '18 at 08:00
  • 2
    I'd recommend you to have a look at our [list of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Yksisarvinen Oct 03 '18 at 08:00
  • Is Python a good language to thinker around (trail and error) in I got "Automate the boring stuff in Python" laying next to me. Is it a good idea to work through that first? – Mapacherama Oct 03 '18 at 08:03
  • 2
    Depends on your intentions. If you wish to learn C++, then tinkering with Python is unlikely to get you there. And as for the tutorial you follow, if it taught you about `if (((else))` then ditch it ASAP. – StoryTeller - Unslander Monica Oct 03 '18 at 08:04
  • Learning *three* languages at the same time sounds even worse than learning two. – Bartek Banachewicz Oct 03 '18 at 08:05
  • @JerômeTesselaar Python is as good as any other programming language. If you want to have something very verbose, try Delphi first (Pascal was often used as language to teach basic programming paradigms). – πάντα ῥεῖ Oct 03 '18 at 08:07
  • I think I learned my lesson, I'm just going to pick one of the three programming languages and put my focus on that. I was just interested how you do the same thing in another language, and the way that it looks. I started with Java and I think I'm just going to stick with Java, till I get a firm grasp on how it all works (in about a year, maybe even 2 years). Every time I post on this site I learn something new, and I love that. Thanks for the help guys, its much appreciated! – Mapacherama Oct 03 '18 at 08:42

2 Answers2

2

The correct syntax for that looks like this:

if (a == 1 || b > 2 ){
    std::cout << "A" << std::endl;
}
else if (a == 1 && b == 2) {
    std::cout << "B" << std::endl;
}
else {
    std::cout << "C" << std::endl;
}

But it doesn't make any sense in your particular case, since B will never be printed (if a == 1, the first clause will hit, never using the second).

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Yea like I said I was just trying some stuff out, thanks for the answer. At least I know that what I tried is possible even tough it doesn't have much use. I just like to try to get as much use as I can out of a lesson, even tough sometimes it doesn't make much sense. I hope you get what I try to say, 9/10 times you don't get anything out of it. But sometimes you might strike gold. – Mapacherama Oct 03 '18 at 08:06
  • @JerômeTesselaar If 9 out of 10 times you don't get anything out of a programming lesson, I strongly suggest you change your learning resource. – Bartek Banachewicz Oct 03 '18 at 08:10
-2

Is it possible to compare two different variables in an if and else if statement?

Yes it is very much possible, you can compare two or more different variables in if and else if stetement and also there can be multiple else if statement as well.

The below code line is wrong

if (((else)) (a == 1) && (b == 2)))) {

write else if instead.

Sanjeev
  • 348
  • 2
  • 9
  • what is the reason for downvote? help me to correct myself by providing the comments. – Sanjeev Oct 03 '18 at 08:25
  • I just think that the people here don't want anything that learns newcomers bad coding practice, I appreciate the help allot but else stated here above I should be using 1 type of variable in the parameter with if and else if, else. If I could up vote I would but I don't have enough reputation to let it be shown ;) – Mapacherama Oct 03 '18 at 08:47