1707586517

Use of conditionals in c++


Another major component in C++ programming is the use of conditionals.  Conditionals are statements that cause a program to alter its intended path based on a program’s current value.  One of the first and most basic conditional statements to be discussed is an if statement. Before going into various types of conditional statements we must first know which relational operands to use within our statements and what they mean. ```c #include <iostream> int main(){ std::cout << "Enter a digit between 1 and 10: "; int num; std::cin >> num; if(num > 10) std::cout << num << "Your number is greater then 10\n"; else std::cou << num << " Your number is not greater"; } ``` The following program will demonstrate how if statements operate and execute.   As you may have guessed, there will also be times when more than one if statement is needed.   Therefore, multiple if statements can be used.   The example program below shows how multiple if statements execute.  In order to successfully execute multiple if statements we can use a block. ```c #include <iostream> int main(){ std::cout << "Enter anumber: "; int x; std::cin >> x; if(x > 10) { //both statements will be executed if x > 10 std::cout << "You entered " << x << "\n"; std::cout << x << "is greater than 10\n"; } else { //both statements will be executed if x <= 10 } } ``` Blocks are essentially the use of the brackets {}.  Using these brackets allows the user to tell program where to start and end an if statement.  In the event that the blocks are not specified the program is smart enough to implicitly define blocks. If statements can also be chained together by using else if as shown in the program below.   Aside from the ability to input chained if statements, nesting if statements are also very possible to do in C++.  The following is an example of nesting functions. ```c #include <iostream> int main() { std::cout << "Please input a digit: "; int num; std::cin >> num; if(num > 10) std::cout << num<< "Your number is far greater than 10\n"; else if(num < 10) std::cout << num << "The entered number is far less" } ``` The above code is confusing simply because of the missing brackets.  Adding blocks around the else would make the code clearer. This ambiguity is known as a dangling else. ```c #include <iostream> int main(){ std::cout << "Entera random digit: "; int num; std::cin >> num; if(num > 10) // outer if statement // it us bad coding style to nest if statements this way if(num < 20) // inner if statement std::cout << num << " Your number is between the number 10 and 20\n"; // which if statement corresponds to the else listed } ``` want to learn more about c++ then leave your comment here

(2) Comments
JavaJuggler
JavaJuggler
0

@gulfcoast9033 the purpose of the platform is to provide a learning and networking environment for programmers. our features are more in line with the two items mentioned above. but we're just getting started and I'm sure more features will be added to the site.


gulfcoast9033
gulfcoast9033
0

Hi, first comment to this platform."Conditionals are statements..."Conditionals are expressions. Lots of operations in C (and other languages) are expressions.These comment boxes are pretty limited, but, at the very least, please keep opening braces in-line with the method, function, etc. You don't write Python like this, do you?def sum_two(a: int, b: int) -> int : return a b


Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]