sir i'm a newbie user here and i'm also a student of IT and i'm a having a problem about this while and if , else combination i really have a problem focusing on while statements because i don't understand it's syntax especially after the condition please just guide me on how to this
thanks for the reply LB you've been most helpful but i really don't understand reading stuffs especially when it comes to C++ can you give me an example of this ? If u can't its okay
you can get them to work on lots of statements by "grouping" them together in { } to create a "compound" statement.
the while loop is the simplest loop in c++
1 2 3 4 5 6 7 8 9
while (something is true)
dothis statement;
while (something is true)
{
dothis statement;
and this statement;
and this one too;
}
the if statement is just as simple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if (something is true)
dothis statement;
elsedothis statement;
if (something is true)
{
dothis statement;
and this statement;
and this one too;
}
else
{
dothis statement;
and this statement;
and this one too;
}
the "else" part is optional
1 2 3 4 5 6 7 8 9
if (something is true)
dothis statement;
if (something is true)
{
dothis statement;
and this statement;
and this one too;
}
dear LB , my teacher gave me this problem "Using while and if/else statement create a program that will summarize the exam's result prompt the user to input the number of students , next to their names is written "1" if the student pass and "0" if fail
i really don't know how to start , because i'm having problems understanding while statements but if/else there's no problem
Well you show a string on the display asking the user to enter the number of students, read the response into a local variable. Then using a while loop, loop through the number of students and ask for the students name and exam result. Really a for loop is probably a better choice, but you've been told to use a while loop!
1 2 3 4 5 6 7 8 9
int num_students;
...
int idx(0);
while (idx < num_students)
{
...
++idx;
}
You don't need those headers for if/else/while etc.
The headers you mention are for manipulating input and output in a console environment in various ways.
They're not required when using if, else and while as those are all C++ keywords. A C++ compiler can recognise and interpret these words as necessary without the use of any headers or libraries.
to ajh32 ,
my program has a problem sir i started first with the while statements and this was the program.
#include<iostream>
using namespace std;
int main()
{
int num_students;
cout<<"Enter number of students:";
cin>>num_students;
int x=0;
while(x<num_students)
{
cout<<"Cecilian:";
cin>>x;
++x;
}
getchar();
return 0;
}
but the output dont stop outputting the word "cecilian" everytime i press enter what's the problem with the program sir ?