Simple if statement if(expression is true) #include <iostream> using namespace std; int main () {
if (x == 100) { The if …… else statement if (expression is true) Example for if….else #include <iostream> using namespace std; int main () {
The switch statement This is a multiple-branching statement where, based on a condition, the control is transferred to one of the many possible points. This is implemented as follows: #include <iostream> using namespace std; int main () { int x; cout<<”Enter the value for x”; switch (x) { case 1:
The do-while loop The do-while is an exit-controlled loop. Based on a condition, the control is transferred back to a particular point in the program. The syntax is as follows: Example of do-while loop #include <iostream> using namespace std; int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }
The While Statement The while statement is also a loop structure, but is an entry-controlled one. The syntax is as follows:
while (condition is true) Example of while loop #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0;} |