Basic Cpp: Conditional Statements

If Else If

if and else are two of the most frequently used conditionals in C/C++, and they enable you to execute zero or one conditional statement among many such dependent conditional statements. We use them in the following ways:

  1. if: This executes the body of bracketed code starting with

if

  • evaluates to true.
    if (condition) {
        statement1;
        ...
    }
    
  • if – else: This executes the body of bracketed code starting with

if evaluates to true, or it executes the body of code starting with if

  • evaluates to false. Note that only one of the bracketed code sections will ever be executed.
    if (condition) {
        statement1;
        ...
    }
    else {
        statement2;
        ...
    }
    
  • if – else if – else: In this structure, dependent statements are chained together and the

for each statement is only checked if all prior conditions in the chain evaluated to false. Once a evaluates to true, the bracketed code associated with that statement is executed and the program then skips to the end of the chain of statements and continues executing. If each

 

in the chain evaluates to false, then the body of bracketed code in the else block at the end is executed.

if(first condition) {
    ...
}
else if(second condition) {
    ...
}
.
.
.
else if((n-1)'th condition) {
    ....
}
else {
    ...
}

Contoh:

#include <bits/stdc++.h>

using namespace std;



int main()
{
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    // Write Your Code Here
    if (n == 1) {
      cout << "one";
    } else if (n == 2) {
        cout << "two";
    } else if (n == 3) {
        cout << "three";
    } else if (n == 4) {
        cout << "four";
    } else if (n == 5){
        cout << "five";
    } else if (n==6){
        cout << "six";
    } else if (n == 7) {
        cout << "seven";
    } else if (n == 8) {
        cout << "eight";
    } else if (n == 9) {
        cout << "nine";
    } else if (n > 9) {
        cout << "Greater than 9";
    }

    return 0;
}

 

For Loop

A for loop is a programming language statement which allows code to be repeatedly executed.

The syntax for this is

for ( <expression_1> ; <expression_2> ; <expression_3> )
    <statement>
  • expression_1 is used for intializing variables which are generally used for controlling terminating flag for the loop.
  • expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated.
  • expression_3 is generally used to update the flags/variables.

A sample loop will be

for(int i = 0; i < 10; i++) {
...
}

Contoh:

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int i, c, b;
    string a[11] = {"even", "odd", "one",   "two",   "three", "four",
                    "five", "six", "seven", "eight", "nine"};
    cin >> c >> b;
    for (i = c; i <= b; i++) {
      if ((i > 9) && (i % 2 == 0))
        cout << a[0] << endl;
      else if ((i > 9) && (i % 2 != 0))
        cout << a[1] << endl;
      else
        cout << a[i + 1] << endl;
    }
    return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *