Conditionals
In case of condition, if it is true then just do something let's look on simple code for understanding
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter two numbers" << endl;
/* here, cout is just for better understaning like
what are we goin' to do */
cin >> a >> b;
if (a == b)
{
cout << "a and b are equal" << endl;
}
else
{
cout << "not equal" << endl;
}
}
if(condition), it's simple to understand like if the condition is true then print else(if condition not true) print other output and we can just use " if( ) ", when we don't want to output anything if condition is not true. Think what can we do if we have more then one condition, yes there is something for that we use else if when we require more then one condition(we can use just else to but it's more complicated and not good way to use ). Let's look on code
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter two numbers" << endl;
cin >> a >> b;
if (a == b)
{
cout << "a and b are equal" << endl;
}
else if (a < b)
{
cout << "a is smaller" << endl;
}
else
{
cout << "a is greater" << endl;
}
/*if (a == b)
{
cout << "a and b are equal" << endl;
}
else
{
if (a < b)
{
cout << "a is smaller" << endl;
}
else
{
cout << "a is greater" << endl;
}
}*/
//single line comment
}
Comment
Now, we have question about like why we are using "/*" in code, Basically it is called as comment.
If we need to write something in code and we want to declare that same time so can understand those code better way, because of that we use comment also compiler just ignore the comment line like you can write whatever you want.
There is two ways for writing comment first is // in this ways it's just comment one line, if we want more then one line then we have to use again and again and if we want to aviod that we just want to write once a time then look the code again there is other way u can use
Tips : If you are using visual studio code then just select all and after that press "ctrl + / " to comment all line it's same with all language like u can use in other langue to not just in C++
While Loop
In while loop if condition is true then repeat or do it again and again unless condition became false, let's look on while loop format
while(condition){
}
Now, Let's took a simple example : We have to print 1 to N numbers, and we will take N from user's
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
int i = 1;
while (i <= N)
{
cout << i << endl;
i++;
}
}
Let's break down the code, int i declaring code going to print form 1 and i++(or i = i + 1) means after we printing i, code are increasing then it's goin' in condition check is it true or not if it's true then it will execute again, otherwise it will be terminate(in simple word we are implementing from 1).
Let's do one more example Take a input N from user and check whether It's prime or not.
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
int d = 2;
bool divided = false;
while (d < N)
{
if (N % d == 0)
{
cout << "Not prime" << endl;
divided = true;
}
d++;
}
if (!divided)
{
cout << "Prime" << endl;
}
}