C++ if else Statement - GeeksforGeeks

C++ if else Statement

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Decision Making in C++ helps to write decision-driven statements and execute a particular set of code based on certain conditions.

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the C++ else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}

Working of if-else statement

  1. Control falls into the if block.
  2. The flow jumps to Condition.
  3. Condition is tested.
    1. If Condition yields true, goto Step 4.
    2. If Condition yields false, goto Step 5.
  4. The if-block or the body inside the if is executed.
  5. The else block or the body inside the else is executed.
  6. Flow exits the if-else block.

Flowchart if-else:

Examples of if else statement in C++

Example 1:

Below program demonstrates the use of if else statements in C++.

C++




// C++ program to illustrate if-else statement 
    
#include <iostream> 
using namespace std; 
    
int main() 
    int i = 20; 
    
    // Check if i is 10 
    if (i == 10) 
        cout << "i is 10"
    
    // Since is not 10 
    // Then execute the else statement 
    else
        cout << "i is 20\n"
    
    cout << "Outside if-else block"
    
    return 0; 


Output

i is 20
Outside if-else block

Explanation:

  • Program starts.
  • i is initialized to 20.
  • if-condition is checked. i == 10, yields false.
  • flow enters the else block.
  • “i is 20” is printed
  • “Outside if-else block” is printed.

Example 2:

Another program to illustrate the use of if else in C.

C++




// C++ program to illustrate if-else statement 
    
#include <iostream> 
using namespace std; 
    
int main() 
    int i = 25; 
    
    if (i > 15) 
        cout << "i is greater than 15"
    else
        cout << "i is smaller than 15"
    
    return 0; 
}


Output

i is greater than 15

Related Articles:

  1. Decision Making in C / C++
  2. C/C++ if statement with Examples
  3. C/C++ if else if ladder with Examples
  4. Switch Statement in C/C++
  5. Break Statement in C/C++
  6. Continue Statement in C/C++
  7. goto statement in C/C++
  8. return statement in C/C++ with Examples
  9. Program to Assign grades to a student using Nested If Else


Previous Article
Next Article

Similar Reads

C++ Nested if-else Statement
In C++, there are various types of conditional statements such as if, if-else, nested if, nested if-else, and switch. In this article, we will learn about the nested if-else statement. What are Nested if-else Statements?Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else sta
3 min read
C++17 new feature : If Else and Switch Statements with initializers
In many cases, we need to check the value of something returned by a function and perform conditional operations based on this value. So our code looks something like this // Some function return_type foo(params) // Call function with params and // store return in var auto var = foo(params); if (var == /* some value */) { //Do Something } else { //
3 min read
Find number formed in K steps by reducing N by 1 if last digit is 0 else divide by 10
Given two integers N and K. Perform the following type of operations on N: if the last digit of N is non-zero, decrease the number by one.if the last digit of N is zero, divide the number by 10 (i.e. remove the last digit). The task is to print the result after K such operations. Examples: Input: N = 512, K = 4Output: 50Explanation: Following are t
4 min read
Execute both if and else statements in C/C++ simultaneously
Write a C/C++ program that executes both if-else block statements simultaneously. Syntax of if-else statement in C/C++ language is: if (Boolean expression) { // Statement will execute only // if Boolean expression is true } else { // Statement will execute only if // the Boolean expression is false } Hence we can conclude that only one of the block
2 min read
switch vs if else
Prerequisite - Switch Statement, Decision making(if else) A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. Check the Testing Expression: An if-then-else statement can test expressions
3 min read
C++ if else if Ladder
Decision-making in C++ helps to write decision-driven statements and execute a particular set of code based on certain conditions. In C++, the if-else-if ladder helps the user decide from among multiple options. The C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associate
3 min read
Difference Between if-else and switch in C
In C programming both switch statements and if-else statements are used to perform decision-making and control the flow of the program according to predefined conditions. In this article, we will discuss the differences between the if-else and switch statements. switch StatementA control flow statement called a switch statement enables a program to
4 min read
Print "Even" or "Odd" without using conditional statement
Write a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, &lt;,&gt;,...etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read
C++ Break Statement
The break in C++ is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there and control returns from the loop immediately to the first statement after the loop. Syntax: break; Basically, break statements are used in situations when we are not sure
5 min read
goto Statement in C
The C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read
Article Tags :
Practice Tags :