Let’s consider the decision making in C and C ++, which determine the direction of the program execution flow, the main operators.
Programming solutions
In real life, situations arise when we need to make some decisions, and on the basis of these decisions we decide what to do next.
Similar situations arise in programming, when it is necessary to make some decisions and, based on them, execute the next block of code. For example in C if happens xthen execute yotherwise do z…
There can also be several conditions, like in C, if xthen execute p, otherwise, if ythen execute qotherwise do r… This condition C else … if is one of many ways to enter multiple conditions.
Below we will look at the basic operators for executing the required blocks of code.
C / C ++ if statement
The if statement is the simplest statement to make a decision. It is used to determine if a specific statement or block of statements will be executed, i.e. if the specified condition is true then the statement block is executed otherwise not.
Syntax:
if(условие)
{
// выполнение кода если
// условие истинно (true)
}
Here the condition after checking will be either true or false. In C language if
operator takes boolean values - if the value is true, then it will execute the block of statements below it, otherwise it will not.
Without curly braces’{‘and’}‘after if (условие)
, then by default the operator if
will assume that the first statement immediately below it is inside its block.
Example:
if (условие)
выражение1;
выражение2;
Above, if the condition is true, the if-block will only consider выражение1
as being inside its block.
Block diagram:
In language C:
// C program to illustrate If statement
#include <stdio.h>
int main() {
int i = 10;
if (i > 15)
{
printf("10 is less than 15");
}
printf("I am Not in if");
}
In language C ++:
// C++ program to illustrate If statement
#include<iostream>
using namespace std;
int main()
{
int i = 10;
if (i > 15)
{
cout<<"10 is less than 15";
}
cout<<"I am Not in if";
}
Output:
I am Not in if
Since the condition present in the operator if
, false, then the block under the operator if
not executed.
if-else in C / C ++
Just one statement if
tells us that if the condition is true, then the block of expressions is executed, and if the condition is false, then no.
But what if we want to do something else if the condition is false. To do this, use the operator else
at C… Operator else
can be used with operator if
to execute a block of code if the condition is false.
Syntax:
if (условие)
{
// запустить этот блок если
// условие истинно (true)
}
else
{
// запустить этот блок если
// условие ложно (false)
}
Block diagram:
In language C:
// C program to illustrate If statement
#include <stdio.h>
int main() {
int i = 20;
if (i < 15)
printf("i is smaller than 15");
else
printf("i is greater than 15");
return 0;
}
In language C ++:
// C++ program to illustrate if-else statement
#include<iostream>
using namespace std;
int main()
{
int i = 20;
if (i < 15)
cout<<"i is smaller than 15";
else
cout<<"i is greater than 15";
return 0;
}
Output:
i is greater than 15
Code block following a statement else
, is satisfied when the condition present in the operator if
, false.
Nested if in C / C ++
Nested if
at C is an operator if
which is the target of another operator if
… Nested operator if
means operator if
inside another operator if
…
Yes, and Cand C ++ allow us to nest operators if
to operators if
, i.e. we can put the operator if
inside another operator if
…
Syntax:
if (условие1)
{
// Выполняется, когда условие1 верно
if (условие2)
{
// Выполняется, когда условие2 истинно
}
}
Block diagram:
In language C:
// C program to illustrate nested-if statement
#include <stdio.h>
int main() {
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf("i is smaller than 15n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 toon");
else
printf("i is greater than 15");
}
return 0;
}
In language C ++:
// C++ program to illustrate nested-if statement
#include <iostream>
using namespace std;
int main()
{
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
cout<<"i is smaller than 15n";
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
cout<<"i is smaller than 12 toon";
else
cout<<"i is greater than 15";
}
return 0;
}
Output:
i is smaller than 15
i is smaller than 12 too
if else if in C / C ++
Here the user works with a choice of one of several options. Operators C if
are performed from top to bottom.
As soon as one of the conditions governing the condition if
, is true, the statement associated with this condition is executed if
and the rest of C else if
– bypasses.
If none of the conditions are met, then the last statement is executed else
…
Syntax:
if (условие)
выражение;
else if (условие)
выражение;
.
.
else
выражение;
Block diagram:
In language C:
// C program to illustrate nested-if statement
#include <stdio.h>
int main() {
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
In language C ++:
// C++ program to illustrate if-else-if ladder
#include<iostream>
using namespace std;
int main()
{
int i = 20;
if (i == 10)
cout<<"i is 10";
else if (i == 15)
cout<<"i is 15";
else if (i == 20)
cout<<"i is 20";
else
cout<<"i is not present";
}
Output:
i is 20
Additional operators
Additional operators break
, continue
, goto
, return
used in C or C ++ for unconditional control flow through functions in the program.
1.C break
This loop control parameter is used to end the loop. Once the interrupt statement break
occurs inside the loop, loop iterations are terminated, and control returns from the loop immediately to the first statement after the end of the loop.
Syntax:
break;
Mainly operators break
are used in situations where we are not sure about the actual number of loop iterations or we want to end the loop based on some condition.
In language C:
// C program to illustrate
// Linear Search
#include <stdio.h>
void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at position: %d", (i + 1));
break;
}
}
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
// no of elements
int n = 6;
// key to be searched
int key = 3;
// Calling function to find the key
findElement(arr, n, key);
return 0;
}
In language C ++:
// CPP program to illustrate
// Linear Search
#include <iostream>
using namespace std;
void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
cout << "Element found at position: " << (i + 1);
break;
}
}
}
// Driver program to test above function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = 6; // no of elements
int key = 3; // key to be searched
// Calling function to find the key
findElement(arr, n, key);
return 0;
}
Output:
Element found at position: 3
2. C continue
This loop control statement is similar to the break statement. The continue statement is the opposite of the break statement, instead of terminating the loop, it forces the next iteration of the loop.
As the name suggests, the continue statement causes the loop to continue or to perform the next iteration. When the continue statement is executed in a loop, the code within the loop following the continue statement will be skipped and the next iteration of the loop will begin.
Syntax:
continue;
In language C:
// C program to explain the use
// of continue statement
#include <stdio.h>
int main() {
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
In language C ++:
// C++ program to explain the use
// of continue statement
#include <iostream>
using namespace std;
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 5 7 8 9 10
3.C goto
The goto statement in C / C ++, also called the unconditional jump statement, can be used to jump from one point to another within a function.
Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to jump or jump to the expression marked as label
… Here label
is a user-defined identifier that points to the target operator.
Operator immediately following label:
is the assignment operator. label:
may also appear before the operator goto label;
in the above syntax.
Block diagram:
Below are examples of using the operator goto
…
In language C:
// C program to print numbers
// from 1 to 10 using goto statement
#include <stdio.h>
// function to print numbers from 1 to 10
void printNumbers()
{
int n = 1;
label:
printf("%d ",n);
n++;
if (n <= 10)
goto label;
}
// Driver program to test above function
int main() {
printNumbers();
return 0;
}
In language C ++:
// C++ program to print numbers
// from 1 to 10 using goto statement
#include <iostream>
using namespace std;
// function to print numbers from 1 to 10
void printNumbers()
{
int n = 1;
label:
cout << n << " ";
n++;
if (n <= 10)
goto label;
}
// Driver program to test above function
int main()
{
printNumbers();
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
4.C return
return
in C or C ++, returns the flow of execution of the function from which it is called. This statement does not necessarily require any conditional statements.
As soon as the statement is executed, the program flow stops immediately and returns the control from where it was called.
The return statement may or may not return anything for void
-function, but for non-void
-function needs to return a return value.
Syntax:
return[expression];
In language C:
// C code to illustrate return
// statement
#include <stdio.h>
// non-void return type
// function to calculate sum
int SUM(int a, int b)
{
int s1 = a + b;
return s1;
}
// returns void
// function to print
void Print(int s2)
{
printf("The sum is %d", s2);
return;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
Print(sum_of);
return 0;
}
In language C ++:
// C++ code to illustrate return
// statement
#include <iostream>
using namespace std;
// non-void return type
// function to calculate sum
int SUM(int a, int b)
{
int s1 = a + b;
return s1;
}
// returns void
// function to print
void Print(int s2)
{
cout << "The sum is "<< s2;
return;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
Print(sum_of);
return 0;
}
Output:
The sum is 20