Can I initialized variable only once in while loop ?

Hello guys , I would like to ask a simple question which is can I initialized variables only once in while loop ? Here is the code , it is working fine but I want to make it simpler by set the variables only once . As you can see that I have to initialized the variables multiple times for part A , B , E even though the value is just the same. I have tried to use only one variable which is firstNum and secondNum but the codes only work until part A , Part B will be no output at all , I get it since the value of firstNum and secondNum already set to the max while conditions when part A is done , the point is I initialized the variabes in outer curly bracket , is it the initialized value should be separate from each part A,B,E curly brackets ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
		
	int firstNum, secondNum, result;
	
	cout << "Enter First Number : ";
	cin >> firstNum ;
	
	cout << "Enter Second Number : ";
	cin >> secondNum ;
	
	int firstnumPartA = firstNum, secnumPartA = secondNum ;
	int firstnumPartB = firstNum, secnumPartB = secondNum ;
	int firstnumPartC = 1, secnumPartC = 10;
	int firstnumPartD = 1, secnumPartD = 10;
	int firstnumPartE = firstNum, secnumPartE = secondNum ;
	
	{
		/* PART A */
		cout << "ODD NUMBERS IS : " ;
		while ( firstnumPartA < secnumPartA)
			{
				if (firstnumPartA % 2 == 1)
					cout << firstnumPartA << " ";
					firstnumPartA ++;
			}
	}
	{
		/*PART B*/
		cout << endl << "SUM OF EVEN NUMBERS IS : " ;
		int total = 0;	
		while (firstnumPartB < secnumPartB)
			{
				if(firstnumPartB % 2 == 0)
					total += firstnumPartB;
					firstnumPartB ++;
			}
					cout << total;
	}
	{		
		/* PART C */
		cout << endl << "NUMBER FROM 1 TO 10 : ";
		while (firstnumPartC <= secnumPartC)
			{
				cout << firstnumPartC << " ";
				firstnumPartC++;
			}
	}
	{
		/* PART D */
		cout << endl << "SQUARE OF THE NUMBER FROM 1 TO 10 :  ";
		while (firstnumPartD <= secnumPartD)
			{
				result = pow(firstnumPartD, 2);
				cout << result << " ";
				firstnumPartD++;
			}
	
	}
	{
		/* PART E*/
		cout << endl << "SUM OF THE SQUARES OF THE ODD NUMBERS : ";
		int result1 = 0;
		while (firstnumPartE < secnumPartE)
			{
				if (firstnumPartE %2 == 1)
					result1 += pow (firstnumPartE, 2);
					firstnumPartE ++;
			}
					cout << result1;

	return 0;
	}
}
Last edited on
As you can see that I have to initialized the variables multiple times for part A , B , E even though the value is just the same.

BY this, I assume you're referring to firstnumPartx and secondnumPartx.
Yes, you have to initialize firstnumPartx for each section because you modify firstnumPartx within the while loop.

You don't need secondnumPartx for each section because secondnumPartx never changes and you can just refer to secondNum.
Registered users can post here. Sign in or register to post.