while and if , else combination

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
Last edited on
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
There are examples on that link, I don't understand what you expect me to give you as an example.
loops and decisions work on a single statement.

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)
      do this statement;

while (something is true)
{
      do this 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)
      do this statement;
else
      do this statement;

if (something is true)
{
      do this statement;
      and this statement;
      and this one too;
}
else
{
      do this 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)
      do this statement;

if (something is true)
{
      do this 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;
}


Does that help?
Last edited on
dear ajh32 ,
thanks for your help sir , this program sir same library with the if/else loop right ?

I think there may be some translation errors, the word "library" does not make sense the way you use it.
to LB : sorry sir i mean header , i used to say that when i was studying C programming
Hm, I am still confused - you don't need any headers to use if and else.
to LB , i really am a beginner i mean this sir
#include<iostream.h>
#include<conio.h>
what do you call that sir ? i'm also confused sorry
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.
<iostream.h> is deprecated - use <iostream> instead

<conio.h> is non-standard - do not use it ever:
https://en.wikipedia.org/wiki/Conio.h
to Lb , thanks for the advice sir


to ajh32 , how to output the "fail and passed " part sir ?
To output the fail and passed is just simply using a cout statement. Such as

1
2
cout << "Student has passed\n";
cout << "Student has failed\n";



You just need to put those cout statement properly into the code where you write your "if" "else" statements.
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 ?


1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/jEywvCM9/

2) Why do you have:

cin>>x;

inside your while loop? x is your loop counter. Why are you asking the user to input a value that will change the value of that counter?
Last edited on
Topic archived. No new replies allowed.